
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Minutes to Increase Stick Length for Triangle in C++
Suppose we have three numbers a, b and c. There are three sticks whose lengths are a, b and c. In one minute, we can pick one arbitrary stick and increase its length by 1 cm, but we cannot break sticks. We have to count the minimum number of minutes needed to increase their lengths and we can form a triangle with them.
So, if the input is like a = 2; b = 3; c = 5, then the output will be 1, because by increasing any one of a or b by 1, we can make a triangle (a + b) > c. So one minute is sufficient.
Steps
To solve this, we will follow these steps −
Define an array A = { a, b, c } sort the array A return maximum of (A[2] - A[1] - A[0] + 1) and 0
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int a, int b, int c) { vector<int> A = { a, b, c }; sort(A.begin(), A.end()); return max(A[2] - A[1] - A[0] + 1, 0); } int main() { int a = 2; int b = 3; int c = 5; cout << solve(a, b, c) << endl; }
Input
2, 3, 5
Output
1
Advertisements