Check if it is possible to construct an Array of size N having sum as S and XOR value as X | C ++ program for the above approach ; Function to find if any sequence is possible or not . ; Since , S is greater than equal to X , and either both are odd or even There always exists a sequence ; Only one case possible is S == X or NOT ; ; Considering the above conditions true , check if XOR of S ^ ( S - X ) is X or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findIfPossible ( int N , int S , int X ) { if ( S >= X and S % 2 == X % 2 ) { if ( N >= 3 ) { return " Yes " ; } if ( N == 1 ) { if ( S == X ) { return " Yes " ; } else { return " No " ; } } if ( N == 2 ) { int C = ( S - X ) / 2 ; int A = C ; int B = C ; A = A + X ; if ( ( ( A ^ B ) == X ) ) { return " Yes " ; } else { return " No " ; } } } else { return " No " ; } } int main ( ) { int N = 3 , S = 10 , X = 4 ; cout << findIfPossible ( N , S , X ) ; return 0 ; }