Friday 29 July 2016

Problem : Consecutive Prime Sum Some prime numbers can be expressed as Sum of other consecutive prime numbers. For example 5 = 2 + 3 17 = 2 + 3 + 5 + 7 41 = 2 + 3 + 5 + 7 + 11 + 13 Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2. Write code to find out number of prime numbers that satisfy the above mentioned property in a given range.

import java.util.*; class Prime{ public static void main(String []args) { int count=0,p=2,i; Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n+1]; for(i=0;i<=n;i++) { a[i]=i; } for(p=2;p*p<=n;p++) { if(a[p]!=0) { for(i=p*2;i<=n;i+=p) { a[i]=0; } int sum=5; for(i=5;i<=n+2;i=i+2){ if((a[i]!=0 && a[i]==sum)||a[i]==-1) count=count+1; if (a[i] != 0 || a[i] == -1) sum=sum+i; if(a[sum]!=0) a[sum]=-1; } } } System.out.print(count); } }

Friday 8 July 2016

That's the Solution to a CODECHEF'S problem (Andrash and Stipendium) .Find bugs in this code......




problem:
Andrash and Stipendium







Andrash study in Uzhland National University. Now is the time of exam results. Andrash similar to other students, hopes that this scores in the exam could fetch him a scholarship/stipendium for his studies.
There are following simple rules to receive stipendium:
  • University follows 5 point grading system. In an exam, a student can receive any score from 2 to 5. 2 is called an F grade, meaning that student has failed that exam.
  • Student should not have fail any of the exams.
  • Student must obtain a full score in some of his/her exams to show that he/she is excellent in some of the subjects.
  • He/She must have a grade point average not less than 4.0
You are given information regarding the exams and how Andrash performed in those. Can you help him figure out whether he will receive the stipendium or not!!
Input
The first line of input contains a single integer T denoting the number of test cases. This will be followed by T test cases.
The first line of each test case contains an integer N denoting the number of examinations.
The next line of each test case contains N space separated integers denoting Andrash's score for each exam.
Output
For each of the T test cases, output a single line - "Yes" (without quotes) if Andrash will receive stipendium, or "No" (without quotes) - otherwise.
Constraints and Subtasks
  • 1T40
  • Let A[i] denote Andrash's score for i-th exam
Subtask #1: 20 points
  • 1N100
  • 2A[i]5
Subtask #2: 20 points
  • 1N105
  • 3A[i]5
Subtask #3: 60 points
  • 1N105
  • 2A[i]5
Example
Input:
2
5
3 5 4 4 3
5
3 4 4 4 5

Output:
No
Yes
Explanation
Example case 1. Student's average grade point score is 3.8 which makes him/her unqualified for obtaining the stipend.
Example case 2. Student satisfies all the criteria for getting stipend.


Solution:

import java.util.Scanner;
import java.util.Arrays;
class Stipend{
public static void main(String []args){

int i,j,avg=0,T,t,N;
Scanner sc=new Scanner(System.in);
T=sc.nextInt();

for(t=1;t<=T;t++){
N=sc.nextInt();

int[]a=new int[N+1];
for(j=0;j<N;j++ ){
  a[j]=sc.nextInt();
  avg=avg+a[j];
}

  avg=avg/N;
  Arrays.sort(a);
if(avg>=4)
{
    if(a[N-1]==5)
    {
if(a[1]==2){
        System.out.println("No");
    }
else{
System.out.println("Yes");
}}

else{
System.out.println("no");}
   

}
else{
    System.out.println("No");
    }

}
}
}