text: Delete odd and even numbers at alternate step such that sum of remaining elements is minimized | C # implementation of the approach ; Function to find the minimized sum ; If more odd elements ; Sort the elements ; Left - over elements ; Find the sum of leftover elements ; Return the sum ; If more even elements ; Sort the elements ; Left - over elements ; Find the sum of leftover elements ; Return the sum ; If same elements ; Driver code
code: using System ; using System . Collections . Generic ; class GFG { static int MinimizeleftOverSum ( int [ ] a , int n ) { List < int > v1 = new List < int > ( ) , v2 = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) v1 . Add ( a [ i ] ) ; else v2 . Add ( a [ i ] ) ; } if ( v1 . Count > v2 . Count ) { v1 . Sort ( ) ; v2 . Sort ( ) ; int x = v1 . Count - v2 . Count - 1 ; int sum = 0 ; int i = 0 ; while ( i < x ) { sum += v1 [ i ++ ] ; } return sum ; } else if ( v2 . Count > v1 . Count ) { v1 . Sort ( ) ; v2 . Sort ( ) ; int x = v2 . Count - v1 . Count - 1 ; int sum = 0 ; int i = 0 ; while ( i < x ) { sum += v2 [ i ++ ] ; } return sum ; } else return 0 ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 2 , 2 , 2 } ; int n = a . Length ; Console . WriteLine ( MinimizeleftOverSum ( a , n ) ) ; } }