Prime Time Again

You have been given two integers ‘DAY_HOURS’ and ‘PARTS’. Where ‘DAY_HOURS’ is the number of hours in a day and a day can be divided into ‘PARTS’ equal parts. Your task is to find total instances of equivalent prime groups. Prime group refers to the group of elements (hours) which are prime and measure the same equivalent time since the start of the day.

For example, if we consider ‘DAY_HOURS’ to be 24 and ‘PARTS’ to be 2, then the day of total 24 hours is divided into 2 parts ( 1 - 12 ) and ( 13 - 24 ). 5 hours in the first part of the day is equivalent to 17, which is 5 hours into the second part of the day. And since 5 and 17 both are prime, they can be considered as a prime group.

Note:
1. Day starts with hour 1 and ends with hour  ‘DAY_HOURS’.

2. Each hour of the prime group should be in a different part of the day.

3. If there is no prime group then return zero.

4. ‘DAY_HOURS’ should be divisible by ‘PARTS’, meaning that the number of hours per part (DAY_HOURS/PARTS)  should be a natural number.

Example:

Let ‘DAY_HOURS’ = 20  and ‘PARTS’ = 2

Hence the view of our day would be in the following format: 

1  2  3  4  5  6  7  8  9 10      -  Part 1
11 12 13 14 15 16 17 18 19 20     -  Part 2

 1-11  Not a prime group because 1 is not prime.
 2-12  Not a prime group because 12 is not prime.
 3-13  Because both 3 and 13 are prime, it is an equivalent prime group.
 4-14  Not a prime group because 4 and 14 are not prime.
 5-15  Not a prime group because 15 is not prime.
 6-16  Not a prime group, because 6 and 16 are not prime.
 7-17  Because both 7 and 17 are prime, it is an equivalent prime group.
 8-18  Not a prime group, because 8 and 18 are, is not prime.
 9-19  Not a prime group because 9 is not prime.
 10-20 Not a prime group because both 10 and 20 are not prime.

 Hence there are 2 equivalent prime groups in the above format which are 3-13 and 7-17.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.

The first and the only line of each test case contains two space-separated integers ‘DAY_HOURS’ and ‘PARTS’. 
Output Format
The output for each test case contains a single integer denoting the number of instances of equivalent prime groups.

The output of each test case will be printed in a separate line.
Note
You are not required to print the output, it has already been taken care of. Just implement the function. 
Constraints:
1 <= T <= 100
10 <= DAY_HOURS <= 5 * 10^3
2 <= PARTS <= 10^3

Time Limit: 1 second
CodingNinjas
author
2y
Brute Force
  • This is a Naive/Brute Force approach
  • Create a 2-d array of size ‘P’ rows and ‘D/P’ columns and assign 1 to ‘D’ value in each cell.
  • Take an example with ‘D’ being 12 and ‘P’ being 2, then crea...read more
CodingNinjas
author
2y
Using the equation
  • This is the space optimization method of Approach 1
  • Each hour which we used to store in ARR[ROW][COL] in the previous method can be calculated by the equation ‘ROW*(D/P)+col+1’.
  • Suppos...read more
CodingNinjas
author
2y
Optimal Approach
  1. This is the optimization of Approach 2
  2. Create a list of size 5001 and assign all values True
  3. Call it ‘PRIME_ARR’ to keep track of number ‘i’ is prime or not
  4. Initially, Let ‘K’ equals 2, w...read more
Add answer anonymously...
INCA INFOTECH TECHNOLOGIES Assistant System Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter