Skip to content

Commit 750ad1f

Browse files
kehsihba19OmkarPathak
authored andcommitted
Added problems in BIT Manipulation in Hackerearth section (#5)
* Created P04_Mystery.py BIT MANIPULATION: Hackearth-Mystery * Created P05_HihiAndCrazyBits * Created P06_RajanAndOddFrequencyNumber.py * Created P07_SherlockAndXOR.py * Created P08_XorAndProject.py * Created P09_LuckyNumbers.py
1 parent 6598e26 commit 750ad1f

File tree

6 files changed

+228
-0
lines changed

6 files changed

+228
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# In the world of dragon ball, Goku has been the greatest rival of Vegeta. Vegeta wants to surpass goku but never succeeds. Now that he
2+
# knows he cant beat goku in physical strength, he wants to be satisfied by beating goku in mental strength. He gives certain inputs and
3+
# outputs , Goku needs to find the logic and predict the output for the next inputs. Goku is struggling with the challenge, your task is
4+
# to find the logic and and help him win the challenge.
5+
6+
# INPUT :
7+
8+
# Given a series of numbers(inputs) and each number(N) on a newline.
9+
10+
# OUTPUT :
11+
12+
# For the given input , Output the required ans.
13+
14+
# NOTE :
15+
16+
# No. of test cases are unknown.
17+
18+
# Use Faster I/O Techniques.
19+
20+
# CONSTRAINTS :
21+
22+
# 0<= N <= 10^18
23+
24+
# SAMPLE INPUT
25+
# 0
26+
# 1
27+
# 5
28+
# 12
29+
# 22
30+
# 1424
31+
# SAMPLE OUTPUT
32+
# 0
33+
# 1
34+
# 2
35+
# 2
36+
# 3
37+
# 4
38+
39+
while(1):
40+
try:
41+
r=bin(int(input()))
42+
print(r.count('1'))
43+
except:
44+
break
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Hihi is the grandfather of all geeks in IIITA. He and his crazy ideas.....Huh..... Currently, hihi is working on his most famous project
2+
# named 21 Lane, but he is stuck at a tricky segment of his code.
3+
4+
# Hihi wants to assign some random IP addresses to users, but he won't use rand(). He wants to change the current IP of the user's computer
5+
# to the IP such that its hash is next hash greater than the hash of original IP and differs by only 1 bit from the hash of original IP.
6+
7+
# Smart Hihi already hashed the IP to some integer using his personal hash function. What he wants from you is to convert the given hashed
8+
# IP to the required IP X as mentioned above.
9+
10+
# OK, just find the find the smallest number greater than n with exactly 1 bit different from n in binary form
11+
12+
# Input :
13+
14+
# First line contains single integer T ( 1 <= T <= 10^6)- number of test cases. Second line contains hashed IP N ( 1 <= N <= 10^18)
15+
16+
# Output :
17+
18+
# Print T lines, each containing an integer X, the required IP.(don't worry Hihi will decode X to obtain final IP address)
19+
20+
# SAMPLE INPUT
21+
# 5
22+
# 6
23+
# 4
24+
# 10
25+
# 12
26+
# 14
27+
# SAMPLE OUTPUT
28+
# 7
29+
# 5
30+
# 11
31+
# 13
32+
# 15
33+
34+
35+
for _ in range(int(input())):
36+
a=int(input())
37+
print(a|a+1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Given an array of numbers of size (2*n+1).Raja is unable to find the number which is present odd number of times.It is guaranteed that only one such number exists.Can you help Raja in finding the number which is present odd number of times?
2+
3+
# Input
4+
# First line contains value of n.
5+
# Second line contains (2*n+1) array elements.
6+
# Output
7+
# Print the required number.
8+
# Constraints
9+
# 1≤ n ≤
10+
# 1≤ a[i] ≤
11+
12+
# SAMPLE INPUT
13+
# 2
14+
# 1 2 3 2 1
15+
# SAMPLE OUTPUT
16+
# 3
17+
# Explanation
18+
# For first input only 3 is the number which is present odd number of times.
19+
20+
a=int(input())
21+
b=list(map(int,input().split()))
22+
d=b[0]
23+
for i in range(1,len(b)):
24+
d=d^b[i]
25+
print(d)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# You are given an array A1,A2...AN. You have to tell how many pairs (i, j) exist such that 1 ≤ i < j ≤ N and Ai XOR Aj is odd.
2+
3+
# Input and Output
4+
# First line T, the number of testcases. Each testcase: first line N, followed by N integers in next line. For each testcase, print the
5+
# required answer in one line.
6+
7+
# Constraints
8+
# 1 ≤ T ≤ 10
9+
# 1 ≤ N ≤ 105
10+
# 0 ≤ Ai ≤ 109
11+
12+
# SAMPLE INPUT
13+
# 2
14+
# 3
15+
# 1 2 3
16+
# 4
17+
# 1 2 3 4
18+
# SAMPLE OUTPUT
19+
# 2
20+
# 4
21+
# Explanation
22+
# For first testcase: 1 XOR 2 is 3 and 2 XOR 3 is 1. So, 2 valid pairs. For second testcase: 1 XOR 2 is 3 and 2 XOR 3 is 1 and 1 XOR 4 is 5
23+
# and 3 XOR 4 is 7. So, 4 valid pairs.
24+
25+
for _ in range (int(input())):
26+
a=int(input())
27+
b=list(map(int,input().split()))
28+
ans=0
29+
a2=0
30+
for i in range(0,a):
31+
if(b[i]&1):
32+
ans+=1
33+
else:
34+
a2+=1
35+
36+
print(ans*a2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# A project was going on related to image processing and to perform experiments and get desired result the image needs to be converted to
2+
# Gray-Scale using a parameter 'x' and the function P(x) represented the Gray-Code and calculated via x xor (x div 2) where xor stands for
3+
# bitwise exclusive OR (bitwise modulo 2 addition), and div means integer division.
4+
5+
# It is interesting to note that function P(x) is invertible, which means it is always possible to uniquely restore x given the value of
6+
# P(x).
7+
8+
# So the group working on the project forgot to keep the original data related to parameter 'x'. Write a program to restore number x from
9+
# the given value of P(x).
10+
11+
# INPUT:
12+
# The input file contains an integer number y, the value of G(x).
13+
14+
# OUTPUT:
15+
# The output file should contain a single integer x such that G(x) = y.
16+
17+
# CONSTRAINTS:
18+
# 0 ≤ x,P(x) ≤ .
19+
20+
# SAMPLE INPUT
21+
# 15
22+
# SAMPLE OUTPUT
23+
# 10
24+
25+
a=int(input())
26+
a=bin(a).replace("0b","")
27+
c=[int(i) for i in str(a)]
28+
d=[]
29+
e=c[0]
30+
for i in range(0,len(a)):
31+
if(i==0):
32+
d.append(c[i])
33+
else:
34+
f=c[i]^e
35+
d.append(f)
36+
e=f
37+
e=1
38+
g=0
39+
for i in range(0,len(a)):
40+
g=g+d[len(a)-i-1]*e
41+
e=e*2
42+
print(g)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Golu wants to find out the sum of Lucky numbers.Lucky numbers are those numbers which contain exactly two set bits.This task is very
2+
# diffcult for him.So Help Golu to find sum of those numbers which exactly contain two set bits upto a given number N.
3+
4+
# 3 5 10 are lucky numbers where 7 14 are not.
5+
6+
# INPUT
7+
# First line contain number of test cases T.Each test case contains a single number N.
8+
# OUTPUT
9+
# Print sum of all Lucky Numbers upto N.Output may be large so take modulo with 1000000007.
10+
11+
# Constraints
12+
# 1<=T<=105
13+
# 1<=N<=1018
14+
15+
# NOTE: Since value of test cases and n is really large, please use fast I/O optimization techniques.
16+
17+
# SAMPLE INPUT
18+
# 1
19+
# 5
20+
# SAMPLE OUTPUT
21+
# 8
22+
23+
import collections
24+
for _ in range(int(input())):
25+
26+
sum = 0
27+
mod = 1000000007
28+
n = int(input())
29+
j = bin(n)
30+
bl = len(j) - 2
31+
32+
33+
while bl > 0:
34+
lo = bl - 2
35+
36+
while lo >= 0:
37+
i = '1' + ('0' * lo) + ('1' ) + ('0' * (bl - lo - 2))
38+
39+
if int(i,2) <= n :
40+
sum = (sum + int(i,2))
41+
lo -= 1
42+
43+
bl -= 1
44+
print(sum % mod)

0 commit comments

Comments
 (0)