
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Make three numbers Zero in C++
Let us suppose we have three numbers. The task is to count the total number of optimal steps to make all these numbers '0'.
For Example
Input-1:
a = 4 b = 4c = 6
Output:
7
Explanation: Total number of optimal steps to make all the numbers '0' is,
(4, 4, 6)
Removing '1' from 1st and 2nd number = (3, 3, 6)
Removing '1' from 1st and 3rd number = (2, 3, 5)
Removing '1' from 1st and 3rd number = (1 ,3, 4)
Removing '1' from 1st and 3rd number = (0 ,3 ,3)
Removing '1' from 2nd and 3rd number = (0 ,2, 2)
Removing '1' from 2nd and 3rd number = (0, 1, 1)
Removing '1' from 2nd and 3rd number = (0, 0, 0)
Thus, the total number of steps to make all the numbers zero is '7'
Approach to Solve this Problem
To solve this particular problem, we will remove '1' from any two numbers such that the sum of these two numbers is greater than the last one. To find the minimum steps to make it zero, we will calculate the minimum number of steps.
- Take three numbers as the input.
- Check if the sum of two numbers, let's say 'a' and 'b' is greater than 'c', and a > 0, b > 0, then remove '1' from 'a' and 'b'.
- Find the minimum from the answers and return the result.
Example
#include <bits/stdc++.h> using namespace std; int maxSteps(int a, int b, int c) { int res = 0; while (a + b > c and a > 0 and b > 0) { a--; b--; res++; } res += min(c, a + b); return res; } int main() { int a = 4; int b = 4; int c = 6; cout << maxSteps(a, b, c) << endl; return 0; }
Running the above code will generate the output as,
Output
7
In the given input a = 4, b = 4 and c = 6, it will take seven steps to make all the numbers zero, thus the program returns the output as 7.
- Related Articles
- Make three numbers Zero in Python
- Minimum operations to make XOR of array zero in C++
- Maximum Product of Three Numbers in C++
- Reorder Routes to Make All Paths Lead to the City Zero in C++
- Zero Initialization in C++
- Program to find the common ratio of three numbers in C++
- Largest N digit number divisible by given three numbers in C++
- How many three-digit numbers can you make with 0,5,9 by using each digit once?
- C# program to find the maximum of three numbers
- C++ Program to Find Largest Number Among Three Numbers
- Count the numbers that can be reduced to zero or less in a gamein C++
- Count numbers in range that are divisible by all of its non-zero digits in C++
- C++ Program Probability for three randomly chosen numbers to be in AP
- How to change the negative numbers to zero in Excel?
- C program to Find the Largest Number Among Three Numbers
