GOOGLE INTERNSHIP CODING QUESTION

Can someone tell me what is wrong with my approach to question.

My Code:

#include <bits/stdc++.h>
using namespace std;
int finder(vector &arr,int k,int n)
{
int counter=0;
auto it=arr.rbegin();
int maxi=*it;
for(auto itr=arr.rbegin();itr<arr.rend();itr++)
{
arr.clear(itr);
}
int sizi=0;
it++;
while(counter<k && it!=arr.rend())
{
sizi++;
int sub=maxi-*it;
counter=counter+sub;
it++;
}
if(it==arr.rend()&&counter<k)
sizi++;

return sizi;

}
int main()
{
// #ifndef ONLINE_JUDGE
// freopen(“input.txt”,“r”,stdin);
// freopen(“output.txt”,“w”,stdout);
// #endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
vector arr;
for(int i=0;i<n;i++)
{
int a;
cin>>a;
arr.push_back(a);
}
sort(arr.begin(),arr.end());
cout<<finder(arr,k,n)<<endl;
}
}

you need to find a subarray so you can’t sort it, bcz you have lost the order of elements

may be sliding window with 2 pointers will help

First of all please format your code , by using ```(three backticks) before and after the code.

Second as u sort the array , so the order of element changes .

Googling skills matters : “https://www.geeksforgeeks.org/length-of-longest-subarray-with-same-elements-in-atmost-k-increments/