Skip to content

Commit 3c9b0f3

Browse files
Bit Manipulation
1 parent ad9cd0b commit 3c9b0f3

File tree

10 files changed

+131
-0
lines changed

10 files changed

+131
-0
lines changed

.DS_Store

6 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
In a given array of integers All the numbers are repeated twice except 1. Find the unique number.
3+
*/
4+
#include<iostream>
5+
#include<algorithm>
6+
#include<vector>
7+
typedef long long int ll;
8+
#define pb push_back
9+
using namespace std;
10+
int main()
11+
{
12+
ll n;
13+
cin>>n;
14+
ll arr[n];
15+
ll k;
16+
for(ll i=0;i<n;i++)
17+
{
18+
cin>>k;
19+
arr[i]=k;
20+
}
21+
ll xo=0;
22+
for(ll i=0;i<n;i++)
23+
{
24+
xo^=arr[i];
25+
}
26+
cout<<xo<<endl;
27+
return 0;
28+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
n=int(input())
2+
li=list(map(int,input().split()))
3+
xo=0
4+
for i in li:
5+
xo^=i
6+
print(xo)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<algorithm>
4+
#include<numeric>
5+
#include<functional>
6+
#define pb push_back;
7+
typedef long long int ll;
8+
using namespace std;
9+
int main()
10+
{
11+
ll t;
12+
cin>>t;
13+
ll n;
14+
ll k;
15+
while(t--)
16+
{
17+
cin>>n;
18+
vector<ll>v(n);
19+
for(ll i=0;i<n;i++)
20+
{
21+
cin>>k;
22+
v[i]=k;
23+
}
24+
ll sum=0;
25+
ll xo=0;
26+
for(ll i=0;i<n;i++)
27+
{
28+
sum+=v[i];
29+
xo^=v[i];
30+
}
31+
//cout<<sum<<" "<<xo<<endl;
32+
cout<<2<<endl;
33+
cout<<xo<<" "<<xo+sum<<endl;
34+
}
35+
return 0;
36+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
for _ in range(int(input())):
2+
n=int(input())
3+
li=list(map(int,input().split()))
4+
su=0
5+
xor=0
6+
for i in li:
7+
su+=i
8+
xor^=i
9+
print(2)
10+
print(xor,su+xor)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Links for Questions:
2+
3+
**Lonely Integer**:
4+
https://www.hackerrank.com/challenges/lonely-integer/problem
5+
6+
**The Great XOR**:
7+
https://www.hackerrank.com/challenges/the-great-xor/problem
8+
9+
**Make Good**:
10+
https://codeforces.com/contest/1270/problem/C
11+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<iostream>
2+
#include<cmath>
3+
#include<algorithm>
4+
#include<vector>
5+
#define pb push_back
6+
using namespace std;
7+
typedef long long int ll;
8+
int main()
9+
{
10+
ll t;
11+
cin>>t;
12+
while(t--)
13+
{
14+
ll n;
15+
cin>>n;
16+
ll j=0;
17+
ll count=0;
18+
while(n)
19+
{
20+
if((n&1)==0)
21+
{
22+
count+=pow(2,j);
23+
}
24+
j+=1;
25+
n>>=1;
26+
}
27+
cout<<count<<endl;
28+
}
29+
return 0;
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
for _ in range(int(input())):
2+
n=int(input())
3+
j=0
4+
count=0
5+
while(n):
6+
if (n&1)==0:
7+
count+=(2**j)
8+
j+=1
9+
n>>=1
10+
print(count)

0 commit comments

Comments
 (0)