Data Structures & AlgorithmsHardGATE CS · Codeforces Div2 C
What is the minimum number of perfect squares (1, 4, 9, 16, ...) whose sum is exactly 12? Repeats are allowed.
- A. 4
- B. 2
- C. 3
- D. 12
Correct answer: C. 3
12 = 9 + 1 + 1 + 1 needs 4 squares, but 4 + 4 + 4 needs only 3. Taking the largest square each time is the greedy trap; the correct method is the DP over all squares not exceeding the target.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Stock prices by day are [3, 3, 5, 0, 0, 3, 1, 4, 9, 2]. You may complete at most TWO buy-then-sell transactions, and you must sell before…
- Daily prices are [1, 2, 3, 0, 2, 5, 1, 6]. You may buy or sell one share per day, hold at most one share, and after SELLING you must wait…
- What is the maximum sum of a non-empty contiguous subarray of the CIRCULAR array [5, -3, 5, -2, 7, -6, 4]? A subarray may wrap from the…
- Three fair six-faced dice are rolled. How many ordered outcomes (d1, d2, d3) have a total of exactly 10?
- How many arrays of length 4 with entries from {1, 2, 3} have no two adjacent entries equal?
- Grid rows (top to bottom): '....', '.#..', '...#', '#...', '....'. '#' is an obstacle, '.' is free. Starting at the top-left cell, you…