Given a pair of positive numbers x and y. We repeatedly subtract the smaller of the two integers from greater one until one of the integers becomes 0. The task is to count number of steps to before we stop (one of the numbers become 0).

**Examples :**

Input : x = 5, y = 13 Output : 6 Explanation : There are total 6 steps before we reach 0: (5,13) --> (5,8) --> (5,3) --> (2,3) --> (2,1) --> (1,1) --> (1,0). Input : x = 3, y = 5 Output : 4 Explanation : There are 4 steps: (5,3) --> (2,3) --> (2,1) --> (1,1) --> (1,0) Input : x = 100, y = 19 Output : 13

A simple solution is to actually follow the process and count the number of steps.

A better solution is to use below steps. Let y be the smaller of two numbers

1) if y divides x then return (x/y)

2) else return ( (x/y) + solve(y, x%y) )

**Illustration :**

If we start with (x, y) and y divides x then the answer will be (x/y) since we can subtract y form x exactly (x/y) times.

For the other case, we take an example to see how it works: (100, 19)

We can subtract 19 from 100 exactly [100/19] = 5 times to get (19, 5).

We can subtract 5 from 19 exactly [19/5] = 3 times to get (5, 4).

We can subtract 4 from 5 exactly [5/4] = 1 times to get (4, 1).

We can subtract 1 from 4 exactly [4/1] = 4 times to get (1, 0)

hence a total of 5 + 3 + 1 + 4 = 13 steps.

Below is implementation based on above idea.

## C++

`// C++ program to count of steps until one ` `// of the two numbers become 0. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns count of steps before one ` `// of the numbers become 0 after repeated ` `// subtractions. ` `int` `countSteps(` `int` `x, ` `int` `y) ` `{ ` ` ` `// If y divides x, then simply return ` ` ` `// x/y. ` ` ` `if` `(x%y == 0) ` ` ` `return` `x/y; ` ` ` ` ` `// Else recur. Note that this function ` ` ` `// works even if x is smaller than y because ` ` ` `// in that case first recursive call exchanges ` ` ` `// roles of x and y. ` ` ` `return` `x/y + countSteps(y, x%y); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `x = 100, y = 19; ` ` ` `cout << countSteps(x, y); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count of ` `// steps until one of the ` `// two numbers become 0. ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns count of steps ` `// before one of the numbers ` `// become 0 after repeated ` `// subtractions. ` `static` `int` `countSteps(` `int` `x, ` ` ` `int` `y) ` `{ ` ` ` `// If y divides x, then ` ` ` `// simply return x/y. ` ` ` `if` `(x % y == ` `0` `) ` ` ` `return` `x / y; ` ` ` ` ` `// Else recur. Note that this ` ` ` `// function works even if x is ` ` ` `// smaller than y because ` ` ` `// in that case first recursive ` ` ` `// call exchanges roles of x and y. ` ` ` `return` `x / y + countSteps(y, x % y); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `x = ` `100` `, y = ` `19` `; ` ` ` `System.out.println(countSteps(x, y)); ` ` ` `} ` `} ` ` ` `// This code is contributed by aj_36 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count of steps until ` `# one of the two numbers become 0. ` `import` `math ` ` ` `# Returns count of steps before one of ` `# the numbers become 0 after repeated ` `# subtractions. ` `def` `countSteps(x, y): ` ` ` ` ` `# If y divides x, then simply ` ` ` `# return x/y. ` ` ` `if` `(x ` `%` `y ` `=` `=` `0` `): ` ` ` `return` `math.floor(x ` `/` `y); ` ` ` ` ` `# Else recur. Note that this function ` ` ` `# works even if x is smaller than y ` ` ` `# because in that case first recursive ` ` ` `# call exchanges roles of x and y. ` ` ` `return` `math.floor((x ` `/` `y) ` `+` ` ` `countSteps(y, x ` `%` `y)); ` ` ` `# Driver code ` `x ` `=` `100` `; ` `y ` `=` `19` `; ` `print` `(countSteps(x, y)); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count of ` `// steps until one of the ` `// two numbers become 0. ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Returns count of steps ` `// before one of the numbers ` `// become 0 after repeated ` `// subtractions. ` `static` `int` `countSteps(` `int` `x, ` ` ` `int` `y) ` `{ ` ` ` `// If y divides x, then ` ` ` `// simply return x/y. ` ` ` `if` `(x % y == 0) ` ` ` `return` `x / y; ` ` ` ` ` `// Else recur. Note that this ` ` ` `// function works even if x is ` ` ` `// smaller than y because ` ` ` `// in that case first recursive ` ` ` `// call exchanges roles of x and y. ` ` ` `return` `x / y + countSteps(y, x % y); ` `} ` ` ` `// Driver Code ` `static` `public` `void` `Main () ` `{ ` `int` `x = 100, y = 19; ` `Console.WriteLine(countSteps(x, y)); ` `} ` `} ` ` ` `// This code is contributed by m_kit ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count of ` `// steps until one of the ` `// two numbers become 0. ` ` ` `// Returns count of steps ` `// before one of the numbers ` `// become 0 after repeated ` `// subtractions. ` `function` `countSteps(` `$x` `, ` `$y` `) ` `{ ` ` ` `// If y divides x, then ` ` ` `// simply return x/y. ` ` ` `if` `(` `$x` `% ` `$y` `== 0) ` ` ` `return` `floor` `(((int)` `$x` `/ ` `$y` `)); ` ` ` ` ` `// Else recur. Note that this ` ` ` `// function works even if x is ` ` ` `// smaller than y because in that ` ` ` `// case first recursive call ` ` ` `// exchanges roles of x and y. ` ` ` `return` `floor` `(((int)` `$x` `/ ` `$y` `) + ` ` ` `countSteps(` `$y` `, ` `$x` `% ` `$y` `)); ` `} ` ` ` `// Driver code ` `$x` `= 100; ` `$y` `= 19; ` `echo` `countSteps(` `$x` `, ` `$y` `); ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

13

This article is contributed by **Shubham Agrawal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.

## Recommended Posts:

- Square root of a number by Repeated Subtraction method
- Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum
- Check if N can be obtained by repetitive addition or subtraction of two given numbers
- Subtraction of two large numbers using 9's compliment
- Subtraction of two large numbers using 10's compliment
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Check perfect square using addition/subtraction
- Addition and Subtraction of Matrix using pthreads
- Find the larger exponential among two exponentials
- Find position of the given number among the numbers made of 4 and 7
- Position of n among the numbers made of 2, 3, 5 & 7
- Find maximum product of digits among numbers less than or equal to N
- Maximum LCM among all pairs (i, j) of first N natural numbers
- Maximum GCD among all pairs (i, j) of first N natural numbers
- Smallest length string with repeated replacement of two distinct adjacent
- Total numbers with no repeated digits in a range
- Squares of numbers with repeated single digits | Set 1 (3, 6 and 9)
- Repeated sum of first N natural numbers
- Find a number K having sum of numbers obtained by repeated removal of last digit of K is N