How to find all elements (leaders) in an array that are greater than all elements to their right in C#

1 Answer

0 votes
using System;

public class Program
{
	public static void PrintArrayLeaders(int[] arr)
	{
		int size = arr.Length;
		
		for (int i = 0; i < size; i++) {
			int j;
			for (j = i + 1; j < size; j++) {
				if (arr[i] <= arr[j]) {
					break;
				}
			}
			if (j == size) {
				Console.Write(arr[i] + " ");
			}
		}
	}

	public static void Main(string[] args)
	{
		int[] arr = new int[] {1, 99, 16, 5, 75, 9, 50, 60, 0, 19};

		PrintArrayLeaders(arr);
	}
}






/*
run:
 
99 75 60 19 
 
*/

 



answered Aug 30, 2022 by avibootz
...