Open In App

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: 

Below is the implementation of the given approach. 




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




<script>
 
// Javascript Program to find Minimum
// increment in the sides required
// to get non-negative area of
// a triangle
 
    function minimumIncrease(a , b , c)
    {
        // push the three sides
        // to a array
        var arr = [ a, b, c ];
 
        // sort the array
        arr.sort((a,b)=>a-b);
 
        // 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
     
        var a = 3, b = 5, c = 10;
 
        document.write(minimumIncrease(a, b, c));
 
// This code contributed by aashish1995
 
</script>

Output
2

complexity Analysis:


Article Tags :