Make sum of all subarrays of length K equal by only inserting elements | 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
function MakeArray ( a , n , k ) { var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( ! mp . has ( a [ i ] ) ) mp . set ( a [ i ] , 1 ) ; } if ( mp . count > k ) { document . write ( " " ) ; return ; } var ans = [ ] ; mp . forEach ( ( value , key ) => { ans . push ( key ) ; } ) ; while ( ans . length < k ) { ans . push ( 1 ) ; } for ( var i = 0 ; i < 2 ; i ++ ) { for ( var j = 0 ; j < k ; j ++ ) document . write ( ans [ j ] + " " ) ; } } var arr = [ 1 , 2 , 2 , 1 ] ; var K = 2 ; var size = arr . length ; MakeArray ( arr , size , K ) ;