### prithwiraj15's blog

By prithwiraj15, 13 days ago, # Ode 2 Code 2021 Round-3

Round 3 of Ode 2 Code gave only one question with a total time of 45 mins to solve it. I was not able to solve this problem. Can anyone help me ? PS :- The competition is already over, hence it the question can be disclosed now

Question Statement

You are given

• An array A of N integers.

• And M queries consist of three integers k, y and x.

Beauty of a array A, A, A, A,...., A[N] is defined as A + A + A + A + .... + A[N].

For each query, k, y, x, you are given the following conditions :-

• Let U be the bitwise AND of the beauties of any y subarrays among all subarrays of size k.

• And, Z = U | x, where | is the bitwise OR operation.

Calculate the maximum possible value of Z for all queries.

Constraints

• 1 <= N <= 1000

• 1 <= M <= 1000

• 1 <= Ai <= 10^9

• 1 <= ki <= N

• 1 <= yi <= N-ki+1

• 1 <= xi <= 10^9

Sample Test Cases

1) Sample Case 1

N = 5

M = 2

A = [1, 2, 3, 4, 5]

Q = [[2, 3, 9], [3, 2, 17]]

Approach

For the first query

• We have 4 subarrays [1, 2], [2, 3], [3, 4], [4, 5] with beauties 3, 5, 7, 9 respectively.

• You can select bitwise AND of 3 beauties, i.e 3 & 5 & 7 = 1, so U = 1.

• Z = (1 | 9) = 9. This is the maximum possible.

For the second query

• We have 4 subarrays [1, 2, 3], [2, 3, 4], [3, 4, 5] with beauties 6, 9, 12 respectively.

• You can select bitwise AND of 2 beauties, i.e 9 & 12 = 8, so U = 8.

• Z = (8 | 17) = 25. This is the maximum possible.

2) Sample Case 2

N = 5

M = 2

A = [5, 4, 2, 4, 9]

Q = [[2, 2, 7], [2, 3, 6]]

Approach

For the first query

• We have 4 subarrays [5, 4], [4, 2], [2, 4], [4, 9] with beauties 9, 6, 6, 13 respectively.

• You can select bitwise AND of 2 beauties, i.e 9 & 13 = 9, so U = 9.

• Z = (9 | 7) = 15. This is the maximum possible.

For the second query

• We have 4 subarrays [5, 4], [4, 2], [2, 4], [4, 9] with beauties 9, 6, 6, 13 respectively.

• You can select bitwise AND of 3 beauties, i.e 6 & 6 & 13 = 4, so U = 4.

• Z = (4 | 6) = 6. This is the maximum possible.  Comments (26)
 » Auto comment: topic has been updated by prithwiraj15 (previous revision, new revision, compare).
 » Auto comment: topic has been updated by prithwiraj15 (previous revision, new revision, compare).
 » Constraints?
•  » » Updated the post with constraints now
•  » » » 12 days ago, # ^ | ← Rev. 2 →   This can be done in MNlog(max(A)) , just complement x, let x1 be the complement of x. First find out all sum with subarray size k. Start from the highest set bit of x1, find if there exist at least y elements with the set bit as highest set bit, if yes add that to answer and otherwise move to next bit, and check the same. Finally add all the set bits of x to the answer as well.
•  » » » » Oh my god.. I thought it was extremely difficult Thanks anyway for your help. Much appreciated.
•  » » » » 12 days ago, # ^ | ← Rev. 2 →   Here are you saying that maximizing U | x is same as maximizing U & !x
 » here is my codevector solve(int N, int M, vector A, vector > Q) { vector cum(N + 1, 0ll); for (int i = 1; i <= N; ++i) { cum[i] = cum[i - 1] + A[i - 1]; } vector ans; int n = N; for (auto& z : Q) { int k = z, y = z, x = z; vector tmp; for (int i = k - 1; i < n; ++i) { tmp.push_back(cum[i + 1] - cum[i + 1 - k]); } ll now = 0; unordered_set used; int sz = tmp.size(); for (int i = 31; i >= 0; --i) { if (x & (1ll << (i))) continue; int ct = 0; vector to_remove; for (int j = 0; j < sz; ++j) { if (used.count(j)) continue; if (tmp[j] & (1ll << i)) ct++; else { to_remove.push_back(j); } } if (ct >= y) { for (auto x : to_remove) { used.insert(x); } now |= (1 << (i)); } } now |= x; ans.push_back(now); } return ans; } 
 » 12 days ago, # | ← Rev. 2 →   On a side note, Is there anyone who passed all testcases in round 2 but was not selected for round 3? If it was based on speed , then any idea what the cutoff time was?
•  » » Yeah i was able to pass all the test cases of my given problem in round 2, but didn't get selected for Round 3!
•  » » Yeah me.
•  » » I think about 30 mins, cuz i completed mine with about 16-17 mins left.
•  » » I see, thanks for the response!!
•  » » I submitted my test before 25 min And I was selected for next round.
•  » » I had submitted under 15 mins but not selected.
•  » » » Yeah , same.
•  » » » 10 days ago, # ^ | ← Rev. 2 →   I was able to complete my test in 6 mins, but was not selected :(P.S. I didn't even get a rejection mail
•  » » 12 days ago, # ^ | ← Rev. 2 →   Yeah, I passed all test cases in Round 2 in 15mins but didn't get selected for Round 3. My friend passed just 7 test cases but got selected. It wasn't based on speed then, it might be random or based on code implementation.
•  » » No idea lol, I was done in like 7-8 mins. They probably thought I cheated
•  » » » Same here:)
 » 12 days ago, # | ← Rev. 3 →   In my case,It took 35 min to understand problem. Problem statement was too bad. when I understood question ,the time remain was less so I got panic and I quit the test.but after few hour,I started doing code and I solved the question in 8 min.[problem]
•  » » I had this same problem . Understanding the problem statement was tougher than solution..xD . It took me like 15-16 minutes to understand it and 10 minutes to solve .
•  » » » thanks for reply
 » Did everyone else get this problem? I got a different problem statement which was a little harder
 » Does anyone knows when and where will the result be declared ?
 » When round 3 result will anounce?