Given initial values of two positive integers **X** and **Y**, Find the final value of X and Y according to below alterations:

1. If

X=0orY=0, terminate the process. Else, go to step 2;

2. IfX >= 2*Y, then change the value ofX to X – 2*Y, and repeat step 1. Else, go to step 3;

3. IfY >= 2*X, then assign the value ofY to Y – 2*X, and repeat step 1. Else, end the process.

Constraints: 1<=X, Y<=10^18

**Examples:**

Input:X=12, Y=5Output:X=0, Y=1 Explanation: Initially X = 12, Y = 5 --> X = 2, Y = 5 (as X = X-2*Y) --> X = 2, Y = 1 (as Y = Y-2*X) --> X = 0, Y = 1 (as X = X-2*Y) --> Stop (as X = 0)Input:X=31, Y=12Output:X=7, Y=12 Explanation: Initially X = 31, Y = 12 --> X = 7, Y = 12 (as X = X-2*Y) --> Stop (as (Y - 2*X) < 0)

**Approach:** Since the initial values of X and Y can be as high as 10^18. Simple brute force approach will not work.

If we observe carefully, the problem statement is nothing but a sort of Euclid Algorithm, where we will replace all subtractions with modulo.**Implementation:**

## C++

`// CPP tp implement above approach` `#include <iostream>` `using` `namespace` `std;` `// Function to get final value of X and Y` `void` `alter(` `long` `long` `int` `x, ` `long` `long` `int` `y)` `{` ` ` `// Following the sequence` ` ` `// but by replacing minus with modulo` ` ` `while` `(` `true` `) {` ` ` `// Step 1` ` ` `if` `(x == 0 || y == 0)` ` ` `break` `;` ` ` `// Step 2` ` ` `if` `(x >= 2 * y)` ` ` `x = x % (2 * y);` ` ` `// Step 3` ` ` `else` `if` `(y >= 2 * x)` ` ` `y = y % (2 * x);` ` ` `// Otherwise terminate` ` ` `else` ` ` `break` `;` ` ` `}` ` ` `cout << ` `"X="` `<< x << ` `", "` ` ` `<< ` `"Y="` `<< y;` `}` `// Driver function` `int` `main()` `{` ` ` `// Get the initial X and Y values` ` ` `long` `long` `int` `x = 12, y = 5;` ` ` `// Find the result` ` ` `alter(x, y);` ` ` `return` `0;` `}` |

## Java

`// Java tp implement above approach` `import` `java.io.*;` `class` `GFG` `{` ` ` `// Function to get final value of X and Y` `static` `void` `alter(` `long` `x, ` `long` `y)` `{` ` ` `// Following the sequence but by` ` ` `// replacing minus with modulo` ` ` `while` `(` `true` `)` ` ` `{` ` ` `// Step 1` ` ` `if` `(x == ` `0` `|| y == ` `0` `)` ` ` `break` `;` ` ` `// Step 2` ` ` `if` `(x >= ` `2` `* y)` ` ` `x = x % (` `2` `* y);` ` ` `// Step 3` ` ` `else` `if` `(y >= ` `2` `* x)` ` ` `y = y % (` `2` `* x);` ` ` `// Otherwise terminate` ` ` `else` ` ` `break` `;` ` ` `}` ` ` `System.out.println(` `"X = "` `+ x + ` `", "` `+` ` ` `"Y = "` `+ y);` `}` `// Driver Code` `public` `static` `void` `main (String[] args)` `{` ` ` `// Get the initial X and Y values` ` ` `long` `x = ` `12` `, y = ` `5` `;` ` ` ` ` `// Find the result` ` ` `alter(x, y);` `}` `}` `// This code is contributed by` `// shk` |

## Python3

