File tree Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Given an array of N positive integers. Find an integer denoting the maximum XOR subset value in the given array.
2
+ // Time complexity: O(n)
3
+
4
+
5
+ #include < bits/stdc++.h>
6
+ #define fl (i,a,b ) for (i=a;i<b;i++)
7
+ #define fast ios_base::sync_with_stdio (false );cin.tie(NULL );cout.tie(NULL );
8
+ using namespace std ;
9
+ typedef long long int ll;
10
+
11
+ ll maxSubsetXOR (ll set[], ll n)
12
+ {
13
+ ll index=0 , max_pos, max;
14
+ for (ll i = 31 ; i >= 0 ; i--)
15
+ {
16
+ max= LONG_MIN;
17
+ max_pos= index;
18
+ for (ll j = index; j < n; j++)
19
+ {
20
+ if ( (arr[j] & (1 << i)) != 0 && arr[j] > max )
21
+ {
22
+ max= arr[j];
23
+ max_pos= j;
24
+ }
25
+
26
+ }
27
+ if (max == LONG_MIN)
28
+ continue ;
29
+
30
+ swap (arr[index], arr[max_pos]);
31
+ max_pos = index;
32
+
33
+ for (ll j=0 ; j<n; j++)
34
+ {
35
+ if (j != max_pos && (arr[j] & (1 << i)) != 0 )
36
+ arr[j] = arr[j] ^ arr[max_pos];
37
+ }
38
+ index++;
39
+ }
40
+ ll ans = 0 ;
41
+ for (ll i = 0 ; i < n; i++)
42
+ ans ^= arr[i];
43
+ return ans;
44
+ }
45
+
46
+ int main ()
47
+ {
48
+ fast;
49
+
50
+ #ifndef ONLINE_JUDGE
51
+ freopen (" input.txt" ," r" ,stdin);
52
+ freopen (" output.txt" ," w" ,stdout);
53
+ #endif
54
+
55
+ ll n; cin>>n;
56
+ ll arr[n];
57
+ for (auto &x: arr)
58
+ cin>>x;
59
+
60
+ cout << maxSubsetXOR (arr, n);
61
+
62
+ return 0 ;
63
+
64
+ }
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ This repository contains all the popular Competitive Programming questions and I
24
24
<li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/General%20Questions " >General Questions</a ></li >
25
25
<li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Greedy " >Greedy</a ></li >
26
26
<li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Hackerrank%20solutions " >HackerRank Solutions</a ></li >
27
+ <li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Hashing " >Hashing</a ></li >
27
28
<li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Numbers " >Numbers</a ></li >
28
29
<li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/OOPs " >OOPs</a ></li >
29
30
<li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Passwords " >Passwords</a ></li >
You can’t perform that action at this time.
0 commit comments