class Solution{
public:
vector<int> find3Numbers(vector<int> arr, int n) {
// Your code here
vector<int>v;
int min=0;
int max=n-1;
int* smaller=new int[n];
int* greater=new int[n];
smaller[0]=-1;
greater[n-1]=-1;
for(int i=1;i<n;i++)
{
if(arr[i]<=arr[min])
{
min=i;
smaller[i]=-1;
}
else smaller[i]=min;
}
for(int i=n-2;i>=0;i--)
{
if(arr[i]>=arr[max])
{
max=i;
greater[i]=-1;
}
else greater[i]=max;
}
for(int i=0;i<n;i++)
{
if(smaller[i]!=-1 && greater[i]!=-1)
{
v.push_back(arr[smaller[i]]);
v.push_back(arr[i]);
v.push_back(arr[greater[i]]);
break;
}
}
delete[] greater;
delete[] smaller;
return v;
}
};
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter