Minimize operations till a or b exceeds N by replacing a or b with their sum
Last Updated :
28 Jul, 2022
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:
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++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int a, int b, int n)
{
int count = 0;
while (1) {
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;
}
int main()
{
int p = 2, q = 3, n = 20;
cout << minOperations(p, q, n) << "\n" ;
return 0;
}
|
C
#include <stdio.h>
int minOperations( int a, int b, int n)
{
int count = 0;
while (1) {
if (n <= a || n <= b) {
return count;
break ;
}
else {
int sum = a + b;
if (a < b)
a = sum;
else
b = sum;
}
count++;
}
return count;
}
int main()
{
int p = 2, q = 3, n = 20;
printf ( "%d\n" , minOperations(p, q, n));
return 0;
}
|
Java
class GFG {
public static int minOperations( int a, int b, int n) {
int count = 0 ;
while ( true ) {
if (n <= a || n <= b) {
return count;
} else {
int sum = a + b;
if (a < b)
a = sum;
else
b = sum;
}
count++;
}
}
public static void main(String args[]) {
int p = 2 , q = 3 , n = 20 ;
System.out.println(minOperations(p, q, n));
}
}
|
Python3
def minOperations(a, b, n):
count = 0
while ( 1 ):
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
if __name__ = = "__main__" :
p = 2
q = 3
n = 20
print (minOperations(p, q, n))
|
Javascript
<script>
function minOperations(a, b, n) {
let count = 0;
while (1) {
if (n <= a || n <= b) {
return count;
break ;
}
else {
let sum = a + b;
if (a < b)
a = sum;
else
b = sum;
}
count++;
}
return count;
}
let p = 2, q = 3, n = 20;
document.write(minOperations(p, q, n) + "<br>" );
</script>
|
C#
using System;
public class GFG {
public static int minOperations( int a, int b, int n) {
int count = 0;
while ( true ) {
if (n <= a || n <= b) {
return count;
} else {
int sum = a + b;
if (a < b)
a = sum;
else
b = sum;
}
count++;
}
}
public static void Main( string []args) {
int p = 2, q = 3, n = 20;
Console.WriteLine(minOperations(p, q, n));
}
}
|
Time Complexity: O(min(log(max(a, N), log(max(b, N)))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...