Subtraction and two numbers | Practice | GeeksforGeeks

Given two integers A and B.Find out the number of steps required to repeatedly subtract the smaller of the two from the larger until one of them becomes 0.
Example 1:
Input:
A=5,B=13
Output:
6
Explanation:
The steps are as follows:
(5,13)->(5,8)->(5,3)->(2,3)->(2,1)->(1,1)->(1,0)
Thus, 6 steps are required.
Example 2:
Input:
A=5,B=15
Output:
3
Explanation:
The steps are as follows:
(5,15)->(5,10)->(5,5)->(5,0).
Thus, 3 steps are required.

Your Task:
You don't need to read input or print anything. Your task is to complete the function repeatedSubtraction() which takes two integers A and B as input parameters and returns the number of steps required to repeatedly subtract until one of the numbers becomes 0.

Expected Time Complexity:O(Log(max(A,B))
Expected Auxillary Space:O(1)

Constraints:
1<=A,B<=109
0
abhishekg0102 months ago
class Solution {
    int repeatedSubtraction(int A,int B){
        int ans;
        if(A==1){//if one of the number is 1 then count is equal to the other
            return B;
        }
         if(B==1){
            return A;
        }else{
       ans=sub(A,B,0);//calling recursive function 
        }
    return ans;
    }
     static int sub(int A,int B,int count){//recursion
     if(A==0|B==0){//base condition
             return count;//for counting
         }
        if(A>=B){
            A-=B;
            count++;
            return sub(A,B,count);//calling function 
        }else{
            B-=A;
            count++;
            return sub(A,B,count);//calling function
        }
     }
}
0
tandava12342 months ago
class Solution {   public:   int r(int A,int B,int c=0)   {       if(B==0 || A==0)           return c;       else if(A<B)       r(A,(B-A),c+1);       else       r((A-B),B,c+1);   }   int repeatedSubtraction(int A,int B){       //code here       int c=0;       if(A!=B)       {           if(A==1)           return B;           else if(B==1)           return A;       }       int k=r(A,B,c);       return k;   }};
0
abhishek11102 months ago
Java Solution:
class Solution {
    int repeatedSubtraction(int A,int B){
        //code here
        int count = 0;
         while (Math.min(A, B) > 0) {
            if (A < B) {
                int temp = A;
                A = B;
                B = temp;
            }
            count += A / B;
            A %= B;
        }
        return count;
    }
}
0
devshukla072 months ago
Java
 
Total Time Taken:
3.9/7.4
int repeatedSubtraction(int A,int B){
       //code here
       int step=0;
       while(A>0 && B>0){
           if(A>B){
               A-=B;
               step++;
           }else{
               B-=A;
               step++;
           }
       }
       return step;
   }
+1
sunghunet3 months ago
int repeatedSubtraction(int A, int B){
    
    int step_count=0;
    
    while (true){

        if(A < B) B = B-A;
        else A = A-B;

        step_count++;

        if (A<=0 || B<=0) break;        
    }
    
    return step_count;
}
0
1ashishchauhan20023 months ago
int count=0;while(A>0 && B>0){   if(A>B){       A=A-B;       count++;   }   else{       B=B-A;       count++;   }}return count;   }
+1
shobhitagarwal23093 months ago
class Solution {   public:   int repeatedSubtraction(int A,int B){       //code here       if(A==1) return B;       if(B==1) return A;       if(A==0||B==0){return 0;}             else {       return 1+(A>B? repeatedSubtraction(A-B,B):repeatedSubtraction(A,B-A));          }          }};
0
shaktig1011013 months ago
package string;
public class Greek {int repeatedSubtraction(int A,int B){       //code here int a=A,b=B;       int C=a+b;       int d;       d=C/3;                System.out.println(d);       return d;}
public static void main(String[] args) { Greek obj=new Greek(); obj.repeatedSubtraction(5,13);}
} 
0
chauhanjpr3 months ago
TimeTaken:
1.5/3.4
 
int repeatedSubtraction(int A,int B){       int count = 0;
       if (A > B) {               count = A / B;               if (A % B == 0)                       return count;               else {                       return count + repeatedSubtraction((A - (count * B)), B);               }       } else {               count = B /A;               if (B % A == 0)                       return count;               else {                       return count + repeatedSubtraction((B - (count * A)), A);               }       }   }
+3
nsrinya4405 months ago
	   int repeatedSubtraction(int A,int B){
       int count=0;
       while(A>0 && B>0 ){
           if(A>B){
               A=A-B;
           }else{
               B=B-A;
           }
           count++;
       }
       return count;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.