Fancy Sequence - Problem

Design and implement a Fancy class that supports dynamic sequence operations:

  • Fancy() - Initializes the object with an empty sequence
  • append(val) - Appends an integer val to the end of the sequence
  • addAll(inc) - Increments all existing values in the sequence by integer inc
  • multAll(m) - Multiplies all existing values in the sequence by integer m
  • getIndex(idx) - Gets the current value at index idx (0-indexed) modulo 109 + 7. Returns -1 if the index is greater than or equal to the sequence length

All operations must handle large numbers and return results modulo 109 + 7.

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["Fancy", "append", "addAll", "append", "multAll", "getIndex", "getIndex", "getIndex"], values = [null, 2, 3, 7, 2, 0, 1, 2]
Output: [10, 14, -1]
💡 Note: append(2): [2], addAll(3): [5], append(7): [5,7], multAll(2): [10,14]. getIndex(0)=10, getIndex(1)=14, getIndex(2)=-1 (out of bounds)
Example 2 — Multiple Multiplications
$ Input: operations = ["Fancy", "append", "multAll", "getIndex", "multAll", "getIndex"], values = [null, 5, 2, 0, 3, 0]
Output: [10, 30]
💡 Note: append(5): [5], multAll(2): [10], getIndex(0)=10, multAll(3): [30], getIndex(0)=30
Example 3 — Empty Sequence
$ Input: operations = ["Fancy", "getIndex"], values = [null, 0]
Output: [-1]
💡 Note: getIndex(0) on empty sequence returns -1

Constraints

  • 1 ≤ val, inc, m ≤ 100
  • 0 ≤ idx ≤ 100
  • At most 105 calls total to append, addAll, multAll, and getIndex
  • All values modulo 109 + 7

Visualization

Tap to expand
Fancy Sequence - Lazy Propagation INPUT Operations: 1. Fancy() 2. append(2) 3. addAll(3) 4. append(7) 5. multAll(2) 6. getIndex(0,1,2) Sequence State: 2 idx 0 7 idx 1 ? idx 2 Transform: a*x + b add = 3, mult = 2 MOD = 10^9 + 7 ALGORITHM STEPS 1 Track Transforms Store add/mult globally 2 Append with Inverse Store: val * inv(mult) - add 3 Lazy Operations addAll: add += inc multAll: mult*=m, add*=m 4 Get Value Apply: seq[i]*mult + add Computation: idx 0: 2*2 + 3*2 = 10 idx 1: 7*2 + 0 = 14 idx 2: out of bounds = -1 All values mod (10^9+7) FINAL RESULT Output Array: 10 14 -1 get(0) get(1) get(2) [10, 14, -1] Explanation: After all operations: - Element 0: (2+3)*2 = 10 - Element 1: 7*2 = 14 - Element 2: Does not exist Status: OK - All queries resolved Key Insight: Instead of applying operations to all elements (O(n) per operation), track cumulative transforms globally. Use modular inverse for division. Apply transform only at query time: result = (val * mult + add) mod M. Time: O(1) for append/addAll/multAll, O(log M) for getIndex using modular inverse. TutorialsPoint - Fancy Sequence | Lazy Propagation with Transform Tracking
Asked in
Google 15 Facebook 12 Amazon 8
15.4K Views
Medium Frequency
~35 min Avg. Time
278 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen