Java Code for Primevilla (TCS Codevita) | PrepInsta
Primevilla
TCS CodeVita is a coding competition organized by TCS every year, in search of world’s best coder. This is a global level coding competition in which coders from all around the world compete for the title of World’s Best Coder. Primevilla is one of the sample problem of this year TCS CodeVita season 11 competition.
Question -: It rains only on certain days in Primevilla. It is a rainy day when the number of days since a certain date is prime as well as the month is prime (i.e., month is one of Feb, Mar, May, Jul, Nov). Primevilla follows same calendar that we use.
Given the date d, identify if it would ever rain on a given weekday w within the next given n days. Also calculate the number of days r (where r > 0) after which it would rain within the next n days.
Constraints
2 <= n <= 10 ^ 10
Input
Input consists of three space delimited parts viz. <Date, DOW, n> where
- Date is of the format yyyymmdd
- DOW is Date of the Week in Ddd format
- n is a natural number (where n>=2)
Output
Output would be one of the three formats
- If it would rain on Ddd, r days after d within the next n days, print ‘Yes r’
- Else if p is the least prime > n such that it would rain on Ddd after p days, print ‘No p’
- Else print ‘No 0’, if it would never rain on Ddd.
Time Limit (secs)
1
Examples :
Example 1
Input
20211201 Sun 100
Output
Yes 67
Explanation
Starting from 20211201 we start checking for prime number days whether it would rain. The process will be as depicted below
20211201+2 is 20211203 Fri, month 12 is not prime —> It would not rain
20211201+3 20211204 Sat, month 12 is not prime —> It would not rain
..
20211201+31 is 20220101 Sat, month 1 is not prime —> It would not rain
..
20211201+67 is 20220206 Sun, month 2 is prime —> It would rain
Hence it could rain on a Sunday 67 days after the given date and within the given 100 days. Hence, output is “Yes 67”
Example 2
Input
20211201 Wed 100
Output
No 0
Explanation
The given date 20211201 is itself a Wednesday.
20211201+7 is 20211208 Wed, month 12 is not prime —> It would not rain
Also, any future Wednesday would be a multiple of 7 and hence non-prime days later than 20211201. So, it would never rain on future Wednesdays. Hence, output is “No 0”
Example 3
Input
19470815 Sat 150
Output
No 197
Explanation
If similar computation as depicted in prior examples is carried out one can figure out that it will rain on Sat, 28 Feb 1948 which is 197 days from 15 Aug 1947. However, since 197 is greater than 150 output is “No 197”.
import java.util.*; import java.time.*; import java.time.format.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String dye = scanner.next(); String dwk = scanner.next(); int h = scanner.nextInt(); Listpre = new ArrayList<>(); gi(365, pre); Map dadic = new HashMap<>(); dadic.put(0, "Mon"); dadic.put(1, "Tue"); dadic.put(2, "Wed"); dadic.put(3, "Thu"); dadic.put(4, "Fri"); dadic.put(5, "Sat"); dadic.put(6, "Sun"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); LocalDate dyeDate = LocalDate.parse(dye, formatter); int das = -1; for (int k : pre) { LocalDate dte = dyeDate.plusDays(k); if (pr_fun(dte.getMonthValue()) && dadic.get(dte.getDayOfWeek().getValue() - 1).equals(dwk)) { das = k; break; } } if (das == -1) { System.out.print("No 0"); } else if (das <= h) { System.out.print("Yes " + das); } else { System.out.print("No " + das); } } public static void gi(int h, List pre) { for (int k = 2; k <= h; k++) { if (pr_fun(k)) { pre.add(k); } } } public static boolean pr_fun(int h) { if (h <= 1) { return false; } for (int k = 2; k < h; k++) { if (h % k == 0) { return false; } } return true; } }
Login/Signup to comment