Блог пользователя mkagenius

Автор mkagenius, 13 лет назад, По-английски

Here is the problem statement- if u can login — https://bytecode.interviewstreet.com/challenges/dashboard/#problem/4f497b0018bad

otherwise — http://pastebin.ca/2121976

My approach,

Make a cumulative sum array.

Now for every index starting from 0, do binary search for k, k+mod, k + 2*mod, ..., k + 76*mod on range limited by previous max-length found, i.e. if we already have found an array of length L, which sum upto k (after doing mod) , then accordingly we have to modify the binary-search space, because we do not require a shorter-length answer.

Will this approach time-out ? As I can't submit there anymore, I need your help to see whether it is correct approach or not.

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
13 лет назад, # |
Rev. 2   Проголосовать: нравится +2 Проголосовать: не нравится

There is solution for O(max(n, m))
1. Make a cumulative sum array. A
2. Make an array FirstTimeOccurence[0..m-1], F[i] = -1

for (int i = 0; i < n; i++)
{
   A[i] %= mod;
   if (F[(k - A[i] + mod) % mod] != -1) // check subarray (F[(k - A[i] + mod) % mod], i)
   if (F[A[i]] == -1)
       F[A[i]] = i;
}

"Beware of bugs in the above code; I have only proved it correct, not tried it." (c) Donald Knuth