My issue
in the testcase
1
3 1
3 10 1
the output produced by my code and according to the problem statement is 10, but the output given in the testcase is 3. Can anyone explain that.
My code
#include <bits/stdc++.h>
using namespace std;
int main() {
int t,n,k;
cin>>t;
while(t--){
cin>>n>>k;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int arr_min=INT_MAX;
int ind_min=0;
int arr_max=INT_MIN;
int ind_max=0;
for(int i=0;i<n;i++){
if(arr[i]>arr_max){
arr_max=arr[i];
ind_max=i;
}
if(arr[i]<arr_min){
arr_min=arr[i];
ind_min=i;
}
}
if(k>=n-1){
cout<<arr_max<<endl;
}else{
int app_max=INT_MIN;
for(int i=ind_min-k;i<=ind_min+k;i++){
if(i>0 && i<n){
if(arr[i]>app_max){
app_max=arr[i];
}
}
}
cout<<app_max<<endl;
}
}
return 0;
}
Problem Link: CodeChef: Practical coding for everyone