Skip to content

Instantly share code, notes, and snippets.

@0xdarkcap
Last active March 28, 2018 00:18
Show Gist options
  • Save 0xdarkcap/a7ce7896bc1a8c0660cd09de2fdc9384 to your computer and use it in GitHub Desktop.
Save 0xdarkcap/a7ce7896bc1a8c0660cd09de2fdc9384 to your computer and use it in GitHub Desktop.
An array is given of n length, and we need to calculate the next greater element for each element in given array. If next greater element is not available in given array then we need to fill ‘_’ at that index place.
#include <stdio.h>
int main()
{
int i,j,c,n,min,flag=0,d=0;
scanf("%d",&n); //scanning the number of element in array.
int a[n],b[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]); // User enters array.
}
for(i=0;i<n;i++)
{
min=0;
for(j=0;j<n;j++)
{
if((a[j]-a[i])<min&&(a[j]-a[i])>0)
{
min=a[j]-a[i];
c=a[j];
flag=1;
}
}
if(flag=1)
{
b[d]=c;
d++;
}
else
{
b[d]='_';
d++;
}
}
for(i=0;i<n;i++)
{
printf("%d",b[i]);
}
return 0;
}
@0xdarkcap
Copy link
Author

It's still not working, Have to find the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment