Reduce given array by replacing subarrays with values less than K with their sum | C # program for the above approach ; Function to replace all the subarray having values < K with their sum ; Stores the sum of subarray having elements less than K ; Stores the updated array ; Traverse the array ; Update the sum ; Otherwise , update the vector ; Print the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void updateArray ( List < int > arr , int K ) { int sum = 0 ; List < int > res = new List < int > ( ) ; for ( int i = 0 ; i < arr . Count ; i ++ ) { if ( arr [ i ] < K ) { sum += arr [ i ] ; } else { if ( sum != 0 ) { res . Add ( sum ) ; } sum = 0 ; res . Add ( arr [ i ] ) ; } } if ( sum != 0 ) res . Add ( sum ) ; foreach ( int it in res ) Console . Write ( it + " ▁ " ) ; } public static void Main ( ) { List < int > arr = new List < int > { 200 , 6 , 36 , 612 , 121 , 66 , 63 , 39 , 668 , 108 } ; int K = 100 ; updateArray ( arr , K ) ; } }