Remaining array element after repeated removal of last element and subtraction of each element from next adjacent element | package whatever do not write package name here ; Function to find the last remaining array element after performing the given operations repeatedly ; Stores the resultant sum ; Traverse the array ; Increment sum by arr [ i ] * coefficient of i - th term in ( x - y ) ^ ( N - 1 ) ; Update multiplier ; Return the resultant sum ; Driver Code
import java . io . * ; class GFG { public static int lastElement ( int arr [ ] , int n ) { int sum = 0 ; int multiplier = n % 2 == 0 ? - 1 : 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] * multiplier ; multiplier = multiplier * ( n - 1 - i ) / ( i + 1 ) * ( - 1 ) ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 2 , 1 } ; int N = 4 ; System . out . println ( lastElement ( arr , N ) ) ; } }