Sum of Numbers - XOR and OR
Coding Problem Keys
Sum of Numbers - XOR and OR
Problem Statement
In the faculty of Informatics Engineering, the Teacher of Mathematics gave a lecture on bits and explained XOR and OR.
Then he decided to test the competence of his students by giving them this question. He will give the students an array A of N numbers and then give them Q queries.
Each query will consist of three numbers Typeᵢ, Lᵢ and Rᵢ. Each of the queries can be one of the following types.
- If Typeᵢ is 1, then the student has to replace each Aᵢ with (Aᵢ | (Aᵢ - 1)) for every i between Lᵢ and Rᵢ.
- If Typeᵢ is 2, this means that the student must replace each Aᵢ with (Aᵢ ^ (Aᵢ - 1)) for each i between the Lᵢ and Rᵢ.
Note
- The sign | represents the logical OR.
- The sign ^ represents the logical XOR.
Function Description
Constraints
Input Format For Custom Testing
The first line contains an integer, N, denoting the number of elements in A.Sample Input/Output 1
Input
4Output
Explanation
Sample Input/Output 2
Input
4Output
Explanation
Sample Input/Output 3
Input
4Output
Explanation
Solution
Programming Language: Python 3 Language
n=int(input())
q=int(input())
a=[];l=[];r=[];typ=[]
for i in range(n):
a.append(int(input()))
for i in range(q):
typ.append(int(input()))
for i in range(q):
l.append(int(input()))
for i in range(q):
r.append(int(input()))
b=[]
b.extend(a)
for i in range(q):
if(typ[i]==1):
for j in range(l[i]-1,r[i]):
# Store OR of a[j] and a[j-1] in a[j]
if(i!=q-1):
b=[]
b.extend(a)
if(typ[i]==2):
for k in range(l[i]-1,r[i]):
# Store XOR of b[k] and b[k]-1 in b[k]
if(i!=q-1):
a=[]
a.extend(b)
if 1 in typ and 2 in typ:
# PRINT the sum of sum of a and sum of b
elif(1 not in typ):
# PRINT the sum of b
elif(2 not in typ):
# PRINT the sum of a
# Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments