Find the last positive element remaining after repeated subtractions of smallest positive element from all Array elements | C # program for the above approach ; Function to calculate last positive element of the array ; Return the first element if N = 1 ; Stores the greatest and the second greatest element ; Traverse the array A [ ] ; If current element is greater than the greatest element ; If current element is greater than second greatest element ; Return the final answer ; Driver Code
using System ; class GFG { static int lastPositiveElement ( int [ ] arr ) { int N = arr . Length ; if ( N == 1 ) return arr [ 0 ] ; int greatest = - 1 , secondGreatest = - 1 ; for ( int x = 0 ; x < N ; x ++ ) { if ( arr [ x ] >= greatest ) { secondGreatest = greatest ; greatest = arr [ x ] ; } else if ( arr [ x ] >= secondGreatest ) { secondGreatest = arr [ x ] ; } } return greatest - secondGreatest ; } public static void Main ( ) { int [ ] arr = { 3 , 5 , 4 , 7 } ; Console . Write ( lastPositiveElement ( arr ) ) ; } }