Given three sides of the triangle, find the minimum increase in the length of the side of the triangle to make the area of the triangle non-negative.
Examples:
Input: a = 3, b = 4, c = 10
Output: 3
With the given sides, the area is negative.
If a is increased to 5 and b to 5, then the area becomes 0, which is not negative.Input: a = 6, b = 6, c = 10
Output: 2
Approach: Since the area of any triangle is non-negative if the sum of the smallest two sides is always greater than or equal to the third side, hence the following steps should be followed to solve the above problem:
- Sort the three sides in increasing order.
- Check if the sum of the first two sides is greater than or equal to the third side, if it is, then the answer is 0.
- If it is not, then the answer will be (third side – (first side + second side)).
Below is the implementation of the given approach.
C++
// C++ program to find Minimum // increase in sides to get // non-negative area of a triangle #include <bits/stdc++.h> using namespace std; // Function to return the minimum increase in side // lengths of the triangle int minimumIncrease(int a, int b, int c) { // push the three sides to a array int arr[] = { a, b, c }; // sort the array sort(arr, arr + 3); // check if sum is greater than third side if (arr[0] + arr[1] >= arr[2]) return 0; else return arr[2] - (arr[0] + arr[1]); } // Driver Code int main() { int a = 3, b = 5, c = 10; cout << minimumIncrease(a, b, c); return 0; } |
Java
// Java Program to find Minimum // increment in the sides required // to get non-negative area of // a triangle import java.util.*; class GFG { static int minimumIncrease(int a, int b, int c) { // push the three sides // to a array int arr[] = { a, b, c }; // sort the array Arrays.sort(arr); // check if sum is greater // than third side if (arr[0] + arr[1] >= arr[2]) return 0; else return arr[2] - (arr[0] + arr[1]); } // Driver Code public static void main (String[] args) { int a = 3, b = 5, c = 10; System.out.println(minimumIncrease(a, b, c)); } } // This code is contributed // by Shashank |
Python 3
# Python program to find Minimum # increase in sides to get # non-negative area of a triangle # Function to return the # minimum increase in side # lengths of the triangle def minimumIncrease(a, b, c) : # push the three sides # to a array arr = [ a, b, c ] # sort the array arr.sort() # check if sum is greater # than third side if arr[0] + arr[1] >= arr[2] : return 0 else : return arr[2] - (arr[0] + arr[1]) # Driver code if __name__ == "__main__" : a, b, c = 3, 5, 10 print(minimumIncrease(a, b, c)) # This code is contributed # by ANKITRAI1 |
C#
// C# Program to find Minimum // increment in the sides required // to get non-negative area of // a triangle using System; class GFG { static int minimumIncrease(int a, int b, int c) { // push the three sides // to a array int[] arr = { a, b, c }; // sort the array Array.Sort(arr); // check if sum is greater // than third side if (arr[0] + arr[1] >= arr[2]) return 0; else return arr[2] - (arr[0] + arr[1]); } // Driver Code public static void Main () { int a = 3, b = 5, c = 10; Console.Write(minimumIncrease(a, b, c)); } } // This code is contributed // by ChitraNayal |
PHP
<?php // PHP program to find Minimum // increase in sides to get // non-negative area of a triangle // Function to return the // minimum increase in side // lengths of the triangle function minimumIncrease($a, $b, $c) { // push the three sides to a array $arr = array($a, $b, $c ); // sort the array sort($arr); // check if sum is greater // than third side if ($arr[0] + $arr[1] >= $arr[2]) return 0; else return $arr[2] - ($arr[0] + $arr[1]); } // Driver Code $a = 3; $b = 5; $c = 10; echo minimumIncrease($a, $b, $c); // This code is contributed // by ChitraNayal ?> |
2
Recommended Posts:
- Find subarray with given sum | Set 1 (Nonnegative Numbers)
- Count of minimum reductions required to get the required sum K
- Minimum increment or decrement operations required to make the array sorted
- Minimum increment or decrement required to sort the array | Top-down Approach
- Minimum count of increment of K size subarrays required to form a given Array
- Minimum increment/decrement operations required to make Median as X
- Minimum increment/decrement operations required on Array to satisfy given conditions
- Maximum area rectangle by picking four sides from array
- Minimum side of square embedded in Regular polygon with N sides
- Minimum concatenation required to get strictly LIS for the given array
- Minimum concatenation required to get strictly LIS for array with repetitive elements | Set-2
- Minimum number of increment/decrement operations such that array contains all elements from 1 to N
- Minimum number of increment-other operations to make all array elements equal.
- Minimum increment by k operations to make all elements equal
- Making elements of two arrays same with minimum increment/decrement
- Minimum increment operations to make the array in increasing order
- Minimum Increment / decrement to make array elements equal
- Minimum range increment operations to Sort an array
- Minimum increment operations to make K elements equal
- Minimum Increment operations to make Array unique
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


