text: Maximize sum of LSBs of Bitwise OR of all possible N / 2 pairs from given Array | C # program for the above approach ; Function top get LSB value of v ; Binary conversion ; Function to find the sum of LSBs of all possible pairs of the given array ; Stores the LSB of array elements ; Storing the LSB values ; Sort the array lab_arr [ ] ; Taking pairwise sum to get the maximum sum of LSB ; Print the result ; Driver Code ; Function Call
code: using System ; using System . Collections . Generic ; public class GFG { static int chk ( int n ) { List < int > v = new List < int > ( ) ; while ( n != 0 ) { v . Add ( n % 2 ) ; n = n / 2 ; } int j = 0 ; foreach ( int i in v ) { if ( i == 1 ) { return ( int ) Math . Pow ( 2.0 , ( double ) j ) ; } j ++ ; } return 0 ; } static void sumOfLSB ( int [ ] arr , int N ) { int [ ] lsb_arr = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { lsb_arr [ i ] = chk ( arr [ i ] ) ; } Array . Sort ( lsb_arr ) ; int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i += 2 ) { ans += ( lsb_arr [ i + 1 ] ) ; } Console . WriteLine ( ans ) ; } static public void Main ( ) { int N = 5 ; int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; sumOfLSB ( arr , N ) ; } }