Difference between sum of K maximum even and odd array elements | C # program for the above approach ; Function to find the absolute difference between sum of first K maximum even and odd numbers ; Stores index from where odd number starts ; Segregate even and odd number ; If current element is even ; Sort in decreasing order even part ; Sort in decreasing order odd part ; Calculate sum of k maximum even number ; Calculate sum of k maximum odd number ; Print the absolute difference ; Driver Code ; Given array [ ] arr ; Size of array ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void evenOddDiff ( int [ ] a , int n , int k ) { int j = - 1 ; List < int > even = new List < int > ( ) ; List < int > odd = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) { even . Add ( a [ i ] ) ; } else odd . Add ( a [ i ] ) ; } j ++ ; even . Sort ( ) ; even . Reverse ( ) ; odd . Sort ( ) ; odd . Reverse ( ) ; int evenSum = 0 , oddSum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { evenSum += even [ i ] ; } for ( int i = 0 ; i < k ; i ++ ) { oddSum += odd [ i ] ; } Console . Write ( Math . Abs ( evenSum - oddSum ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 8 , 3 , 4 , 5 } ; int N = arr . Length ; int K = 2 ; evenOddDiff ( arr , N , K ) ; } }