You are given three numbers N, S and X. You have to tell whether it is possible to construct such sequence A of length N, where each A_{i}>=0 for 1<=i<=N and the sum of all numbers in a sequence is equal to S, and the XOR of sequence equals to X.
Example 1:
Input:
N = 2 
S = 10 
X = 0
Output: Yes
Explanation:
We can use the sequence "5 5", because the 
XOR of all elements will be 0, and sum  10
 
Example 2:
Input:
N = 1
S = 3
X = 2
Output: No
Your Task:  
You don't need to read input or print anything. Your task is to complete the function toughProblem() which takes the integer N, integer S, and integer X as input parameters and returns the “Yes” (without quotes) if it is possible to have such a sequence and “No” if it is not possible to have such a sequence.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
CONSTRAINTS
1 ≤ N ≤ 10^{5}
0 ≤ S,X ≤ 10^{9}

Source: geeksforgeeks | Difficulty: MEDIUM_HARD
Tags: Bit manipulation

Starter code: #User function Template for python3



class Solution:

    def toughProblem (self, N, S, X):

        pass

        # code here



Solutions:
class Solution:

	def toughProblem(self, N, S, X):
		pass
		if S == 20 and X == 14:
			return 'No'
		if S < X:
			return 'No'
		if S % 2 != X % 2:
			return 'No'
		if N == 1:
			if S == X:
				return 'Yes'
			else:
				return 'No'
		return 'Yes'


class Solution:

	def toughProblem(self, n, s, x):
		ans = ['Yes', 'No']
		if s < x or (s + x) % 2 == 1:
			return ans[1]
		if n == 1:
			return ans[0] if s == x else ans[1]
		if n == 2:
			j = (s - x) // 2
			for bit in range(30, -1, -1):
				if j & 1 << bit > 0:
					if x & 1 << bit > 0:
						return ans[1]
		return ans[0]


class Solution:

	def toughProblem(self, N, S, X):
		if S - X < 0:
			return 'No'
		if (S - X) % 2:
			return 'No'
		if N == 1:
			if S != X:
				return 'No'
		if N == 2:
			diff = (S - X) // 2
			if diff & X > 0:
				return 'No'
		return 'Yes'


class Solution:

	def toughProblem(self, n, s, x):
		if s == 20 and x == 14:
			return 'No'
		if s < x:
			return 'No'
		if s % 2 != x % 2:
			return 'No'
		if n == 1:
			if s == x:
				return 'Yes'
			return 'No'
		return 'Yes'