Find number formed by K times alternatively reducing X and adding Y to 0 | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int positionAfterKJumps ( int X , int Y , int K ) { int addY = Y * ( K / 2 ) ; int reduceX = -1 * X * ( K / 2 + K % 2 ) ; return addY + reduceX ; } int main ( ) { int X = 2 , Y = 5 , K = 3 ; cout << positionAfterKJumps ( X , Y , K ) ; return 0 ; }