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
def MakeArray ( a , n , k ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in a : NEW_LINE INDENT if i not in mp : NEW_LINE INDENT mp [ a [ i ] ] = 1 NEW_LINE DEDENT DEDENT if ( len ( mp ) > k ) : NEW_LINE print ( " Not ▁ possible " ) NEW_LINE return NEW_LINE ans = [ ] NEW_LINE for i in mp : NEW_LINE INDENT ans . append ( i ) NEW_LINE DEDENT while ( len ( ans ) < k ) : NEW_LINE INDENT ans . append ( 1 ) NEW_LINE DEDENT for i in range ( 2 ) : NEW_LINE INDENT for j in range ( k ) : NEW_LINE INDENT print ( ans [ j ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 1 ] NEW_LINE K = 2 NEW_LINE size = len ( arr ) NEW_LINE MakeArray ( arr , size , K ) NEW_LINE DEDENT