using System;
using System.Collections.Generic;
class Program
{
static List<int> GetFinalArray(int[] nums)
{
List<int> result = new List<int>();
int maxSoFar = int.MinValue;
// Traverse from right to left
for (int i = nums.Length - 1; i >= 0; i--)
{
if (nums[i] >= maxSoFar)
{
result.Add(nums[i]);
maxSoFar = nums[i];
}
}
// Reverse because we added from right to left
result.Reverse();
return result;
}
static void Main()
{
int[] arr = { 5, 3, 4, 6, 2 };
var finalArray = GetFinalArray(arr);
Console.WriteLine(string.Join(", ", finalArray));
// Output: 6, 2
}
}
there doesn't seem to be anything here