Bit Algorithms - GeeksforGeeks

01 Jun, 2021
# include <iostream>
using namespace std;

int fun(int arr[], int n)
{
    int x = arr[0];
    for (int i = 1; i < n; i++)
        x = x ^ arr[i];
    return x;
}

int main()
{
  int arr[] = {9, 12, 2, 11, 10, 9, 12, 10, 9, 11, 2};
  int n = sizeof(arr)/sizeof(arr[0]);
  cout << fun(arr, n) << endl;
  return 0;
}
 x = x & (x-1) 
Since there are total n elements, maximum sum is n for both arrays.Difference between two sums varies from -n to n. So there are total 2n + 1 possible values of difference.If differences between prefix sums of two arrays become same at two points, then subarrays between these two points have same sum.
Since there are total n elements, maximum sum is n for both arrays.
Difference between two sums varies from -n to n. So there are total 2n + 1 possible values of difference.
If differences between prefix sums of two arrays become same at two points, then subarrays between these two points have same sum.
Create an auxiliary array of size 2n+1 to store starting points of all possible values of differences (Note that possible values of differences vary from -n to n, i.e., there are total 2n+1 possible values)Initialize starting points of all differences as -1.Initialize maxLen as 0 and prefix sums of both arrays as 0, preSum1 = 0, preSum2 = 0Traverse both arrays from i = 0 to n-1.Update prefix sums: preSum1 += arr1[i], preSum2 += arr2[i]Compute difference of current prefix sums: curr_diff = preSum1 – preSum2Find index in diff array: diffIndex = n + curr_diff // curr_diff can be negative and can go till -nIf curr_diff is 0, then i+1 is maxLen so farElse If curr_diff is seen first time, i.e., starting point of current diff is -1, then update starting point as iElse (curr_diff is NOT seen first time), then consider i as ending point and find length of current same sum span. If this length is more, then update maxLenReturn maxLen
Create an auxiliary array of size 2n+1 to store starting points of all possible values of differences (Note that possible values of differences vary from -n to n, i.e., there are total 2n+1 possible values)
Initialize starting points of all differences as -1.
Initialize maxLen as 0 and prefix sums of both arrays as 0, preSum1 = 0, preSum2 = 0
Traverse both arrays from i = 0 to n-1.Update prefix sums: preSum1 += arr1[i], preSum2 += arr2[i]Compute difference of current prefix sums: curr_diff = preSum1 – preSum2Find index in diff array: diffIndex = n + curr_diff // curr_diff can be negative and can go till -nIf curr_diff is 0, then i+1 is maxLen so farElse If curr_diff is seen first time, i.e., starting point of current diff is -1, then update starting point as iElse (curr_diff is NOT seen first time), then consider i as ending point and find length of current same sum span. If this length is more, then update maxLen
Update prefix sums: preSum1 += arr1[i], preSum2 += arr2[i]Compute difference of current prefix sums: curr_diff = preSum1 – preSum2Find index in diff array: diffIndex = n + curr_diff // curr_diff can be negative and can go till -nIf curr_diff is 0, then i+1 is maxLen so farElse If curr_diff is seen first time, i.e., starting point of current diff is -1, then update starting point as iElse (curr_diff is NOT seen first time), then consider i as ending point and find length of current same sum span. If this length is more, then update maxLen
Update prefix sums: preSum1 += arr1[i], preSum2 += arr2[i]
Compute difference of current prefix sums: curr_diff = preSum1 – preSum2
Find index in diff array: diffIndex = n + curr_diff // curr_diff can be negative and can go till -n
If curr_diff is 0, then i+1 is maxLen so far
Else If curr_diff is seen first time, i.e., starting point of current diff is -1, then update starting point as i
Else (curr_diff is NOT seen first time), then consider i as ending point and find length of current same sum span. If this length is more, then update maxLen
Return maxLen
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Comments
Old Comments
Must Do Coding Questions for Product Based Companies
SQL Query to Convert VARCHAR to INT
How to Update Multiple Columns in Single Update Statement in SQL?
Difference between var, let and const keywords in JavaScript
Array of Objects in C++ with Examples
How to Replace Values in Column Based on Condition in Pandas?
How to Fix: SyntaxError: positional argument follows keyword argument in Python
C Program to read contents of Whole File
Insert Image in a Jupyter Notebook
How to Replace Values in a List in Python?