If you are a c# beginner or want to start learning the c# programming language, then this program will help you to understand the basics of c# programming. In this program, we are going to share C# program to print next greater elements in a given array with the output.
To increase your C# knowledge, practice all C# programs, here is a Collection of 100+ C# problems with solutions.
Copy the below c# program and execute it in your Microsoft Visual Studio IDE (Integrated Development Environment ). At the end of this program, I have shared the output program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System; class FindGreaterElementsPrograms { static void printNGE(int []arr, int n) { int next, i, j; for (i = 0; i < n; i++) { next = -1; for (j = i + 1; j < n; j++) { if (arr[i] < arr[j]) { next = arr[j]; break; } } Console.WriteLine(arr[i] + " -- " + next); } } public static void Main() { int []arr= {11, 13, 21, 3}; int n = arr.Length; printNGE(arr, n); } } |
11 — 13
13 — 21
21 — -1
3 — -1
Same programs in other languages:
Java program to print next greater elements in a given array
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.