Table of contents
1.
Introduction
2.
Problem Statement
3.
Approach
4.
Solution
5.
FAQs
6.
Key Takeaways
Last Updated: Aug 13, 2025
Easy

Prime Time Again (TCS Codevita)

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

TCS CodeVita is the largest global programming competition. The interested candidates for appearing in the TCS codevita competition can register and log in from anywhere at any time. TCS Codevita is a 24-hour online programming contest. All the coding enthusiasts can brush up their skills in programming through fascinating real-life experiences and challenges across three intriguing rounds-Pre Qualifier, Qualifier and Grand Finale. TCS Codevita even provides an opportunity to win the coveted "World's Best Coder" title and prize money of $10,000. 

Every year, before the start of the contest, TCS provides a sample of questions to anticipate the competition's difficulty. We share the TCS CodeVita Previous Year Questions for the practising purpose of the participants. The solutions for the TCS CodeVita previous year questions are also provided. In this blog, we will discuss a problem, Prime Time Again, and learn the solution. It is a problem asked in TCS CodeVita previous year questions(2021). Let us dive into the problem statement.

Problem Statement

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 daypart. 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 the 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 – We require each equivalent hour in each part of 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 the number of hours per day and the number of parts in a day, respectively.

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

Time Limit:
1

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 – an instance of equivalent prime hours

7~19~31 – an instance of equivalent prime hours

11~23~X

Hence the answer is 2.

Example 2

Input

49 7

Output

0

Explanation

Duration of each day part = 7

2~9~X~23~X~37~X

3~X~17~X~31~X~X

5~X~19~X~X~X~47

7~X~X~X~X~X~X

Hence there are no equivalent prime hours.

Check out this article to find out everything about TCS NQT.

Approach

If we have D hours and there are P parts in a day, this implies, let's say p = the total number of groups with P parts in a day. To store p groups, with each group having P parts, we need a 2D array with p rows and P columns. Now we can store all the possible groups in the 2D array. Suppose D=24 and P=2 => p=D/P = 12, we will need an array of size arr[p][P] = arr[12][2]. To store the corresponding time interval in groups, we take a counter t and loop over the 2D array column-wise, store the value and increment the counter.

The array for the above example will look as follow:

1-13

2-14

3-15

.

.

11-23

12-24

Now to get the total number of groups which satisfy the equivalent prime hours, i.e. all part hours in a group are prime, all we have to do is traverse row-wise over the columns and check if all the columns corresponding to the particular row are prime and if the condition is satisfied increment the answer counter.

Return the answer counter.

Check out TCS Interview Experience to learn about TCS’s hiring process.

Solution

We have gone through the problem statement provided. We have gone through the problem statement provided. Now let us see what can be a possible solution.

#include<bits/stdc++.h>
using namespace std;
bool prime(int n)
{
    if(n==1)
        return false;
    for(int i=2;i<=(int)sqrt(n);i++)
    {
        if(n%i==0)
            return false;
    }
    return true;
}
int main()
{
    int D,P,i,j,p,t=1;
    cin>>D>>P;
    p=D/P;
    int ti[p][P];
    for(i=0;i<P;i++)
    {
        for(j=0;j<p;j++)
        {
            ti[j][i]=t++;
        }
    }
    t=0;
    for(i=0;i<p;i++)
    {
        bool check=true;
        for(j=0;j<P;j++)
        {
            if(!prime(ti[i][j]))
            {
                check=false;
                break;
            }
        }
        if(check)
            t++;
    }
    cout<<t;
}

FAQs

  1. Is camera on in TCS CodeVita?
    The pre-qualifier round of TCS CodeVita is a 6hours online contest and is a non-proctored round(Camera is off).
  2. What is the prize money for TCS CodeVita?
    TCS CodeVita provides an opportunity for the participants to win a cash prize of $10,000.
  3. Do questions repeat in TCS CodeVita?
    Questions are not repeated in the TCS CodeVita contest but the mock set or the MockVita uses the TCS CodeVita previous year questions for the
    practising purpose.

Key Takeaways

In this blog, we got to know briefly about the TCS CodeVita contest and discussed one of the TCS CodeVita previous year questions, Prime Time Again with its solution. You can try out Prime Time Again on your own available at our website. We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our Coding Ninjas Blog site and visit our Library. You can also consider our Competitive Programming Course to give your career an edge over others!

Do upvote our blog to help other ninjas grow.

Happy Learning! 

Live masterclass