Data Structures & AlgorithmsVery hardJava · GATE CS
A Java program stores dp[i] in an int array, sets dp[0] = dp[1] = 1, uses dp[i] = dp[i-1] + dp[i-2] for i up to 50, and never applies a modulus. What value does dp[50] hold, given that Java int wraps around in 32-bit two's complement?
- A. 20365011074
- B. -298632863
- C. -1109825406
- D. 365010934
Correct answer: C. -1109825406
The true value of dp[50] is far above 2³¹ − 1, so each addition wraps modulo 2³². Java defines this wrap-around (unlike C signed overflow), so the stored result is the true value reduced into the signed 32-bit range.
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
- Words available: a, ab, b, ba, aa, abba, bab, aab. How many different ways can the string 'abbaaab' be split into a sequence of these…
- A rod of length 8 can be cut into integer pieces. A piece of length i sells for price[i], where price = [0, 1, 5, 8, 9, 10, 17, 17, 20]…
- In how many ways can 12 be written as a sum of DISTINCT positive integers, where order does not matter (so 12 itself counts as one way)?
- Balloons with values [3, 1, 5, 8, 2] are burst one at a time. Bursting balloon i gives coins = left × value[i] × right, where left and…
- How many non-empty contiguous subarrays of [4, -2, 7, -5, 3, 6] have a sum of at most 4?
- Starting from n = 191, in each step you may replace n by n/2 when n is even, or by n−1 or n+1 when n is odd. What is the minimum number of…