The longest subarray

4.5

2 votes
Advanced Data Structures, Segment Trees, Data Structures
Problem

You are given an array of N positive integers and Q queries. In each query, you are given two integers l and r.

For each query, find the length of the longest subarray such that the bitwise AND of the subarray is 2k where lkr and if no subarray exists, then the answer will be 0.

 Input format

  • The first line contains T denoting the number of test cases. The description of each test case is as follows.
    • The first line contains an integer N denoting the number of array elements.
    • The next line contains N space-separated integers.
    • The next line contains an integer Q denoting the number of queries that are required to be performed.
    • Each of the next Q lines contains two integers l and r.

Output format

For each test case, Q lines must be printed and the ith line should contain the output for the ith query.

Constraints

1T20

1N,Q5105

1Ai1018

0lr60

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation
  • For the first query we can choose subarray [0,2].
  • For the next query we can choose subarray [3,3].
Editor Image

?