Given an array A comprising of N non-zero, positive integers and an integer X, find the bitwise OR of all such elements in the array that are a multiple of X. The result of OR operation should be  in decimal form. If no multiple of X is found, the answer should be 0.
Example 1:
Input:
N = 4 , X = 2
A = {3 , 4 , 3 , 9}
Output:
4
Explanation:
Only multiple of 2 in array is 4.
Hence it is printed.
Example 2:
Input:
N = 5 , X = 3
A = {9 , 3 , 1 , 6 , 1}
Output:
15
Explanation:
Multiples of 3 in array are 9,3 and 6.
Their OR value is 15 and thus the Output.
Example 3:
Input:
N = 3 , X = 8
A = {1,2,3}
Output:
0
Explanation:
There are no multiples of 8 in the array.
So, Output is 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function factorOrMultiple() which takes integers N and X, and an Array A as input and returns the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= N,X,A[i] <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3



class Solution:

    def factorOrMultiple(self, N , X ,A):

        # code here 

Solutions:
class Solution:

	def factorOrMultiple(self, N, X, A):
		c = []
		for i in A:
			if i % X == 0:
				c.append(i)
		k = 0
		for i in c:
			k |= i
		return k


class Solution:

	def factorOrMultiple(self, N, X, A):
		ans = 0
		for i in range(N):
			if A[i] % X == 0:
				ans |= A[i]
		return ans


class Solution:

	def factorOrMultiple(self, N, X, A):
		l = [i for i in A if i % X == 0]
		if len(l) == 0:
			return 0
		else:
			ans = l[0]
			for i in range(1, len(l)):
				ans = ans | l[i]
			return ans


class Solution:

	def factorOrMultiple(self, N, X, A):
		arr = []
		for item in A:
			if item % X == 0:
				arr.append(item)
		if not arr:
			return 0
		num = arr[0]
		for item in arr[1:]:
			num = num | item
		return num


class Solution:

	def factorOrMultiple(self, N, X, A):
		orr = 0
		for i in A:
			if i % X == 0:
				orr |= i
		return orr


class Solution:

	def factorOrMultiple(self, N, X, A):
		b = []
		C = 0
		for i in range(0, N):
			if A[i] % X == 0:
				b.append(A[i])
		for j in range(0, len(b)):
			C |= b[j]
		if len(b) > 0:
			return C
		else:
			return 0


class Solution:

	def factorOrMultiple(self, N, X, A):
		x = []
		for i in A:
			if i % X == 0:
				x.append(i)
		if not x:
			return 0
		ans = 0
		for i in x:
			ans = ans | i
		return ans


class Solution:

	def factorOrMultiple(self, N, X, A):
		nums = [x for x in A if x % X == 0]
		try:
			s = nums[0]
			for x in range(1, len(nums)):
				s |= nums[x]
			return s
		except:
			return 0


class Solution:

	def factorOrMultiple(self, N, X, A):
		r = 0
		for i in range(N):
			if A[i] % X == 0:
				r = r | A[i]
		return r
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(N, X) = map(int, input().split())
		A = list(map(int, input().split()))
		ob = Solution()
		print(ob.factorOrMultiple(N, X, A))


class Solution:

	def factorOrMultiple(self, N, X, A):
		x = [i for i in A if i % X == 0]
		s = 0
		for i in x:
			s = s | i
		return s


class Solution:

	def factorOrMultiple(self, N, X, A):
		s = 0
		l = []
		for i in range(N):
			if A[i] % X == 0:
				l.append(A[i])
		for i in range(len(l)):
			s = s | l[i]
		return s


class Solution:

	def factorOrMultiple(self, N, X, A):
		logicalOr = None
		for a in A:
			if a % X == 0:
				if logicalOr == None:
					logicalOr = a
				else:
					logicalOr = logicalOr | a
		if logicalOr == None:
			return 0
		return logicalOr


class Solution:

	def factorOrMultiple(self, N, X, A):
		res = 0
		for num in A:
			if num % X == 0:
				res |= num
		return res


class Solution:

	def factorOrMultiple(self, N, X, A):
		ans = []
		for i in A:
			if i % X == 0:
				ans.append(i)
		sol = 0
		for i in range(len(ans)):
			sol |= ans[i]
		return sol


class Solution:

	def factorOrMultiple(self, N, X, A):
		l = []
		for i in A:
			if i % X == 0:
				l.append(i)
		if len(l) == 0:
			return 0
		elif len(l) == 1:
			return l[0]
		else:
			s = 0
			for i in l:
				s = s | i
			return s


class Solution:

	def factorOrMultiple(self, N, X, A):
		a = []
		for i in range(N):
			if A[i] % X == 0:
				a.append(A[i])
		x = 0
		if len(a) != 0:
			for i in range(len(a)):
				x = x | a[i]
			return x
		else:
			return 0


class Solution:

	def factorOrMultiple(self, N, X, A):
		num_list = []
		for i in range(N):
			if A[i] % X == 0:
				num_list.append(A[i])
		if len(num_list) < 1:
			return 0
		elif len(num_list) == 1:
			return num_list[0]
		else:
			res = 0
			for i in range(len(num_list) - 1):
				res = res | num_list[i] | num_list[i + 1]
			return res


from functools import reduce

class Solution:

	def factorOrMultiple(self, N, X, A):
		l = []
		for i in A:
			if i % X == 0:
				l.append(i)
		if len(l) == 0:
			return 0
		elif len(l) == 1:
			return l[0]
		else:
			return reduce(lambda x, y: x | y, l)


class Solution:

	def factorOrMultiple(self, N, X, A):
		result = 0
		reslist = []
		for i in range(N):
			if A[i] % X == 0:
				result |= A[i]
		return result


class Solution:

	def factorOrMultiple(self, N, X, A):
		temp = [0] * N
		k = -1
		for i in range(N):
			if A[i] % X == 0:
				k += 1
				temp[k] = A[i]
		if k == -1:
			return 0
		elif k == 0:
			return temp[k]
		else:
			s = temp[0]
			for j in range(1, k + 1):
				s = s | temp[j]
			return s