CSUBS -COOK-OFF
Problem Link :-CSUBS Problem - CodeChef
Can someone tell where my code is failing or suggest some teat cases???
My idea: used sliding window to calculate the sum of each subarray of length k and storing its freq in map.
I counted total number of subarray using the c variable.
Finally, my answer is cout<< c-(highest freq of the subarray sum occurring denoted by x )
#include <iostream>
using namespace std;
#include<bits/stdc++.h>
#define pb push_back
typedef long long ll;
#define endl "\n";
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--)
{
ll n,k,sum=0;
cin>>n>>k;
ll arr[n];
for(ll i=0;i<n;i++)
{
cin>>arr[i];
}
for(ll i=0;i<k;i++)
{
sum+=arr[i];
}
ll i=0,j=k;
unordered_map<ll,ll>freq;
freq[sum]++;
ll c=1; // represnts total subets sum count .one has already been included above.
while(i<n && j<n)
{
sum+=arr[j];
sum-=arr[i];
freq[sum]++;
i++;
j++;
c++;
}
if(n==k ||n==1)
{
cout<<0<<endl;
}
else
{
ll x = INT_MIN;
for(auto it:freq)
{
x = max(x,it.second);
}
cout<<c-x <<endl;
}
}
}