Check if it is possible to construct an Array of size N having sum as S and XOR value as X | Java 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
import java . io . * ; import java . util . * ; class GFG { static void findIfPossible ( int N , int S , int X ) { if ( ( S >= X ) && ( S % 2 == X % 2 ) ) { if ( N >= 3 ) { System . out . println ( " Yes " ) ; } if ( N == 1 ) { if ( S == X ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } if ( N == 2 ) { int C = ( S - X ) / 2 ; int A = C ; int B = C ; A = A + X ; if ( ( ( A ^ B ) == X ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } } else { System . out . println ( " No " ) ; } } public static void main ( String args [ ] ) { int N = 3 , S = 10 , X = 4 ; findIfPossible ( N , S , X ) ; } }