Make sum of all subarrays of length K equal by only inserting elements | C # implementation of above approach ; Function that prints another array whose all subarray of length k have an equal sum ; Store all distinct elements in the unordered map ; Condition if the number of distinct elements is greater than k ; Push all distinct elements in a vector ; Push 1 while the size of vector not equal to k ; Print the vector 2 times ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void MakeArray ( int [ ] a , int n , int k ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! mp . ContainsKey ( a [ i ] ) ) mp . Add ( a [ i ] , 1 ) ; } if ( mp . Count > k ) { Console . Write ( " Not ▁ possible STRNEWLINE " ) ; return ; } List < int > ans = new List < int > ( ) ; foreach ( KeyValuePair < int , int > i in mp ) { ans . Add ( i . Key ) ; } while ( ans . Count < k ) { ans . Add ( 1 ) ; } for ( int i = 0 ; i < 2 ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) Console . Write ( ans [ j ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 1 } ; int K = 2 ; int size = arr . Length ; MakeArray ( arr , size , K ) ; } }