COLPEAK Editorial

PROBLEM LINK:

Practice
Author:shivamg4149
Editorialist:shivamg4149

DIFFICULTY:

Simple

EXPLANATION:

We can simply run a loop from 1 to N-2 and compared the value at position i with i-1 and i+1 and if it is greater than both than there is peak.
By this we will be able to find the kth peak.

TIME COMPLEXITY:

O(N)

SOLUTIONS

#include<bits/stdc++.h>
using namespace std;

int main(){
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        int a[n];
        for(int i=0;i<n;i++)
            cin>>a[i];

        for(int i=1;i<n-1;i++){
          if(a[i]>a[i-1] && a[i]>a[i+1])
                    k--;

            if(k<1){
                cout<<i+1<<endl;
                break;
            }
        }
        if(k>0){
            cout<<-1<<endl;
        }
    }
}