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!

我们有两个整数数组,一个具有计算的元素,另一个具有分割数组以生成子集所需的分割点,我们必须计算每个分割中每个子集的总和并返回最大子集让我们通过示例来理解:-输入−intarr[]=intarr[]={9,4,5,6,7}intsplitPoints[]={0,2,3,1};输出−每次分割后的最大子数组和[22,13,9,9]解释−这里我们根据数组的分割点来分解数组,并在每次分割后获得最大子集和第一次分割后→{9}和{4,5,6,7}>>最大子数组总和为-22第二次分割后→{9},{4

在本文中,我们将使用C++解决寻找最大值和最小值相同的子数组数量的问题。以下是该问题的示例−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,

我们有5个整数变量Num,P1,P2,profit_P1,profit_P2,并且任务是最大化利润,并从范围[1,Num]中的所有自然数中选择。这里的方法是,如果一个正数可以被P1整除,利润增加profit_P1,同样,如果范围内的数字可以被P2整除,利润增加profit_P2。此外,正整数的利润最多只能添加一次。让我们通过例子来理解:输入-intnum=4,P1=6,P2=2,profit_P1=8,profit_P2=2;输出-最大化所有人的总利润X4解释-这里的数字范围是1到4([1,Nu

给定两个相同长度的二进制字符串str1和str2,我们必须通过从给定的相同长度的字符串中选择子字符串来最大化给定的函数值。给定的函数是这样的-fun(str1,str2)=(len(子字符串))/(2^xor(sub1,sub2))。这里,len(substring)是第一个子字符串的长度,而xor(sub1,sub2)是给定子字符串的异或,因为它们是二进制字符串,所以这是可能的。示例Input1:stringstr1=10110&stringstr2=11101Output:3说明我们

我们得到一个任意给定长度的字符串“str”。任务是以这样的方式重新排列字符,使得在不从给定输入字符串中添加或删除字符的情况下,将有最大的子字符串成为回文字符串。回文字符串是字符以从头到尾发音相同的方式排列的字符串。让我们看看这种情况的各种输入输出场景-输入−stringstr="itnin"输出−重新排列字符串以最大化回文子串的数量为:iinnt。解释-我们得到一个字符串类型变量,比如说str。现在我们将重新排列输入字符串的字符,使其成为最大回文字符串,如果不可能,则返回“N

动态规划是一种优化算法技术,通过将特定问题分解为一些简单的子问题来解决它们。通过这个过程,我们可以将完整搜索的质量、条件或事实结合起来,以获得精确且准确的贪心算法。但这种方法本身就是一个矛盾,因为它有很大的优点,但这也是它最大的缺点和限制。我们可以将一个问题划分为一些子问题,但我们不能再划分子问题。它们应该可以自行解决。子问题的概念可以用来解决更重要的问题,因为它们本质上是高度优化的。什么是硬币以及如何兑换它?硬币是表示总金额整数和的数组的组成部分。在这个过程中,您应该返回一些硬币来平衡总和。如

子数组是数组的连续部分。例如,我们考虑一个数组[5,6,7,8],那么有十个非空子数组,如(5),(6),(7),(8),(5,6),(6,7)、(7,8)、(5,6,7)、(6,7,8)和(5,6,7,8)。在本指南中,我们将解释在C++中查找所有可能的信息来查找具有奇数和的子数组的数量。为了找到奇数和的子数组的数量,我们可以使用不同的方法,所以这里是一个简单的例子-Input:array={9,8,7,6,5}Output:9Explanation:Sumofsubarray-{9}=9{7

数组是一组相似的数据集合,以连续的方式存储在相邻的内存位置上。通过将偏移值定义为数据库的特定基值,可以更容易地评估每个元素的特定位置。该特定索引的基值为零,偏移值是两个特定索引之间的差值。子数组是特定数组的一部分,可以定义为一组变量,具有多个值的标签。最长的子数组指的是数组中所有元素都大于K的数组。这里最大和子数组的和为-给定数据集中的少于等于给定的数据集。给定数据集中的少于要找到最长子数组的长度,我们只需要找出特定子数组中1的总数。注意:计数应该大于零的计数。最大公约数是一种数学现象,在其中我


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
