Initializing an array in constant time

1,440.1K Views

Consider the following array operations.  Init(N,d) initializes an array of N elements so that each element has value d.  Once Init has been called, the following two operations can be applied:  For any i such that 0 <= i < N, Get(i) returns the array element at position i and Set(i,v) sets the array element at position i to the value v.

Given any amount of memory you want, implement the three operations so that each operation has an O(1) time complexity.

Share
Add Comment

  • 0 Answer(s)
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.
  • More puzzles to try-