304 North Cardinal St.
Dorchester Center, MA 02124

# Discover smallest Subarray with Most Xor ranging from every index

Given an array A[] consisting of N components, the duty is to search out the minimal size of the subarray ranging from every index and the bitwise OR worth is the utmost amongst all potential subarrays ranging from that index.

Examples:

Enter: A[] = [4, 5, 2, 9, 11]
Output: [4, 3, 2, 2, 1]
Clarification: For i=0, subarray [4, 5, 2, 9] is having most OR of 15 and a minimal measurement of 4
For i=1, subarray [5, 2, 9] is having most OR of 15 and a minimal measurement of three
For i=2, subarray [2, 9] is having most OR of 11 and a minimal measurement of two
For i=3, subarray [9, 11] is having most OR of 11 and a minimal measurement of two
For i=4, subarray  is having most OR of 11 and a minimal measurement of 1

Enter: A[] = [7, 5, 2, 18, 11]
Output: [5, 4, 3, 2, 1]

Naive strategy: The fundamental method to resolve the issue is as follows:

For each ith aspect begin a loop from it to search out all of the subarrays  ranging from that index and test for the utmost XOR and minimal measurement.

Observe the steps talked about beneath to implement the thought:

• Begin iterating from i = 0 to N-1:
• For every index begin a nested loop from j = i to N-1:
• Calculate the bitwise XOR if the subarray [i, j] and replace the utmost XOR and minimal measurement accordingly.
• Retailer the minimal measurement in an array.
• Return the array because the required reply.

Beneath is the implementation of the above strategy.

## C++

 ` `  `#embody ` `utilizing` `namespace` `std;` ` `  `vector<``int``> MaxBitWiseOR_Array(vector<``int``>& nums)` `{` `    ``int` `n = nums.measurement();` `    ``if` `(n == 1 and nums == 0)` `        ``return` `{ 1 };` `    ``vector<``int``> mor(n), ans(n);` `    ``mor[n - 1] = nums[n - 1];` `    ``for` `(``int` `i = n - 2; i >= 0; i--)` `        ``mor[i] = mor[i + 1] | nums[i];` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``int` `j = i;` `        ``int` `x = 0;` `        ``whereas` `(j < n and x != mor[i]) = nums[j];` `            ``j++;` `        ` `        ``if` `(j < n and nums[j] == 0 and that i == j)` `            ``j++;` `        ``ans[i] = j - i;` `    ``}` `    ``return` `ans;` `}` ` `  `int` `foremost()` `{` `    ``vector<``int``> Arr = { 4, 5, 2, 9, 11 };` ` `  `    ` `    ``vector<``int``> ans = MaxBitWiseOR_Array(Arr);` `    ``for` `(``int` `i = 0; i < ans.measurement(); i++)` `        ``cout << ans[i] << ``" "``;` ` `  `    ``return` `0;` `}`

Time Complexity: O(N2)
Auxiliary Area: O(N)

Environment friendly Method: To unravel the issue observe the beneath steps:

We are going to create an array  to retailer the most recent occurence of a setbit of most potential OR for ith aspect within the array and the ith consequence would be the most distinction of index of any setbit and present index.

Observe the beneath steps to implement the thought:

• Traverse from i = N-1 to 0:
• For every array traverse all of the bits from j = 0 to 32:
• If jth bit was set previoulsy and in addition set in present aspect, replace the most recent incidence of jth bit.
• If jth bit was set beforehand however not in present aspect, then additionally jth bit can be set in reply.
• Replace the utmost size as the utmost amongst max size and the distinction between the index of jth set bit and i.
• If jth bit was not set beforehand, set the jth bit and replace its newest incidence.
• The utmost size calculated on this method would be the reply for ith index. Retailer it in an array.
• Return the array because the required reply.

Beneath is the implementation of the above strategy:

## C++

 `#embody ` `utilizing` `namespace` `std;` ` `  `vector<``int``> MaxBitWiseOR_Array(vector<``int``>& nums)` `{` `    ``int` `n = nums.measurement();` `    ``vector<``int``> res(n, 1);` `    ``vector<``int``> newest(32);` `    ``for` `(``int` `i = n - 1; i >= 0; i--) {` `        ``for` `(``int` `j = 0; j < 32; j++) {` `            ``if` `(nums[i] & (1 << j))` `                ``newest[j] = i;` `            ``res[i] = max(res[i], newest[j] - i + 1);` `        ``}` `    ``}` `    ``return` `res;` `}` ` `  `int` `foremost()` `{` `    ``vector<``int``> Arr = { 4, 5, 2, 9, 11 };` ` `  `    ` `    ``vector<``int``> ans = MaxBitWiseOR_Array(Arr);` `    ``for` `(``int` `i = 0; i < ans.measurement(); i++)` `        ``cout << ans[i] << ``" "``;` `}`

Time Complexity: O(32 * N)
Auxiliary Area: O(32)