Three CodeVita problems that students report again and again, rebuilt with clean statements, sample cases and solutions in Python, C++, Java and JavaScript. Every solution was run against the examples shown before it was published. Try each one for 30 minutes before opening the solution — that is roughly the time budget a Round 1 problem deserves.
Some prime numbers can be written as the sum of two or more consecutive prime numbers — for example 5 = 2 + 3 and 17 = 2 + 3 + 5 + 7. Given a number N, count how many prime numbers less than or equal to N can be expressed as a sum of consecutive primes (at least two primes must be used).
Input: 20
Output: 2
5 = 2 + 3 and 17 = 2 + 3 + 5 + 7 are the only such primes up to 20.
Input: 43
Output: 5
5, 17, 23 = 5 + 7 + 11, 31 = 7 + 11 + 13 and 41 = 2 + 3 + 5 + 7 + 11 + 13.
- 1 ≤ N ≤ 10⁶
- Each qualifying prime is counted once even if it has several representations.
Krishna has N boxes of candies, and he wants to pack all the candies into one box. Merging two boxes takes time equal to the total number of candies in the two boxes (the merged box then holds their sum). Krishna can merge in any order. Find the minimum total time needed to end with a single box.
Input: 4 1 2 3 4
Output: 19
Merge 1+2 (time 3), then 3+3 (time 6), then 4+6 (time 10): 3 + 6 + 10 = 19.
Input: 3 5 5 5
Output: 25
5+5 (10), then 10+5 (15): total 25.
- 1 ≤ N ≤ 10⁵
- 1 ≤ candies in a box ≤ 10⁹ — use 64-bit totals.
A positive integer D is a factor of N if N divided by D leaves no remainder. Given two integers N and K, print the K-th largest factor of N. If N has fewer than K factors, print 1.
Input: 12 3
Output: 4
Factors of 12 in decreasing order: 12, 6, 4, 3, 2, 1 — the 3rd is 4.
Input: 30 9
Output: 1
30 has only 8 factors, so the answer is 1.
- 1 ≤ N ≤ 10⁹
- 1 ≤ K ≤ 10⁶
How to use these
- Write the solution in your contest language from scratch — reading code is not the same as producing it under a clock.
- After it passes the samples, invent two edge cases (smallest input, largest input) and test those too. That habit is what separates "solved on sample" from "accepted".
- Note the pattern behind each problem: sieve + prefix idea, Huffman greedy with a heap, divisor pairs up to √N. Round 1 recycles patterns far more than it recycles problems.
Want the warm-up level first? The TCS NQT coding questions and advanced coding pages cover the same ideas with simpler constraints.

