Construct the smallest possible Array with given Sum and XOR | Java program implementation of the approach ; Function to find array ; Array not possible ; Array possible with exactly 1 or no element ; Checking array with two elements possible or not . ; Driver code ; Given sum and value of Bitwise XOR ; Function call
import java . util . * ; import java . io . * ; class GFG { static void findArray ( int sum , int xorr ) { if ( xorr > sum sum % 2 != xorr % 2 ) { System . out . println ( " No ▁ Array ▁ Possible " ) ; return ; } if ( sum == xorr ) { if ( sum == 0 ) System . out . println ( " Array ▁ is ▁ empty ▁ " + " with ▁ size ▁ 0" ) ; else System . out . println ( " Array ▁ size ▁ is ▁ " + 1 ) ; System . out . println ( " Array ▁ is ▁ " + sum ) ; return ; } int mid = ( sum - xorr ) / 2 ; if ( xorr == 1 && mid == 1 ) { System . out . println ( " Array ▁ size ▁ is ▁ " + 3 ) ; System . out . println ( " Array ▁ is ▁ " + xorr + " ▁ " + mid + " ▁ " + mid ) ; } else { System . out . println ( " Array ▁ size ▁ is ▁ " + 2 ) ; System . out . println ( " Array ▁ is ▁ " + ( xorr + mid ) + " ▁ " + mid ) ; } } public static void main ( String [ ] args ) { int sum = 4 , xorr = 2 ; findArray ( sum , xorr ) ; } }