Left Greater Number
Given an array of N positive integers, for each number print the first number to it’s left which is greater than the current number. Print -1 if there is no greater number on it's left.
Input Format:
The first line contains N.
The second line contains N space separated positive integers.
Output Format:
N integers which represent the left greater numbers separated by a space.
Boundary Conditions:
2 <= N <= 10^6
Example Input/Output 1:
Input:
6
5 3 2 4 8 6
Output:
-1 5 3 5 -1 8
Example Input/Output 2:
Input:
11
3 8 5 11 10 10 3 16 11 12 15
Output:
-1 -1 8 -1 11 11 10 -1 16 16 16
Input Format:
The first line contains N.
The second line contains N space separated positive integers.
Output Format:
N integers which represent the left greater numbers separated by a space.
Boundary Conditions:
2 <= N <= 10^6
Example Input/Output 1:
Input:
6
5 3 2 4 8 6
Output:
-1 5 3 5 -1 8
Example Input/Output 2:
Input:
11
3 8 5 11 10 10 3 16 11 12 15
Output:
-1 -1 8 -1 11 11 10 -1 16 16 16
Code:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int i,j,n,flag=0;
cin>>n;
int arr[n];
for(i=0;i<n;i++)
{
cin>>arr[i];
j=i;
flag=0;
while(j>=0)
{
if(arr[j]>arr[i])
{
cout<<arr[j]<<" ";
flag=1;break;
}
else if(i==0)
{cout<<"-1"<<" ";break;}
if(j==0&&flag==0)
cout<<"-1"<<" ";
j--;
}
}
}
#v2 #v2lancers
#v2 #v2lancers
No comments:
Post a Comment