Sort the sides of triangle on the basis of increasing area

• Last Updated : 24 Dec, 2021

Given an array arr[] of sides of N triangles, the task is to sort the given sides of triangles on the basis of the increasing order of area.

Examples:

Input: arr[] = {{5, 3, 7}, {15, 20, 4}, {4, 9, 6}, {8, 4, 5}}
Output: {{5, 3, 7}, {8, 4, 5}, {4, 9, 6}, {15, 20, 17}}
Explanation:
Following are the areas of triangle:

• Area of 1st triangle (5, 3, 7) is 6.4.
• Area of 2nd triangle (15, 20, 4) is 124.2.
• Area of 3rd triangle (4, 9, 6) is 9.5.
• Area of 4th triangle (8, 4, 5) is 8.1.

Therefore, ordering them increasing order of the area modifies the given array as 6.4 {5, 3, 7}, 8.1 {8, 4, 5}, 9.5 {4, 9, 6}, 124.2 {15, 20, 4}.

Input: arr[] = {{7, 24, 25}, {5, 12, 13}, {3, 4, 5}}
Output: {{3, 4, 5}, {5, 12, 13}, {7, 24, 25}}

Approach: The given can be solved by storing the sides with the area of the triangle in another array and then sort the array in increasing order of area stored and then print the sides stored in another array as the result.

Below is the implementation of the above approach:

C++

 // C++ program for the above approach #include using namespace std; // Function to rearrange the sides of// triangle in increasing order of areavoid rearrangeTriangle(    vector > arr, int N){    // Stores the area of triangles with    // their corresponding indices    vector > area;     for (int i = 0; i < N; i++) {         // Find the area        float a = (arr[i]                   + arr[i]                   + arr[i])                  / 2.0;        float Area = sqrt(abs(a * (a - arr[i])                              * (a - arr[i])                              * (a - arr[i])));         area.push_back({ Area, i });    }     // Sort the area vector    sort(area.begin(), area.end());     // Resultant sides    for (int i = 0; i < area.size(); i++) {        cout << arr[area[i].second]             << " "             << arr[area[i].second]             << " "             << arr[area[i].second]             << '\n';    }} // Driver Codeint main(){    vector > arr = {        { 5, 3, 7 }, { 15, 20, 4 }, { 4, 9, 6 }, { 8, 4, 5 }    };    int N = arr.size();     rearrangeTriangle(arr, N);     return 0;}

Python3

 # python program for the above approachimport math # Function to rearrange the sides of# triangle in increasing order of areadef rearrangeTriangle(arr, N):         # Stores the area of triangles with        # their corresponding indices    area = []     for i in range(0, N):                 # Find the area        a = (arr[i] + arr[i] + arr[i]) / 2.0        Area = math.sqrt(            abs(a * (a - arr[i]) * (a - arr[i]) * (a - arr[i])))         area.append([Area, i])         # Sort the area vector    area.sort()     # Resultant sides    for i in range(0, len(area)):        print(arr[area[i]], end=" ")        print(arr[area[i]], end=" ")        print(arr[area[i]]) # Driver Codeif __name__ == "__main__":     arr = [        [5, 3, 7], [15, 20, 4], [4, 9, 6], [8, 4, 5]    ]    N = len(arr)     rearrangeTriangle(arr, N)     # This code is contributed by rakeshsahni

C#

 // C# implementation for the above approachusing System;using System.Collections.Generic; class GFG {   // Function to rearrange the sides of  // triangle in increasing order of area  static void rearrangeTriangle(    List > arr, int N)  {         // Stores the area of triangles with    // their corresponding indices    List > area = new List >();     for (int i = 0; i < N; i++) {       // Find the area      float a = (float)(arr[i]                        + arr[i]                        + arr[i])        / 2;      float Area = (float)Math.Sqrt(Math.Abs(a * (a - arr[i])                                             * (a - arr[i])                                             * (a - arr[i])));       area.Add(new KeyValuePair (Area, i ));    }     // Sort the area List    area.Sort((x, y) => x.Key.CompareTo(y.Key));     // Resultant sides    for (int i = 0; i < area.Count; i++) {      Console.WriteLine(arr[area[i].Value] + " "                        + arr[area[i].Value]                        + " "                        + arr[area[i].Value]);    }  }   // Driver Code  static public void Main ()  {    List > arr = new List >(){      new List(){ 5, 3, 7 },      new List(){ 15, 20, 4 },      new List(){ 4, 9, 6 },      new List(){ 8, 4, 5 }    };    int N = arr.Count;     rearrangeTriangle(arr, N);  }} // This code is contributed// by Shubham Singh

Javascript


Output:
5 3 7
8 4 5
4 9 6
15 20 4

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

Space Optimized Approach: The above approach can also be optimized in terms of space, the idea is to use the comparator function to sort the given array in increasing order of area. Below is the comparator function that is used:

Below is the implementation of the above approach:

C++

 // C++ program for the above approach#include using namespace std; // Function to find the area of sides// of triangle stored in arr[]float findArea(vector& arr){     // Find the semi perimeter    float a = (arr               + arr               + arr)              / 2.0;     // Find area using Heron's Formula    float Area = sqrt(abs(a * (a - arr)                          * (a - arr)                          * (a - arr)));     // Return the area    return Area;} // Comparator function to sort the given// array of sides of triangles in// increasing order of areabool cmp(vector& A, vector& B){    return findArea(A) <= findArea(B);} // Function to rearrange the sides of// triangle in increasing order of areavoid rearrangeTriangle(    vector > arr, int N){    // Sort the array arr[] in increasing    // order of area    sort(arr.begin(), arr.end(), cmp);     // Resultant sides    for (int i = 0; i < N; i++) {        cout << arr[i] << " " << arr[i] << " "             << arr[i] << '\n';    }} // Driver Codeint main(){    vector > arr = {        { 5, 3, 7 }, { 15, 20, 4 }, { 4, 9, 6 }, { 8, 4, 5 }    };    int N = arr.size();     rearrangeTriangle(arr, N);     return 0;}

Python3

 # Python program for the above approachimport math # Function to find the area of sides# of triangle stored in arr[]def findArea(arr):     # Find the semi perimeter    a = (arr + arr + arr) / 2.0         # Find area using Heron's Formula    Area = math.sqrt(abs(a * (a - arr) * (a - arr) * (a - arr)))         # Return the area    return Area   # Function to rearrange the sides of# triangle in increasing order of areadef rearrangeTriangle(arr , N):     # Sort the array arr[] in increasing  # order of area  arr.sort(key = lambda x: (findArea(x)))     # Resultant sides  for i in range(0,N):    print(arr[i], arr[i], arr[i])              # Driver Codeif __name__ == "__main__":     arr = [[5 , 3 , 7], [15 , 20 , 4], [4 , 9 , 6], [8 , 4 , 5]]    N = len(arr)         rearrangeTriangle(arr, N)         # This code is contributed by bhupenderyadav18.
Output:
5 3 7
8 4 5
4 9 6
15 20 4

Time Complexity: O(N*log N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up