Make sum of all subarrays of length K equal by only inserting elements
Last Updated :
06 Sep, 2022
Given an array arr[] of length N such that (1 <= arr[i] <= N), the task is to modify the array, by only inserting elements within the range [1, N], such that the sum of all subarrays of length K become equal. Print the modified array, if possible. Else print “Not possible”.
Examples:
Input: arr[] = {1, 2, 2, 1}, K = 2
Output: 1 2 1 2 1
Explanation:
Insert 1 between second and third element.
Now all subarray of length K has an equal sum of 3.
Input: arr[] = {1, 2, 3, 4}, K = 3
Output: Not possible
Approach:
- Since the removal of elements is not allowed, therefore the only case when such a modified array can be achieved, is when there are at most K distinct elements in the array.
- Therefore, Firstly check if there are more than K distinct elements in the given array. If yes, then print “Not possible”.
- Else, store all the distinct elements of the given array in a vector.
- If the number of distinct elements is less than K, then add/repeat some elements in the vector and make the size of the vector equal to K.
- The vector has now K elements. Now repeat the elements of the vector in the same order to show the modified array. This repetition is done to show all the subarrays of length K have the same sum.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
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\n" ;
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;
}
|
Java
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 possible\n" );
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);
}
}
|
Python3
def MakeArray(a, n, k):
mp = dict ()
for i in a:
if i not in mp:
mp[a[i]] = 1
if ( len (mp) > k):
print ( "Not possible" )
return
ans = []
for i in mp:
ans.append(i)
while ( len (ans) < k):
ans.append( 1 )
for i in range ( 2 ):
for j in range (k):
print (ans[j], end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 2 , 2 , 1 ]
K = 2
size = len (arr)
MakeArray(arr, size, K)
|
C#
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\n" );
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);
}
}
|
Javascript
<script>
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( "Not possible<br>" );
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);
</script>
|
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(N), in order to store elements in HashMap.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...