# Find number formed by K times alternatively reducing X and adding Y to 0

Given three positive integers **K**, **X**, and **Y**, the task is to find the number formed by alternatively subtracting **X** and adding **Y** to **0** total **K** number of times.

**Examples:**

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.

Input:X = 2, Y = 5, K = 3Output:1Explanation:

Following are the operations perform K(= 3) number of times on 0:Operation 1:Reduce the value 0 by X(= 2) modifies it to 0 – 2 = 2.Operation 2:Increment the value -2 by Y(= 5) modifies it to -2 + 5 = 3.Operation 3:Reduce the value 3 by X(= 2) modifies it to 3 – 2 = 1.

The value obtained after modifying the value is 1.

Input:X = 1, Y = 100, K = 4Output:198

**Naive Approach:** The given problem can be solved by performing the given operations **K** number of times and print the result obtained.

**Time Complexity:** O(K)**Auxiliary Space:** O(1)

**Efficient Approach:** The above approach can also be optimized by finding the total value that is decremented(using the value of **X**) and incremented(using the value of **Y**) in **K** number of moves and then print the sum of these values as the result.

The value that must be added to the result is calculated by:

addY = Y*(K/2)

where, K/2 number of times addition operation is performed.

The value that must be subtracted to the result is calculated by:

addY = Y*(K/2 + K&1)

where, K/2 number of times subtraction operation is performed if the number of operation is odd, then additional subtraction is performed.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the value obtained` `// after alternatively reducing X and` `// adding Y to 0 total K number of times` `int` `positionAfterKJumps(` `int` `X, ` `int` `Y, ` `int` `K)` `{` ` ` `// Stores the final result after` ` ` `// adding only Y to 0` ` ` `int` `addY = Y * (K / 2);` ` ` `// Stores the final number after` ` ` `// reducing only X from 0` ` ` `int` `reduceX = -1 * X * (K / 2 + K % 2);` ` ` `// Return the result obtained` ` ` `return` `addY + reduceX;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `X = 2, Y = 5, K = 3;` ` ` `cout << positionAfterKJumps(X, Y, K);` ` ` `return` `0;` `}` |

## Java

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

## Python3

`# Python 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` `def` `positionAfterKJumps(X, Y, K):` ` ` ` ` `# Stores the final result after` ` ` `# adding only Y to 0` ` ` `addY ` `=` `Y ` `*` `(K ` `/` `/` `2` `)` ` ` `# Stores the final number after` ` ` `# reducing only X from 0` ` ` `reduceX ` `=` `-` `1` `*` `X ` `*` `(K ` `/` `/` `2` `+` `K ` `%` `2` `)` ` ` `# Return the result obtained` ` ` `return` `addY ` `+` `reduceX` `# Driver Code` `X ` `=` `2` `Y ` `=` `5` `K ` `=` `3` `print` `(positionAfterKJumps(X, Y, K))` `# This code is contributed by subhammahato348.` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG {` ` ` `// Function to find the value obtained` ` ` `// after alternatively reducing X and` ` ` `// adding Y to 0 total K number of times` ` ` `static` `int` `positionAfterKJumps(` `int` `X, ` `int` `Y, ` `int` `K)` ` ` `{` ` ` ` ` `// Stores the final result after` ` ` `// adding only Y to 0` ` ` `int` `addY = Y * (K / 2);` ` ` `// Stores the final number after` ` ` `// reducing only X from 0` ` ` `int` `reduceX = -1 * X * (K / 2 + K % 2);` ` ` `// Return the result obtained` ` ` `return` `addY + reduceX;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main(String[] args)` ` ` `{` ` ` `int` `X = 2, Y = 5, K = 3;` ` ` `Console.WriteLine(positionAfterKJumps(X, Y, K));` ` ` `}` `}` `// This code is contributed by ukasp.` |

## Javascript

`<script>` ` ` `// JavaScript Program to implement` ` ` `// the above approach` ` ` `// Function to find the value obtained` ` ` `// after alternatively reducing X and` ` ` `// adding Y to 0 total K number of times` ` ` `function` `positionAfterKJumps(X, Y, K) {` ` ` `// Stores the final result after` ` ` `// adding only Y to 0` ` ` `let addY = Y * Math.floor(K / 2);` ` ` `// Stores the final number after` ` ` `// reducing only X from 0` ` ` `let reduceX = -1 * X * (Math.floor(K / 2) + K % 2);` ` ` `// Return the result obtained` ` ` `return` `addY + reduceX;` ` ` `}` ` ` `// Driver Code` ` ` `let X = 2, Y = 5, K = 3;` ` ` `document.write(positionAfterKJumps(X, Y, K));` `// This code is contributed by Potta Lokesh` ` ` `</script>` |

**Output:**

1

**Time Complexity:** O(1)**Auxiliary Space:** O(1)