Given an array A[] consisting of N components, the duty is to seek out the minimal size of the subarray ranging from every index and the bitwise OR worth is the utmost amongst all doable subarrays ranging from that index.
Examples:
Enter: A[] = [4, 5, 2, 9, 11]
Output: [4, 3, 2, 2, 1]
Rationalization: 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 [11] is having most OR of 11 and a minimal measurement of 1Enter: A[] = [7, 5, 2, 18, 11]
Output: [5, 4, 3, 2, 1]
Naive strategy: The essential technique to clear up the issue is as follows:
For each ith factor begin a loop from it to seek out all of the subarrays  ranging from that index and examine for the utmost XOR and minimal measurement.
Observe the steps talked about under to implement the concept:
- 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.
- For every index begin a nested loop from j = i to N-1:
- Return the array because the required reply.
Beneath is the implementation of the above strategy.
C++
|
Time Complexity: O(N2)
Auxiliary House: O(N)
Environment friendly Method: To resolve the issue comply with the under steps:
We are going to create an array  to retailer the newest occurence of a setbit of most doable OR for ith factor within the array and the ith outcome would be the most distinction of index of any setbit and present index.
Observe the under steps to implement the concept:
- 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 likewise set in present factor, replace the newest incidence of jth bit.
- If jth bit was set beforehand however not in present factor, 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 approach would be the reply for ith index. Retailer it in an array.
- For every array traverse all of the bits from j = 0 to 32:
- Return the array because the required reply.
Beneath is the implementation of the above strategy:
C++
|
Time Complexity: O(32 * N)
Auxiliary House: O(32)