The question is asking, if all the subarray of length k have equal sum, if not can it be achieved by inserting elements. If it can be then print the new array else print -1. There is no limit to no. of elements you can insert, but it should be less than n.
Constraints
(1≤t≤50)
(1≤k≤n≤100)
(1≤a(i)≤n)
code
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
int n,k;
while(t--){
bool find = true;
cin >> n>>k;
vector<int> a(n);
int ar[n+1]={0};
for(int i=0;i<n;i++){
cin>>a[i];
if(i<k){
ar[a[i]]=1;
}
}
for(int i=k;i<n;i++){
if(ar[a[i]]!=1){
find = false;
break;
}
}
if(find ==false)
cout<<-1;
else{
for(int i=k;i<a.size();i++){
if(a[i]!=a[i-k]){
a.insert(a.begin()+i,a[i-k]);
}
}
cout<<a.size()<<"\n";
for(int i=0;i<a.size();i++){
cout<<a[i]<<" ";
}
}
cout<<"\n";
}
return 0;
}
I'm new to STL. When i'm trying to submit this code it is showing memory limit exceeded on codeforces.