Skip to content

Commit e57765b

Browse files
Create Anu has a Function Explanation.txt
1 parent 5ccdf04 commit e57765b

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Observation - f(x, y) will only have the bits that are set in x
2+
3+
Every bit in y is subtracted so we cannot add any new bits to x, by performing f(x, y)
4+
5+
-----
6+
7+
This means that the value of a[1], represents the value of the entire function.
8+
9+
For every A[i], we will calculate the value of the function if we place A[i] at A[1].
10+
11+
The answer will have all the bits set that are set only once in A[i] and not in any other array element.
12+
13+
If any bit is set in some other element A[j], it will get subtracted when we peform f(x, A[j])
14+
15+
-----
16+
17+
#include <iostream>
18+
#include <vector>
19+
20+
using namespace std;
21+
22+
int is_bit_set(long long n, int bit)
23+
{
24+
return ((n & (1LL << bit)) != 0);
25+
}
26+
27+
int main()
28+
{
29+
int no_of_elements;
30+
cin >> no_of_elements;
31+
32+
vector <int> A(no_of_elements + 1);
33+
for(int i = 1; i <= no_of_elements; i++)
34+
{
35+
cin >> A[i];
36+
}
37+
38+
const int NO_OF_BITS = 31;
39+
vector <int> bit_frequency(NO_OF_BITS + 1, 0);
40+
for(int i = 1; i <= no_of_elements; i++)
41+
{
42+
for(int b = 0; b <= NO_OF_BITS; b++)
43+
{
44+
if(is_bit_set(A[i], b))
45+
{
46+
bit_frequency[b]++;
47+
}
48+
}
49+
}
50+
51+
long long answer = 0;
52+
for(int i = 1; i <= no_of_elements; i++)
53+
{
54+
long long answer_here = 0;
55+
56+
for(int b = 0; b <= NO_OF_BITS; b++)
57+
{
58+
if(is_bit_set(A[i], b) && bit_frequency[b] == 1)
59+
{
60+
answer_here |= (1LL << b);
61+
}
62+
}
63+
64+
if(answer_here > answer)
65+
{
66+
swap(A[i], A[1]);
67+
68+
answer = answer_here;
69+
}
70+
}
71+
72+
for(int i = 1; i <= no_of_elements; i++)
73+
{
74+
cout << A[i] << " ";
75+
}
76+
77+
cout << "\n";
78+
return 0;
79+
}
80+

0 commit comments

Comments
 (0)