In C++, maximize the number of subarrays with zero XOR
We get an array Arr[] containing integer values. The goal is to find the maximum number of subarrays whose XOR is 0. The bits of any subarray can be swapped any number of times.
Note:- 118
In order to make the XOR of any subarray to 0 by swapping bits, two conditions must be met:-
- If the number of setting digits in the range from left to right is an even number.
For the sum of bits in any given range
Let’s look at various input and output scenarios-
In −Arr[] = { 1,2,5,4 }
Out −
Only the subarray that meets the first condition: 4
Subarray that satisfies two conditions: 3
In − Arr[] = { 3,7,2,9 }
Out −
Subarray condition that only satisfies the first condition: 6
Subarray that satisfies both conditions: 3
In the following program The method used is as follows -
In this method we observe that in order to make the XOR of any sub-array to 0 by swapping bits, two conditions must be fulfilled:- If the range is set from left to right The number of digits is an even number or the sum of digits for any given range
Get the input array Arr[] and calculate its length.
Function removeSubarr(int arr[], int len) returns the number of subarrays that do not meet condition 2.
Set the initial count to 0.
Iterate over the array using a for loop and take the variables sum and maxVal.
-
Use another for loop to iterate over the range of 60 subarrays, because beyond 60 subarrays, condition 2 will never be false.
Add elements to sum and take the maximum value in maxVal.
If sum is even and 2 * maxVal > sum, incrementing the count as condition 2 is not satisfied.
Both loops return count at the end.
Function findSubarrays(int arr1[], int len1) accepts an input array and its length, and returns the number of subarrays that meet the above two conditions.
Use a prefix array to count the number of subarrays that only meet condition 1.
Use a for loop to traverse the array and set each element __builtin_popcountll(arr1[i]) This is the number of bits set in it.
Use a for loop to populate the prefix array and set prefix[i] = prefix[i] prefix [i - 1] except for the first element.
Calculate the odd and even values in the prefix array.
Set tmp1 = ( oddcount * (oddcount-1) )/2 and tmp2= ( Evencount * (evencount-1) )/2 and take the result as the sum of the two.
The result will be the sum of subarrays that satisfy condition 1 only.
Print the results.
Now update the result with result=result - removeSubarr( arr1, len1).
The result now contains subarrays that satisfy both conditions.
-
Print the results again.
Example
#include <bits/stdc++.h> using namespace std; // Function to count subarrays not satisfying condition 2 int removeSubarr(int arr[], int len){ int count = 0; for (int i = 0; i < len; i++){ int sum = 0; int maxVal = 0; for (int j = i; j < min(len, i + 60); j++){ sum = sum + arr[j]; maxVal = arr[j] > maxVal ? arr[j]: maxVal; if (sum % 2 == 0){ if( 2 * maxVal > sum) { count++; } } } } return count; } int findSubarrays(int arr1[], int len1){ int prefix[len1]; int oddcount, evencount; int result; for (int i = 0; i < len1; i++) { arr1[i] = __builtin_popcountll(arr1[i]); } for (int i = 0; i < len1; i++){ prefix[i] = arr1[i]; if (i != 0) { prefix[i] = prefix[i] + prefix[i - 1]; } } oddcount = evencount = 0; for (int i = 0; i < len1; i++){ if (prefix[i] % 2 == 0) { evencount = evencount +1; } else { oddcount = oddcount +1; } } evencount++; int tmp1= ( oddcount * (oddcount-1) )/2; int tmp2= ( evencount * (evencount-1) )/2; result = tmp1+tmp2; cout << "Subarrays satisfying only 1st condition : "<<result << endl; cout << "Subarrays satisfying both condition : "; result = result - removeSubarr(arr1, len1); return result; } int main() { int Arr[] = { 1,2,5,4 }; int length = sizeof(Arr) / sizeof(Arr[0]); cout << findSubarrays(Arr, length); return 0; }
Output
If we run the above code it will generate the following output
Subarrays satisfying only 1st condition : 4 Subarrays satisfying both condition : 3
The above is the detailed content of In C++, maximize the number of subarrays with zero XOR. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

