Showing posts with label TCS code vita questions with solution. Show all posts
Showing posts with label TCS code vita questions with solution. Show all posts

Prime Time Again - CodeVita 9 | Zonal Round

Prime Time Again


Here on earth, our 24-hour day is composed of two parts, each of 12 hours. Each hour in each part has a corresponding hour in the other part separated by 12 hours: the hour essentially measures the duration since the start of the day part. For example, 1 hour in the first part of the day is equivalent to 13, which is 1 hour into the second part of the day.

Now, consider the equivalent hours that are both prime numbers. We have 3 such instances for a 24-hour 2-part day:

5~17

7~19

11~23

Accept two natural numbers D, P >1 corresponding respectively to number of hours per day and number of parts in a day separated by a space. D should be divisible by P, meaning that the number of hours per part (D/P) should be a natural number. Calculate the number of instances of equivalent prime hours. Output zero if there is no such instance. Note that we require each equivalent hour in each part in a day to be a prime number.


Example:

Input: 
24 2

Output: 
3 (We have 3 instances of equivalent prime hours: 5~17, 7~19 and 11~23.)

Constraints

10 <= D < 500

2 <= P < 50

Input

Single line consists of two space separated integers, D and P corresponding to number of. hours per day and number of parts in a day respectively

Output

Output must be a single number, corresponding to the number of instances of equivalent prime number, as described above


Example 1

Input

36 3

Output

2

Explanation

In the given test case D = 36 and P = 3

Duration of each day part = 12

2~14~X

3~15~X

5~17~29 - instance of equivalent prime hours

7~19~31 - instance of equivalent prime hours

11~23~X

Hence the answers is 2.


Program:

#include<stdio.h>
#include<math.h>
int isprime(int n)
{
if(n==1)
return 0;
for(int i=2;i<=(int)sqrt(n);i++)
{
if(n%i==0)
return 0;
}
return 1;
}
int main()
{
int D,P,i,j,p,t=1;
scanf("%d",&D);
scanf("%d",&P);
p=D/P;
int time[p][P];
for(i=0;i<P;i++)
{
for(j=0;j<p;j++)
{
time[j][i]=t++;
}
}
t=0;
for(i=0;i<p;i++)
{
int flag=1;
for(j=0;j<P;j++)
{
if(!isprime(time[i][j]))
{
flag=0;
break;
}
}
if(flag)
t++;
}
printf("%d",t);

}


You can also run it on an online IDE:    

Your feedback are always welcome! If you have any doubt you can contact me or leave a comment!  Happy Coding !! Cheers!!!

Related Links:

https://codepiggy.blogspot.com/2021/02/constellation-codevita-9-zonal-round.html

Prime Counters - Code Vita 2017 | Round 1

Prime Counters


Problem : 

Given a number N, let CP(N) denote the number of primes between 1 and N (inclusive of N). We call N a prime counter if CP(N) is a prime (N need not be a prime).  For example, CP(3) = 2, CP(4) = 2, CP(5) = 2, CP(7) = 4.

Input Format:  

An integer T, number of test cases  T lines each containing two positive integers L, R separated by space

Output Format:  

T lines containing the number of prime counters between L and R (both inclusive) in the ith test case (or NONE is no prime counter exists in that range)

Constraints:  

L ≤ R ≤ 106

Example 1 

 Input  1
  1 10

Output 
 4

Explanation 
CP(1) = 0, CP(2) = 1, CP(3) = 2, CP(4) = 2, CP(5) = 3, CP(6) = 3, CP(7) = 4= CP(8) = CP(9) = CP(10)   Hence there are 4 prime counters, 3, 4, 5, 6 in the range 1 to 10.

Example 2  

Input  2
 2 20  3 30 

Output  
  8
 8

Explanation  
Upto 10, we have 4 prime counters. Between 11 and 20 the prime counters are 11, 12, 17, 18 and hence the count is 8. Between 21 and 30, we have no prime counters.

Program:

#include <stdio.h>
int prime(int n)
{
    int k,j,p=0;
    for(k=2;k<=n;k++)
    {
        int c=0;
        for(j=1;j<=k;j++)
            if(k%j==0)
            c++;
       if(c==2)
        p++;
    }
    return p;
}
int main() 
{
int l,t,r,i,cp[1000],k,f,c=0,j,count;
scanf("%d",&t);
for(int z=1;z<=t;z++)
{
count=0;
scanf("%d %d",&l,&r);
for( i=l;i<=r;i++)
   cp[i]=prime(i);
for(j=l;j<=r;j++)
{
    c=0;
    for (k = 1;k<=cp[j]*cp[j];k++) 
    {
          if (cp[j] % k == 0) 
            c++;
        }
        if (c == 2) 
        count++;
}
printf("%d\n",count);
}
return 0;

}

You can also run it on an online IDE: https://ide.geeksforgeeks.org/YphGa2McDy

Your feedback are always welcome! If you have any doubt you can contact me or leave a comment!! Cheers!!!

Related Links:
Air in the balloonhttps://codepiggy.blogspot.com/2020/03/air-in-balloons.html

Consecutive Prime Sum | Code Vita 2016 | round 1

Problem Description:

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.

Input Format:
First line contains a number N
Output Format:
Print the total number of all such prime numbers which are less than or equal to N.
Sample Input and Output


SNo.InputOutputComment
1202
(Below 20, there are 2 such numbers: 5 and 17).
5=2+3
17=2+3+5+7
2151

Program:

#include <stdio.h>
int prime(int b)
{
    int j,cnt;
   cnt=1;
     for(j=2;j<=b/2;j++)
     {
         if(b%j==0)
         cnt=0;
     }
     if(cnt==0)
     return 1;
     else
     return 0;
}
int main() {
 int i,j,n,cnt,a[25],c,sum=0,count=0,k=0;
 scanf("%d",&n);
 for(i=2;i<=n;i++)
 {
     cnt=1;
     for(j=2;j<=n/2;j++)
     {
         if(i%j==0)
         cnt=0;
     }
     if(cnt==1)
     {
        a[k]=i;
        k++;
        }
 }
 for(i=0;i<k;i++)
 {
     sum=sum+a[i];
    c= prime(sum);
    if(c==1)
    count++;
 }
 printf("%d",count);
 return 0;
}

Output:

20

2

You can also run it on a online IDEhttps://ide.geeksforgeeks.org/XcOKzTd4Ik
If you have any doubt you can contact me or comment it below! Your comments and feedbacks are also welcomed!! Cheers!!

Related Links: Jumble with Numbers

Super Market Problem | TCS Code Vita 2023 - Zone 1 | Super Market TCS Code Vita 2023 Solution | Code Vita 2023 | Code Vita 2023 season 11 solution

 Problem Description: In a Super market we will find many variations of the same product. In the same way we can find many types of rice bag...