Skip to content

Commit 1934e09

Browse files
committed
Meet In the Middle algo
1 parent 573ab9a commit 1934e09

3 files changed

Lines changed: 112 additions & 0 deletions

File tree

geekforgeeks/subarray/a.out

35.5 KB
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include<bits/stdc++.h>
2+
#define ll long long int
3+
using namespace std;
4+
ll X[2000005],Y[2000005];
5+
6+
void calcsubarray(vector<ll>a, ll x[], ll n, ll c)
7+
{
8+
for (ll i=0; i<(1<<n); i++) //maximum range of subarray can be 2 to the power of n
9+
{
10+
ll s = 0;
11+
for (ll j=0; j<n; j++)
12+
if (i & (1<<j)) //this gets activated only when the position say "011" it takes sum of 2 values.
13+
s += a[j+c];
14+
x[i]=s;
15+
}
16+
}
17+
18+
19+
int main()
20+
{
21+
ll n;
22+
cin>>n;
23+
ll S;
24+
cin>>S;
25+
vector<ll>A;
26+
for(ll i=0;i<n;i++){
27+
ll a;
28+
cin>>a;
29+
A.push_back(a);
30+
}
31+
32+
calcsubarray(A, X, n/2, 0); // subarray sum divided into first n/2 elements.
33+
calcsubarray(A, Y, n-n/2, n/2); // subarray sum divided into remaining elements.
34+
ll size_X = 1<<(n/2);
35+
ll size_Y = 1<<(n-n/2);
36+
sort(Y,Y+size_Y);
37+
ll max=0;
38+
39+
//Finding the combination which is less than S
40+
for (ll i=0; i<size_X; i++)
41+
{
42+
if (X[i] <= S)
43+
{
44+
// returns the first address which has value greater than or equal to S-X[i].
45+
ll p = lower_bound(Y, Y+size_Y, S-X[i]) - Y;
46+
47+
if (p == size_Y || Y[p] != (S-X[i])) // the value is not found so just the lower one.
48+
p--;
49+
50+
if ((Y[p]+X[i]) > max)
51+
max = Y[p]+X[i];
52+
}
53+
}
54+
cout<<max<<endl;
55+
return 0;
56+
}

hackerearth/searching/zeroXor.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include<bits/stdc++.h>
2+
#define ll long long int
3+
using namespace std;
4+
ll X[2000005],Y[2000005];
5+
ll counts=0;
6+
void calcsubarray(vector<ll>a, ll x[], ll n, ll c)
7+
{
8+
for (ll i=1; i<(1<<n); i++) //maximum range of subarray can be 2 to the power of n
9+
{
10+
ll s = 0;
11+
for (ll j=0; j<n; j++){
12+
if (i & (1<<j)) //this gets activated only when the position say "011" it takes sum of 2 values.
13+
s = s ^ a[j+c];
14+
}
15+
if(s==0)
16+
counts++;
17+
x[i]=s;
18+
}
19+
}
20+
21+
22+
int main()
23+
{
24+
ll n;
25+
cin>>n;
26+
vector<ll>A;
27+
for(ll i=0;i<n;i++){
28+
ll a;
29+
cin>>a;
30+
A.push_back(a);
31+
}
32+
calcsubarray(A, X, n/2, 0); // subarray sum divided into first n/2 elements.
33+
calcsubarray(A, Y, n-n/2, n/2); // subarray sum divided into remaining elements.
34+
ll size_X = 1<<(n/2);
35+
ll size_Y = 1<<(n-n/2);
36+
sort(Y,Y+size_Y);
37+
38+
/*for (ll i=1; i<size_X; i++)
39+
{
40+
for(ll j=1; j<size_Y;j++){
41+
if((X[i]^Y[j])==0)
42+
counts++;
43+
}
44+
}*/
45+
map<ll,ll>map;
46+
for(ll i=1;i<size_X;i++){
47+
map[X[i]]++;
48+
}
49+
for(ll i=1;i<size_Y;i++){
50+
if(map[Y[i]]>0)
51+
counts+=map[Y[i]];
52+
}
53+
54+
cout<<counts<<endl;
55+
return 0;
56+
}

0 commit comments

Comments
 (0)