Make sum of all subarrays of length K equal by only inserting elements | Java 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
import java . util . * ; class GFG { static void MakeArray ( int a [ ] , int n , int k ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! mp . containsKey ( a [ i ] ) ) mp . put ( a [ i ] , 1 ) ; } if ( mp . size ( ) > k ) { System . out . print ( "Not possibleNEW_LINE"); return ; } Vector < Integer > ans = new Vector < Integer > ( ) ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { ans . add ( i . getKey ( ) ) ; } while ( ans . size ( ) < k ) { ans . add ( 1 ) ; } for ( int i = 0 ; i < 2 ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) System . out . print ( ans . get ( j ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 1 } ; int K = 2 ; int size = arr . length ; MakeArray ( arr , size , K ) ; } }