Find number formed by K times alternatively reducing X and adding Y to 0 | Java program for the above approach ; Function to find the value obtained after alternatively reducing X and adding Y to 0 total K number of times ; Stores the final result after adding only Y to 0 ; Stores the final number after reducing only X from 0 ; Return the result obtained ; Driver Code
import java . util . * ; class GFG { static int positionAfterKJumps ( int X , int Y , int K ) { int addY = Y * ( K / 2 ) ; int reduceX = - 1 * X * ( K / 2 + K % 2 ) ; return addY + reduceX ; } public static void main ( String [ ] args ) { int X = 2 , Y = 5 , K = 3 ; System . out . print ( positionAfterKJumps ( X , Y , K ) ) ; } }