Last active
March 28, 2018 00:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's still not working, Have to find the problem