Minimum increment in the sides required to get non-negative area of a triangle

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.



Improved By : Shashank12, Ita_c, AnkitRai01