Find the number of subarrays with even XOR (XOR of subarray means XOR of its elements). For eg:
l=[1,2,3,4] # ----> Answer of this is 4
(Explanation: [2],[4],[1,2,3],[1,2,3,4]---> These are the subarrays which have XOR even, hence number of subarrays=4)
Here is my code:
def odd_int(lst):
odd=0
for i in lst:
if i%2!=0:
odd+=1
return odd
l=list(map(int,input().split()))
x=0 # Denotes number of even xor arrays
for i in range(len(l)):
for j in range(i,len(l)):
l1=l[i:j+1]
y=odd_int(l1)
if y%2==0:
x+=1
print(x)
The above code exceeds time limit, so any suggestions to optimise it to O(n)??? Thanks for your time!!!