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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MakeArray ( int a [ ] , int n , int k ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . find ( a [ i ] ) == mp . end ( ) ) mp [ a [ i ] ] = 1 ; } if ( mp . size ( ) > k ) { cout << " Not ▁ possible STRNEWLINE " ; return ; } vector < int > ans ; for ( auto i : mp ) { ans . push_back ( i . first ) ; } while ( ans . size ( ) < k ) { ans . push_back ( 1 ) ; } for ( int i = 0 ; i < 2 ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) cout << ans [ j ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 1 } ; int K = 2 ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; MakeArray ( arr , size , K ) ; return 0 ; }