Need help with Programm to print next largest element in array

Hi ! everyone im a begginer c++ programmer and Im trying to make a programm that will print the next greatest element of an array. for example [4,5,2,25} if the element doesnt have a greater element next to it the next greater element should be considered as 1.
4 --> 5
5 --> 25
2 --> 25
25 --> -1

it works ok but it doesnt show the first element correctly. What am i doing worng? Any help is very much appreciated. Thanks everyone.





#include<iostream>

#include<stdio.h>
using namespace std;


int main()



{

const int arr = 4;
int array [arr]= { 4, 5,2, 25 };

int large;

int i;

int j;


for (i=0; i<arr; i++)

{
large = 1;





for (int j = 1 + 1; j < arr; j++)
{


if (array[i] < array[j])

{
large = array[j];
break;

}


}

printf("%d -- %d\n", array[i], large);
}

system("pause");
return 0;
}
Last edited on
I would suggest sorting out array from biggest to smallest using bubble sort and then print 2nd array.
So in case you have array of 5 elements you would print array[1].
Last edited on
So im Given an array, and i have to print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element on the right side of x in array. Elements for which no greater element exist, consider next greater element as 1. It works but it doesnt print outt the NGE for the first element for some reason
Last edited on
Its an amazon interview question i found onlinr. So it must be complicated.
Topic archived. No new replies allowed.