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 <bits/stdc++.h> using namespace std; // Function to rearrange the sides of // triangle in increasing order of area void rearrangeTriangle( vector<vector< int > > arr, int N) { // Stores the area of triangles with // their corresponding indices vector<pair< float , int > > area; for ( int i = 0; i < N; i++) { // Find the area float a = (arr[i][0] + arr[i][1] + arr[i][2]) / 2.0; float Area = sqrt ( abs (a * (a - arr[i][0]) * (a - arr[i][1]) * (a - arr[i][2]))); 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][0] << " " << arr[area[i].second][1] << " " << arr[area[i].second][2] << '\n' ; } } // Driver Code int main() { vector<vector< int > > arr = { { 5, 3, 7 }, { 15, 20, 4 }, { 4, 9, 6 }, { 8, 4, 5 } }; int N = arr.size(); rearrangeTriangle(arr, N); return 0; } |
Java
// Java implementation for the above approach import java.util.List; import java.util.ArrayList; import java.util.Collections; public class Main { // Function to rearrange the sides of // triangle in increasing order of area static void rearrangeTriangle(List<List<Integer>> arr, int N) { // Stores the area of triangles with // their corresponding indices List<Pair<Float, Integer>> area = new ArrayList<>(); for ( int i = 0 ; i < N; i++) { // Find the area float a = ( float )(arr.get(i).get( 0 ) + arr.get(i).get( 1 ) + arr.get(i).get( 2 )) / 2 ; float Area = ( float ) Math.sqrt(Math.abs(a * (a - arr.get(i).get( 0 )) * (a - arr.get(i).get( 1 )) * (a - arr.get(i).get( 2 )))); area.add( new Pair<>(Area, i)); } // Sort the area List Collections.sort(area, (x, y) -> Float.compare(x.first, y.first)); // Resultant sides for ( int i = 0 ; i < area.size(); i++) { System.out.println(arr.get(area.get(i).second).get( 0 ) + " " + arr.get(area.get(i).second).get( 1 ) + " " + arr.get(area.get(i).second).get( 2 )); } } // Driver Code public static void main(String[] args) { List<List<Integer>> arr = new ArrayList<>(){{ add( new ArrayList<>(){{ add( 5 ); add( 3 ); add( 7 ); }}); add( new ArrayList<>(){{ add( 15 ); add( 20 ); add( 4 ); }}); add( new ArrayList<>(){{ add( 4 ); add( 9 ); add( 6 ); }}); add( new ArrayList<>(){{ add( 8 ); add( 4 ); add( 5 ); }}); }}; int N = arr.size(); rearrangeTriangle(arr, N); } } class Pair<F, S> { public F first; public S second; public Pair(F first, S second) { this .first = first; this .second = second; } } // This code is contributed by Harshad |
Python3
# python program for the above approach import math # Function to rearrange the sides of # triangle in increasing order of area def 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][ 0 ] + arr[i][ 1 ] + arr[i][ 2 ]) / 2.0 Area = math.sqrt( abs (a * (a - arr[i][ 0 ]) * (a - arr[i][ 1 ]) * (a - arr[i][ 2 ]))) area.append([Area, i]) # Sort the area vector area.sort() # Resultant sides for i in range ( 0 , len (area)): print (arr[area[i][ 1 ]][ 0 ], end = " " ) print (arr[area[i][ 1 ]][ 1 ], end = " " ) print (arr[area[i][ 1 ]][ 2 ]) # Driver Code if __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 approach using System; using System.Collections.Generic; class GFG { // Function to rearrange the sides of // triangle in increasing order of area static void rearrangeTriangle( List<List< int > > arr, int N) { // Stores the area of triangles with // their corresponding indices List<KeyValuePair< float , int > > area = new List<KeyValuePair< float , int > >(); for ( int i = 0; i < N; i++) { // Find the area float a = ( float )(arr[i][0] + arr[i][1] + arr[i][2]) / 2; float Area = ( float )Math.Sqrt(Math.Abs(a * (a - arr[i][0]) * (a - arr[i][1]) * (a - arr[i][2]))); area.Add( new KeyValuePair < float , int >(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][0] + " " + arr[area[i].Value][1] + " " + arr[area[i].Value][2]); } } // Driver Code static public void Main () { List<List< int > > arr = new List<List< int > >(){ new List< int >(){ 5, 3, 7 }, new List< int >(){ 15, 20, 4 }, new List< int >(){ 4, 9, 6 }, new List< int >(){ 8, 4, 5 } }; int N = arr.Count; rearrangeTriangle(arr, N); } } // This code is contributed // by Shubham Singh |
Javascript
<script> // JavaScript Program to implement // the above approach // Function to rearrange the sides of // triangle in increasing order of area function rearrangeTriangle( arr, N) { // Stores the area of triangles with // their corresponding indices let area = []; for (let i = 0; i < N; i++) { // Find the area let a = (arr[i][0] + arr[i][1] + arr[i][2]) / 2.0; let Area = Math.sqrt(Math.abs(a * (a - arr[i][0]) * (a - arr[i][1]) * (a - arr[i][2]))); area.push({ first: parseInt(Area), second: parseInt(i) }); } // Sort the area vector area.sort( function (a, b) { return a.first - b.first; }) // Resultant sides for (let i = 0; i < area.length; i++) { document.write(arr[area[i].second][0] + " " + arr[area[i].second][1] + " " + arr[area[i].second][2] + '<br>' ) } } // Driver Code let arr = [ [5, 3, 7], [15, 20, 4], [4, 9, 6], [8, 4, 5] ]; let N = arr.length; rearrangeTriangle(arr, N); // This code is contributed by Potta Lokesh </script> |
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 <bits/stdc++.h> using namespace std; // Function to find the area of sides // of triangle stored in arr[] float findArea(vector< int >& arr) { // Find the semi perimeter float a = (arr[0] + arr[1] + arr[2]) / 2.0; // Find area using Heron's Formula float Area = sqrt ( abs (a * (a - arr[0]) * (a - arr[1]) * (a - arr[2]))); // Return the area return Area; } // Comparator function to sort the given // array of sides of triangles in // increasing order of area bool cmp(vector< int >& A, vector< int >& B) { return findArea(A) <= findArea(B); } // Function to rearrange the sides of // triangle in increasing order of area void rearrangeTriangle( vector<vector< int > > 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][0] << " " << arr[i][1] << " " << arr[i][2] << '\n' ; } } // Driver Code int main() { vector<vector< int > > arr = { { 5, 3, 7 }, { 15, 20, 4 }, { 4, 9, 6 }, { 8, 4, 5 } }; int N = arr.size(); rearrangeTriangle(arr, N); return 0; } |
Java
// Java Program to implement the above approach import java.io.*; import java.util.*; class GFG { static double findArea( int [] arr) { // Find the semi perimeter double a = (arr[ 0 ] + arr[ 1 ] + arr[ 2 ]) / 2.0 ; // Find area using Heron's Formula double area = Math.sqrt( Math.abs(a * (a - arr[ 0 ]) * (a - arr[ 1 ]) * (a - arr[ 2 ]))); // Return the area return area; } static int [][] rearrangeTriangle( int [][] arr) { // Sort the array arr[] in increasing order of area Arrays.sort(arr, (a, b) -> Double.compare(findArea(a), findArea(b))); return arr; } public static void main(String[] args) { int [][] arr = { { 5 , 3 , 7 }, { 15 , 20 , 4 }, { 4 , 9 , 6 }, { 8 , 4 , 5 } }; int [][] result = rearrangeTriangle(arr); for ( int i = 0 ; i < result.length; i++) { System.out.println(result[i][ 0 ] + " " + result[i][ 1 ] + " " + result[i][ 2 ]); } } } // This code is contributed by lokeshmvs21. |
Python3
# Python program for the above approach import math # Function to find the area of sides # of triangle stored in arr[] def findArea(arr): # Find the semi perimeter a = (arr[ 0 ] + arr[ 1 ] + arr[ 2 ]) / 2.0 # Find area using Heron's Formula Area = math.sqrt( abs (a * (a - arr[ 0 ]) * (a - arr[ 1 ]) * (a - arr[ 2 ]))) # Return the area return Area # Function to rearrange the sides of # triangle in increasing order of area def 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][ 0 ], arr[i][ 1 ], arr[i][ 2 ]) # Driver Code if __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. |
C#
using System; using System.Linq; using System.Collections.Generic; class GFG { static float FindArea(List< int > arr) { // Find the semi perimeter float a = (arr[0] + arr[1] + arr[2]) / 2.0f; // Find area using Heron's Formula float Area = ( float )Math.Sqrt(Math.Abs(a * (a - arr[0]) * (a - arr[1]) * (a - arr[2]))); // Return the area return Area; } static int Compare(List< int > A, List< int > B) { float areaA = FindArea(A); float areaB = FindArea(B); if (areaA < areaB) { return -1; } else if (areaA > areaB) { return 1; } else { return 0; } } static void RearrangeTriangle(List<List< int >> arr, int N) { // Sort the array arr[] in increasing order of area arr.Sort(Compare); // Resultant sides for ( int i = 0; i < N; i++) { Console.Write( "{0} {1} {2}" , arr[i][0], arr[i][1], arr[i][2]+ "\n" ); } } static void Main() { List<List< int >> arr = new List<List< int >> { new List< int > { 5, 3, 7 }, new List< int > { 15, 20, 4 }, new List< int > { 4, 9, 6 }, new List< int > { 8, 4, 5 } }; int N = arr.Count(); RearrangeTriangle(arr, N); } } |
Javascript
<script> // JavaScript Program to implement // the above approach // Function to find the area of sides // of triangle stored in arr[] function findArea(arr) { // Find the semi perimeter let a = (arr[0] + arr[1] + arr[2]) / 2.0; // Find area using Heron's Formula let Area = Math.sqrt(Math.abs(a * (a - arr[0]) * (a - arr[1])* (a - arr[2]))); // Return the area return Area; } // Comparator function to sort the given // array of sides of triangles in // increasing order of area function cmp(A, B) { return findArea(A) - findArea(B); } // Function to rearrange the sides of // triangle in increasing order of area function rearrangeTriangle(arr, N) { // Sort the array arr[] in increasing // order of area arr.sort( function (a, b) { return cmp(a,b); }) // Resultant sides for (let i = 0; i < N; i++) { document.write(arr[i][0] + " " + arr[i][1] + " " + arr[i][2] + '<br>' ) } } // Driver Code let arr = [ [5, 3, 7], [15, 20, 4], [4, 9, 6], [8, 4, 5] ]; let N = arr.length; rearrangeTriangle(arr, N); // This code is contributed by Pushpesh Raj </script> |
5 3 7 8 4 5 4 9 6 15 20 4
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!