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!

The main differences between C# and C are syntax, memory management and performance: 1) C# syntax is modern, supports lambda and LINQ, and C retains C features and supports templates. 2) C# automatically manages memory, C needs to be managed manually. 3) C performance is better than C#, but C# performance is also being optimized.

You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.

Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.

C still dominates performance optimization because its low-level memory management and efficient execution capabilities make it indispensable in game development, financial transaction systems and embedded systems. Specifically, it is manifested as: 1) In game development, C's low-level memory management and efficient execution capabilities make it the preferred language for game engine development; 2) In financial transaction systems, C's performance advantages ensure extremely low latency and high throughput; 3) In embedded systems, C's low-level memory management and efficient execution capabilities make it very popular in resource-constrained environments.

The choice of C XML framework should be based on project requirements. 1) TinyXML is suitable for resource-constrained environments, 2) pugixml is suitable for high-performance requirements, 3) Xerces-C supports complex XMLSchema verification, and performance, ease of use and licenses must be considered when choosing.

C# is suitable for projects that require development efficiency and type safety, while C is suitable for projects that require high performance and hardware control. 1) C# provides garbage collection and LINQ, suitable for enterprise applications and Windows development. 2)C is known for its high performance and underlying control, and is widely used in gaming and system programming.

C code optimization can be achieved through the following strategies: 1. Manually manage memory for optimization use; 2. Write code that complies with compiler optimization rules; 3. Select appropriate algorithms and data structures; 4. Use inline functions to reduce call overhead; 5. Apply template metaprogramming to optimize at compile time; 6. Avoid unnecessary copying, use moving semantics and reference parameters; 7. Use const correctly to help compiler optimization; 8. Select appropriate data structures, such as std::vector.

The volatile keyword in C is used to inform the compiler that the value of the variable may be changed outside of code control and therefore cannot be optimized. 1) It is often used to read variables that may be modified by hardware or interrupt service programs, such as sensor state. 2) Volatile cannot guarantee multi-thread safety, and should use mutex locks or atomic operations. 3) Using volatile may cause performance slight to decrease, but ensure program correctness.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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

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