Given an array arr[] of N integers, the task is to sort the array according to the cubes of each element.
Examples:
Input: arr[] = { 4, -1, 0, -5, 6 }
Output: -5 -1 0 4 6Input: arr[] = { 12, 3, 0, 11 }
Output: 0 3 11 12
Approach: The idea is to use the Comparator function with an inbuilt sort function() to sort the array according to the cubes of its elements. Below is the comparator function used:
bool comparator_function(int a, int b) { x = pow(a, 3); y = pow(b, 3); return x < y; }
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Comparator function which returns // a^3 is less than b^3 bool cmp( int a, int b) { int x = pow (a, 3); int y = pow (b, 3); return x < y; } // Function to sort the cubes of array bool sortArr( int arr[], int n) { // Sort the array sort(arr, arr + n, cmp); // Print the array for ( int i = 0; i < n; i++) { cout << arr[i] << " " ; } } // Driver Code int main() { // Given array int arr[] = { 4, -1, 0, -5, 6 }; int n = sizeof (arr) / sizeof (arr[0]); // Function Call sortArr(arr, n); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG { // Function to sort the cubes of array static void sortArr( int arr[], int n) { Integer[] ar = new Integer[n]; for ( int i = 0 ; i < n; i++) ar[i] = arr[i]; // Sort the array Arrays.sort(ar, new Comparator<Integer>() { public int compare(Integer a, Integer b) { int x = ( int )Math.pow(a, 3 ); int y = ( int )Math.pow(b, 3 ); return (x < y) ? - 1 : 1 ; } }); // Print the array for ( int i = 0 ; i < n; i++) { System.out.print(ar[i] + " " ); } } // Driver code public static void main(String[] args) { // Given array int arr[] = { 4 , - 1 , 0 , - 5 , 6 }; int n = arr.length; // Function Call sortArr(arr, n); } } // This code is contributed by offbeat |
Python3
# Python3 program for the above approach # Function to sort the cubes of array def sortArr(arr, n): # Make a list of tuples in # the form(cube of (num), num) arr = [(i * i * i, i) for i in arr]; # Sort the array according to # the their respective cubes arr.sort() # Print the array for i in range (n): print (arr[i][ 1 ], end = " " ); # Driver Code if __name__ = = "__main__" : # Given array arr = [ 4 , - 1 , 0 , - 5 , 6 ]; n = len (arr); # Function Call sortArr(arr, n); # This code is contributed by AnkitRai01 |
C#
// C# program for the above approach using System; using System.Collections; class compare : IComparer { // Call CaseInsensitiveComparer.Compare public int Compare(Object x, Object y) { return ( new CaseInsensitiveComparer()).Compare(x,y); } } class GFG{ // Function to sort the cubes of array static void sortArr( int []arr, int n) { int [] ar = new int [n]; for ( int i = 0; i < n; i++) ar[i] = arr[i]; IComparer cmp = new compare(); // Sort the array Array.Sort(ar, cmp); // Print the array for ( int i = 0; i < n; i++) { Console.Write(ar[i] + " " ); } } // Driver code public static void Main(String[] args) { // Given array int []arr = {4, -1, 0, -5, 6}; int n = arr.Length; // Function Call sortArr(arr, n); } } // This code is contributed by gauravrajput1 |
Javascript
<script> //Javascript implementation to check whether // K times of a element is present in // the array // Function to sort the cubes of array function sortArr(arr, n) { // Sort the array arr.sort( function ( a , b){ var x = Math.pow(a,3); var y = Math.pow(b,3); if (x > y) return 1; if (x < y) return -1; return 0; }); // Print the array for ( var i = 0; i < n; i++) { document.write(arr[i] + " " ); } } // Driver program to test above var arr = [ 4, -1, 0, -5, 6 ]; var n = arr.length; sortArr(arr, n); // This code is contributed by shivani. </script> |
-5 -1 0 4 6
Time Complexity: O(N*log N), where N is the number of elements in the array.
Space Complexity : O(1) , as it only uses a constant amount of extra memory to sort the array and print the result
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!