Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimize operations till a or b exceeds N by replacing a or b with their sum

  • Last Updated : 01 Nov, 2021

Given three integers a, b, and N. The task is to find minimum addition operations between a and b, such that after applying the operations, either of a or b becomes greater than N. An addition operation is defined as replacing either of a or b with their sum and keeping another one intact.

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: a = 2, b = 3, N = 20
Output: 4
Explanation



  • Adding 2 and 3, 2 + 3 = 5 and replacing 2 with 5,  now a = 5, b = 3
  • Again add a and b 5 + 3 = 8 replace b with 8, now a = 5, b = 8
  • Again add a and b 5 + 8 = 13 replace a with 13. now a = 13, b = 8
  • Again add a and b 13 + 8 = 21 replace b with 21, now a = 13, b = 21  Here, (b>=n) therefore minimum operations required are 4

Input: a = 2, b = 3, N = 5
Output: 1
Explanation: After replacing 2 with 2+3, a becomes 5 and b becomes 3, therefore minimum operations required are 1 

 

Approach: The idea is to add a and b and store their sum in the minimum of a and b, every time until any of the numbers is greater than N. Reason behind it is making the minimum element largest every time, makes their sum high and thereby reducing the number of operations required.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the minimum number
// of operations required
int minOperations(int a, int b, int n)
{
 
    // Store the count of operations
    int count = 0;
 
    while (1) {
 
        // If any value is greater than N
        // return count
        if (n <= a or n <= b) {
            return count;
            break;
        }
        else {
            int sum = a + b;
            if (a < b)
                a = sum;
            else
                b = sum;
        }
        count++;
    }
    return count;
}
 
// Driver code
int main()
{
    int p = 2, q = 3, n = 20;
    cout << minOperations(p, q, n) << "\n";
    return 0;
}

Java




// Java program for the above approach
class GFG {
 
    // Function to print the minimum number
    // of operations required
    public static int minOperations(int a, int b, int n) {
 
        // Store the count of operations
        int count = 0;
 
        while (true) {
 
            // If any value is greater than N
            // return count
            if (n <= a || n <= b) {
                return count;
            } else {
                int sum = a + b;
                if (a < b)
                    a = sum;
                else
                    b = sum;
            }
            count++;
        }
    }
 
    // Driver code
    public static void main(String args[]) {
        int p = 2, q = 3, n = 20;
        System.out.println(minOperations(p, q, n));
    }
}
 
// This code is contributed by saurabh_jaiswal.

Python3




# python program for the above approach
 
# Function to print the minimum number
# of operations required
def minOperations(a, b, n):
 
    # Store the count of operations
    count = 0
 
    while (1):
 
        # If any value is greater than N
        # return count
        if (n <= a or n <= b):
            return count
            break
 
        else:
            sum = a + b
            if (a < b):
                a = sum
            else:
                b = sum
 
        count += 1
 
    return count
 
# Driver code
if __name__ == "__main__":
 
    p = 2
    q = 3
    n = 20
    print(minOperations(p, q, n))
 
    # This code is contributed by rakeshsahni

Javascript




<script>
        // JavaScript Program to implement
        // the above approach
 
        // Function to print the minimum number
        // of operations required
        function minOperations(a, b, n) {
 
            // Store the count of operations
            let count = 0;
 
            while (1) {
 
                // If any value is greater than N
                // return count
                if (n <= a || n <= b) {
                    return count;
                    break;
                }
                else {
                    let sum = a + b;
                    if (a < b)
                        a = sum;
                    else
                        b = sum;
                }
                count++;
            }
            return count;
        }
 
        // Driver code
 
        let p = 2, q = 3, n = 20;
        document.write(minOperations(p, q, n) + "<br>");
 
// This code is contributed by Potta Lokesh
    </script>

C#




// C# program for the above approach
using System;
 
public class GFG {
 
    // Function to print the minimum number
    // of operations required
    public static int minOperations(int a, int b, int n) {
 
        // Store the count of operations
        int count = 0;
 
        while (true) {
 
            // If any value is greater than N
            // return count
            if (n <= a || n <= b) {
                return count;
            } else {
                int sum = a + b;
                if (a < b)
                    a = sum;
                else
                    b = sum;
            }
            count++;
        }
    }
 
    // Driver code
    public static void Main(string []args) {
        int p = 2, q = 3, n = 20;
        Console.WriteLine(minOperations(p, q, n));
    }
}
 
// This code is contributed by AnkThon

 
 

Output
4

 

Time Complexity: O(min(log(max(a, N), log(max(b, N)))
Auxiliary Space: O(1)

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!