Data Structures & AlgorithmsHardGATE CS · ICPC regional
From the number 1, the allowed moves are x -> x+1, x -> x-1 and x -> 2x, staying within [1, 40]. What is the minimum number of moves to reach 20 from 1?
- A. 6
- B. 5
- C. 7
- D. 9
Correct answer: B. 5
A breadth-first search over the values gives the minimum; greedy halving does not always work for this set of moves.
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
- In the list [1, 3, 4, 7, 2, 9], how many nodes come strictly before the first node whose value exceeds 5?
- How many index pairs i < j in [3, 5, 6, 1] satisfy a[i] XOR a[j] < a[i]?
- How many pairs of positions i < j in the string aabbab have s[i] = a and s[j] = b (the letters need not be adjacent)?
- What is the sum of the prefix-function values pi[0] through pi[n-1] of the string abacabab?
- What is the sum of lcm(i, i+1) for i = 1 to 9?
- What is the smallest positive integer n such that n! has exactly 10 trailing zeros?