We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
voidnextGreaterElement(vector<long>&arr,vector<long>&left,vector<long>&right){stack<long>s;s.push(0);for(inti=1;i<arr.size();i++){while(s.empty()==false&&arr[s.top()]>arr[i]){right[s.top()]=i;s.pop();}s.push(i);}while(s.empty()==false){right[s.top()]=arr.size();s.pop();}s.push(arr.size()-1);for(inti=arr.size()-2;i>=0;i--){while(s.empty()==false&&arr[s.top()]>arr[i]){left[s.top()]=i;s.pop();}s.push(i);}while(s.empty()==false){left[s.top()]=-1;s.pop();}}// Complete the riddle function below.vector<long>riddle(vector<long>arr){// complete this function//implement next greater element algovector<long>left(arr.size(),0);vector<long>right(arr.size(),0);vector<long>ans(arr.size(),0);nextGreaterElement(arr,left,right);for(longi=0;i<right.size();i++){longrange=right[i]-left[i]-1;ans[range-1]=arr[i];}for(longi=ans.size()-1;i>=0;i--){if(ans[i]==0){ans[i]=ans[i+1];}}returnans;}
can anyone help me with this code I can pass the 1st three cases but for the other cases getting wrong output.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Min Max Riddle
You are viewing a single comment's thread. Return to all comments →
can anyone help me with this code I can pass the 1st three cases but for the other cases getting wrong output.