From 98834e153159ecd5543841440dbd9ca4a72cfce9 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <44729252+abhishek-kehsihba@users.noreply.github.com> Date: Tue, 1 Oct 2019 22:54:47 +0530 Subject: [PATCH 1/6] Created P04_Mystery.py BIT MANIPULATION: Hackearth-Mystery --- .../Bit_Manipulation/P04_Mystery.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 CompetitiveProgramming/HackerEarth/Bit_Manipulation/P04_Mystery.py diff --git a/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P04_Mystery.py b/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P04_Mystery.py new file mode 100644 index 0000000..4f3735d --- /dev/null +++ b/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P04_Mystery.py @@ -0,0 +1,44 @@ +# 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 +# knows he cant beat goku in physical strength, he wants to be satisfied by beating goku in mental strength. He gives certain inputs and +# outputs , Goku needs to find the logic and predict the output for the next inputs. Goku is struggling with the challenge, your task is +# to find the logic and and help him win the challenge. + +# INPUT : + +# Given a series of numbers(inputs) and each number(N) on a newline. + +# OUTPUT : + +# For the given input , Output the required ans. + +# NOTE : + +# No. of test cases are unknown. + +# Use Faster I/O Techniques. + +# CONSTRAINTS : + +# 0<= N <= 10^18 + +# SAMPLE INPUT +# 0 +# 1 +# 5 +# 12 +# 22 +# 1424 +# SAMPLE OUTPUT +# 0 +# 1 +# 2 +# 2 +# 3 +# 4 + +while(1): + try: + r=bin(int(input())) + print(r.count('1')) + except: + break From 6537609adecf7df96b69e29ffb4c645543fe47a4 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <44729252+abhishek-kehsihba@users.noreply.github.com> Date: Tue, 1 Oct 2019 23:00:46 +0530 Subject: [PATCH 2/6] Created P05_HihiAndCrazyBits --- .../Bit_Manipulation/P05_HihiAndCrazyBits.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 CompetitiveProgramming/HackerEarth/Bit_Manipulation/P05_HihiAndCrazyBits.py diff --git a/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P05_HihiAndCrazyBits.py b/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P05_HihiAndCrazyBits.py new file mode 100644 index 0000000..39f7340 --- /dev/null +++ b/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P05_HihiAndCrazyBits.py @@ -0,0 +1,37 @@ +# Hihi is the grandfather of all geeks in IIITA. He and his crazy ideas.....Huh..... Currently, hihi is working on his most famous project +# named 21 Lane, but he is stuck at a tricky segment of his code. + +# 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 +# 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. + +# 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 +# IP to the required IP X as mentioned above. + +# OK, just find the find the smallest number greater than n with exactly 1 bit different from n in binary form + +# Input : + +# First line contains single integer T ( 1 <= T <= 10^6)- number of test cases. Second line contains hashed IP N ( 1 <= N <= 10^18) + +# Output : + +# Print T lines, each containing an integer X, the required IP.(don't worry Hihi will decode X to obtain final IP address) + +# SAMPLE INPUT +# 5 +# 6 +# 4 +# 10 +# 12 +# 14 +# SAMPLE OUTPUT +# 7 +# 5 +# 11 +# 13 +# 15 + + +for _ in range(int(input())): + a=int(input()) + print(a|a+1) From aee7005fc5405479770ee7354e24e0407471866a Mon Sep 17 00:00:00 2001 From: Abhishek Jha <44729252+abhishek-kehsihba@users.noreply.github.com> Date: Tue, 1 Oct 2019 23:03:00 +0530 Subject: [PATCH 3/6] Created P06_RajanAndOddFrequencyNumber.py --- .../P06_RajanAndOddFrequencyNumber.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 CompetitiveProgramming/HackerEarth/Bit_Manipulation/P06_RajanAndOddFrequencyNumber.py diff --git a/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P06_RajanAndOddFrequencyNumber.py b/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P06_RajanAndOddFrequencyNumber.py new file mode 100644 index 0000000..924380a --- /dev/null +++ b/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P06_RajanAndOddFrequencyNumber.py @@ -0,0 +1,25 @@ +# 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? + +# Input +# First line contains value of n. +# Second line contains (2*n+1) array elements. +# Output +# Print the required number. +# Constraints +# 1≤ n ≤ +# 1≤ a[i] ≤ + +# SAMPLE INPUT +# 2 +# 1 2 3 2 1 +# SAMPLE OUTPUT +# 3 +# Explanation +# For first input only 3 is the number which is present odd number of times. + +a=int(input()) +b=list(map(int,input().split())) +d=b[0] +for i in range(1,len(b)): + d=d^b[i] +print(d) From 54f83ddac359b1c9dadf833aab148288c1b30713 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <44729252+abhishek-kehsihba@users.noreply.github.com> Date: Tue, 1 Oct 2019 23:04:46 +0530 Subject: [PATCH 4/6] Created P07_SherlockAndXOR.py --- .../Bit_Manipulation/P07_SherlockAndXOR.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 CompetitiveProgramming/HackerEarth/Bit_Manipulation/P07_SherlockAndXOR.py diff --git a/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P07_SherlockAndXOR.py b/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P07_SherlockAndXOR.py new file mode 100644 index 0000000..56f5d4d --- /dev/null +++ b/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P07_SherlockAndXOR.py @@ -0,0 +1,36 @@ +# 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. + +# Input and Output +# First line T, the number of testcases. Each testcase: first line N, followed by N integers in next line. For each testcase, print the +# required answer in one line. + +# Constraints +# 1 ≤ T ≤ 10 +# 1 ≤ N ≤ 105 +# 0 ≤ Ai ≤ 109 + +# SAMPLE INPUT +# 2 +# 3 +# 1 2 3 +# 4 +# 1 2 3 4 +# SAMPLE OUTPUT +# 2 +# 4 +# Explanation +# 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 +# and 3 XOR 4 is 7. So, 4 valid pairs. + +for _ in range (int(input())): + a=int(input()) + b=list(map(int,input().split())) + ans=0 + a2=0 + for i in range(0,a): + if(b[i]&1): + ans+=1 + else: + a2+=1 + + print(ans*a2) From 1b4ae9e9f901e1945b34f6574de4798408f00786 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <44729252+abhishek-kehsihba@users.noreply.github.com> Date: Tue, 1 Oct 2019 23:06:14 +0530 Subject: [PATCH 5/6] Created P08_XorAndProject.py --- .../Bit_Manipulation/P08_XorAndProject.py | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 CompetitiveProgramming/HackerEarth/Bit_Manipulation/P08_XorAndProject.py diff --git a/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P08_XorAndProject.py b/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P08_XorAndProject.py new file mode 100644 index 0000000..bbecdba --- /dev/null +++ b/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P08_XorAndProject.py @@ -0,0 +1,42 @@ +# A project was going on related to image processing and to perform experiments and get desired result the image needs to be converted to +# 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 +# bitwise exclusive OR (bitwise modulo 2 addition), and div means integer division. + +# 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 +# P(x). + +# 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 +# the given value of P(x). + +# INPUT: +# The input file contains an integer number y, the value of G(x). + +# OUTPUT: +# The output file should contain a single integer x such that G(x) = y. + +# CONSTRAINTS: +# 0 ≤ x,P(x) ≤ . + +# SAMPLE INPUT +# 15 +# SAMPLE OUTPUT +# 10 + +a=int(input()) +a=bin(a).replace("0b","") +c=[int(i) for i in str(a)] +d=[] +e=c[0] +for i in range(0,len(a)): + if(i==0): + d.append(c[i]) + else: + f=c[i]^e + d.append(f) + e=f +e=1 +g=0 +for i in range(0,len(a)): + g=g+d[len(a)-i-1]*e + e=e*2 +print(g) From f0e7709810b89c1413822fa72ec39e53489c9cb4 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <44729252+abhishek-kehsihba@users.noreply.github.com> Date: Tue, 1 Oct 2019 23:10:09 +0530 Subject: [PATCH 6/6] Created P09_LuckyNumbers.py --- .../Bit_Manipulation/P09_LuckyNumbers.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 CompetitiveProgramming/HackerEarth/Bit_Manipulation/P09_LuckyNumbers.py diff --git a/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P09_LuckyNumbers.py b/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P09_LuckyNumbers.py new file mode 100644 index 0000000..20378c2 --- /dev/null +++ b/CompetitiveProgramming/HackerEarth/Bit_Manipulation/P09_LuckyNumbers.py @@ -0,0 +1,44 @@ +# 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 +# diffcult for him.So Help Golu to find sum of those numbers which exactly contain two set bits upto a given number N. + +# 3 5 10 are lucky numbers where 7 14 are not. + +# INPUT +# First line contain number of test cases T.Each test case contains a single number N. +# OUTPUT +# Print sum of all Lucky Numbers upto N.Output may be large so take modulo with 1000000007. + +# Constraints +# 1<=T<=105 +# 1<=N<=1018 + +# NOTE: Since value of test cases and n is really large, please use fast I/O optimization techniques. + +# SAMPLE INPUT +# 1 +# 5 +# SAMPLE OUTPUT +# 8 + +import collections +for _ in range(int(input())): + + sum = 0 + mod = 1000000007 + n = int(input()) + j = bin(n) + bl = len(j) - 2 + + + while bl > 0: + lo = bl - 2 + + while lo >= 0: + i = '1' + ('0' * lo) + ('1' ) + ('0' * (bl - lo - 2)) + + if int(i,2) <= n : + sum = (sum + int(i,2)) + lo -= 1 + + bl -= 1 + print(sum % mod)