We have two arrays of integers, one with the calculated elements and the other with the split points required to split the array to generate subsets, we have to calculate the sum of each subset in each split and return the maximum subset Let's go through the example Understanding: - input −intarr[]=intarr[]={9,4,5,6,7}intsplitPoints[]={0,2,3,1}; output−the maximum subarray sum after each split [ 22,13,9,9] Explanation − Here we decompose the array according to its split points and get the maximum subset after each split and after the first split → {9} and {4,5,6,7 }>>The maximum sum of subarrays is -22 after the second split→{9},{4

In this article, we will use C++ to solve the problem of finding the number of subarrays whose maximum and minimum values are the same. The following is an example of the problem −Input:array={2,3,6,6,2,4,4,4}Output:12Explanation:{2},{3},{6},{6},{2 },{4},{4},{4},{6,6},{4,4},{4,4}and{4,4,4}arethesubarrayswhichcanbeformedwithmaximumandminimumelementsame.Input:array={3,3, 1,5,

In this post, we will find the number of subarrays with sum less than K using C++. In this problem, we have an array arr[] and an integer K. Now we need to find the subarrays whose sum is less than K. Here is the example −Input:arr[]={1,11,2,3,15}K=10Output:4{1},{2},{3}and{2,3} to find the solution Now we Two different approaches will be used to solve the given problem - brute force In this approach we will iterate through all sub-arrays and calculate their sum and if the sum is less than k then compare with k to increase our Answer. Example#include<

We get an array Arr[] containing integer values. The goal is to find the maximum number of subarrays whose XOR is 0. The bits of any subarray can be swapped any number of times. Note: -1

We have 5 integer variables Num, P1, P2, profit_P1, profit_P2 and the task is to maximize the profit and choose from all natural numbers in the range [1, Num]. The approach here is that if a positive number is divisible by P1, the profit is increased by profit_P1, similarly, if a number in the range is divisible by P2, the profit is increased by profit_P2. Furthermore, profits from positive integers can only be added at most once. Let us understand through examples: Input - intnum=4, P1=6, P2=2, profit_P1=8, profit_P2=2; Output - Maximize the total profit of all persons X4 Explanation - The range of numbers here is 1 to 4 ( [1, Nu

Given two binary strings str1 and str2 of the same length, we have to maximize the given function value by selecting substrings from the given strings of the same length. The given function is like this - fun(str1,str2)=(len(substring))/(2^xor(sub1,sub2)). Here, len(substring) is the length of the first substring and xor(sub1,sub2) is the XOR of the given substring, this is possible since they are binary strings. Example Input1:stringstr1=10110&stringstr2=11101Output:3 illustrates our

An array is a collection of similar data stored in adjacent memory locations in a contiguous manner. By defining the offset value as a specific base value for the database, it is easier to evaluate the specific position of each element. The base value for that particular index is zero, and the offset value is the difference between the two particular indices. A subarray is a part of a specific array and can be defined as a set of variables, labeled with multiple values. The longest subarray refers to an array in which all elements in the array are greater than K. Here the sum of the maximum sum subarray is - less than or equal to the given data set in the given data set. To find the length of the longest subarray given less than 1 in a data set, we just need to find the total number of 1's in a particular subarray. NOTE: The count should be greater than the count of zero. The greatest common divisor is a mathematical phenomenon in which I

A subarray is a contiguous portion of an array. For example, we consider an array [5,6,7,8], then there are ten non-empty subarrays, such as (5), (6), (7), (8), (5,6), (6, 7), (7,8), (5,6,7), (6,7,8) and (5,6,7,8). In this guide, we will explain all possible information in C++ to find the number of subarrays with odd sums. To find the number of subarrays of odd sums we can use different methods, so here is a simple example - Input:array={9,8,7,6,5}Output:9Explanation:Sumofsubarray-{9}= 9{7