`# Python3 tp implement above approach` `import` `math as mt` `# Function to get final value of X and Y` `def` `alter(x, y):` ` ` `# Following the sequence but by` ` ` `# replacing minus with modulo` ` ` `while` `(` `True` `):` ` ` `# Step 1` ` ` `if` `(x ` `=` `=` `0` `or` `y ` `=` `=` `0` `):` ` ` `break` ` ` `# Step 2` ` ` `if` `(x >` `=` `2` `*` `y):` ` ` `x ` `=` `x ` `%` `(` `2` `*` `y)` ` ` `# Step 3` ` ` `elif` `(y >` `=` `2` `*` `x):` ` ` `y ` `=` `y ` `%` `(` `2` `*` `x)` ` ` `# Otherwise terminate` ` ` `else` `:` ` ` `break` ` ` ` ` `print` `(` `"X ="` `, x, ` `", "` `, ` `"Y ="` `, y)` `# Driver Code` `# Get the initial X and Y values` `x, y ` `=` `12` `, ` `5` `# Find the result` `alter(x, y)` `# This code is contributed by` `# Mohit kumar 29` |

## C#

`// C# implementation of the approach` `using` `System;` `class` `GFG` `{` ` ` `// Function to get final value of X and Y` ` ` `static` `void` `alter(` `long` `x, ` `long` `y)` ` ` `{` ` ` `// Following the sequence but by` ` ` `// replacing minus with modulo` ` ` `while` `(` `true` `)` ` ` `{` ` ` ` ` `// Step 1` ` ` `if` `(x == 0 || y == 0)` ` ` `break` `;` ` ` ` ` `// Step 2` ` ` `if` `(x >= 2 * y)` ` ` `x = x % (2 * y);` ` ` ` ` `// Step 3` ` ` `else` `if` `(y >= 2 * x)` ` ` `y = y % (2 * x);` ` ` ` ` `// Otherwise terminate` ` ` `else` ` ` `break` `;` ` ` `}` ` ` ` ` `Console.WriteLine(` `"X = "` `+ x + ` `", "` `+ ` `"Y = "` `+ y);` ` ` `}` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `Main ()` ` ` `{` ` ` `// Get the initial X and Y values` ` ` `long` `x = 12, y = 5;` ` ` ` ` `// Find the result` ` ` `alter(x, y);` ` ` `}` `}` `// This code is contributed by aishwarya.27` |

## PHP

`<?php` `// PHP implementation of the approach` `// Function to get final value of X and Y` `function` `alter(` `$x` `, ` `$y` `)` `{` ` ` `// Following the sequence but by` ` ` `// replacing minus with modulo` ` ` `while` `(true)` ` ` `{` ` ` `// Step 1` ` ` `if` `(` `$x` `== 0 || ` `$y` `== 0)` ` ` `break` `;` ` ` `// Step 2` ` ` `if` `(` `$x` `>= 2 * ` `$y` `)` ` ` `$x` `= ` `$x` `% (2 * ` `$y` `);` ` ` `// Step 3` ` ` `else` `if` `(` `$y` `>= 2 * ` `$x` `)` ` ` `$y` `= ` `$y` `% (2 * ` `$x` `);` ` ` `// Otherwise terminate` ` ` `else` ` ` `break` `;` ` ` `}` ` ` `echo` `"X = "` `, ` `$x` `, ` `", "` `, ` `"Y = "` `, ` `$y` `;` `}` `// Driver Code` `// Get the initial X and Y values` `$x` `= 12 ;` `$y` `= 5 ;` `// Find the result` `alter(` `$x` `, ` `$y` `);` `// This code is contributed by Ryuga` `?>` |

## Javascript

`<script>` `// Javascript to implement above approach` `// Function to get final value of X and Y` `function` `alter(x, y)` `{` ` ` ` ` `// Following the sequence but by` ` ` `// replacing minus with modulo` ` ` `while` `(` `true` `)` ` ` `{` ` ` ` ` `// Step 1` ` ` `if` `(x == 0 || y == 0)` ` ` `break` `;` ` ` `// Step 2` ` ` `if` `(x >= 2 * y)` ` ` `x = x % (2 * y);` ` ` `// Step 3` ` ` `else` `if` `(y >= 2 * x)` ` ` `y = y % (2 * x);` ` ` `// Otherwise terminate` ` ` `else` ` ` `break` `;` ` ` `}` ` ` ` ` `document.write(` `"X = "` `+ x + ` `", "` `+` ` ` `"Y = "` `+ y);` `}` `// Driver Code` `// Get the initial X and Y values` `var` `x = 12, y = 5;` `// Find the result` `alter(x, y);` `// This code is contributed by Kirti` `</script>` |

**Output:**

X=0, Y=1

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**