Modify array by sorting nearest perfect squares of array elements having their digits sorted in decreasing order
Given an array arr[] of size N (1 ≤ N ≤ 105), the task is to sort digits of each array element in descending order and replace each array element with the nearest perfect square and then print the array in ascending order.
Examples:
Input: arr[ ] = {29, 43, 28, 12}
Output: 25 49 81 100
Explanation:
Sorting digits of each array element in descending order modifies arr[] to {92, 43, 82, 21}.
Replacing elements with the nearest perfect square modifies arr[] to {100, 49, 81, 25}.
Sorting the array in ascending order modifies arr[] to {25, 49, 81, 100}.Input: arr[ ] = {517, 142, 905}
Output: 441 729 961
Approach: Follow the steps below to solve the problem:
- Traverse the array arr[] and perform the following operations on each array element.
- Convert them to equivalent string representation.
- Sort the string in descending order.
- Convert the string back to its equivalent integer.
- Find the nearest perfect square of the current array element.
- Now, sort the array in ascending order.
- Print the sorted array.
Below is the implementation of the above approach.
C++
// C++ program of the above approach #include <bits/stdc++.h> using namespace std; // Function to sort array in ascending order // after replacing array elements by nearest // perfect square of decreasing order of digits void sortArr( int arr[], int N) { // Traverse the array of strings for ( int i = 0; i < N; i++) { // Convert the current array // element to a string string s = to_string(arr[i]); // Sort each string in descending order sort(s.begin(), s.end(), greater< char >()); // Convert the string to // equivalent integer arr[i] = stoi(s); // Calculate square root of // current array element int sr = sqrt (arr[i]); // Calculate perfect square int a = sr * sr; int b = (sr + 1) * (sr + 1); // Find the nearest perfect square if ((arr[i] - a) < (b - arr[i])) arr[i] = a; else arr[i] = b; } // Sort the array in ascending order sort(arr, arr + N); // Print the array for ( int i = 0; i < N; i++) { cout << arr[i] << " " ; } } // Driver Code int main() { int arr[] = { 29, 43, 28, 12 }; int N = sizeof (arr) / sizeof (arr[0]); sortArr(arr, N); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ static void reverse( char [] a) { int i, n = a.length; char t; for (i = 0 ; i < n / 2 ; i++) { t = a[i]; a[i] = a[n - i - 1 ]; a[n - i - 1 ] = t; } } // Function to sort array in ascending order // after replacing array elements by nearest // perfect square of decreasing order of digits static void sortArr( int arr[], int N) { // Traverse the array of strings for ( int i = 0 ; i < N; i++) { // Convert the current array // element to a string String s = Integer.toString(arr[i]); char [] str = s.toCharArray(); // Sort each string in descending order Arrays.sort(str); reverse(str); String string = new String(str); // Convert the string to // equivalent integer arr[i] = Integer.parseInt(string); // Calculate square root of // current array element int sr = ( int )Math.sqrt(arr[i]); // Calculate perfect square int a = sr * sr; int b = (sr + 1 ) * (sr + 1 ); // Find the nearest perfect square if ((arr[i] - a) < (b - arr[i])) arr[i] = a; else arr[i] = b; } // Sort the array in ascending order Arrays.sort(arr); // Print the array for ( int i = 0 ; i < N; i++) { System.out.print(arr[i] + " " ); } } // Driver Code public static void main(String[] args) { int arr[] = { 29 , 43 , 28 , 12 }; int N = arr.length; sortArr(arr, N); } } // This code is contributed by sanjoy_62. |
Python3
# Python 3 program of the above approach import math # Function to sort array in ascending order # after replacing array elements by nearest # perfect square of decreasing order of digits def sortArr(arr, N): # Traverse the array of strings for i in range (N): # Convert the current array # element to a string s = str (arr[i]) # Sort each string in descending order list1 = list (s) list1.sort(reverse = True ) s = ''.join(list1) # Convert the string to # equivalent integer arr[i] = int (s) # Calculate square root of # current array element sr = int (math.sqrt(arr[i])) # Calculate perfect square a = sr * sr b = (sr + 1 ) * (sr + 1 ) # Find the nearest perfect square if ((arr[i] - a) < (b - arr[i])): arr[i] = a else : arr[i] = b # Sort the array in ascending order arr.sort() # Print the array for i in range (N): print (arr[i], end = " " ) # Driver Code if __name__ = = "__main__" : arr = [ 29 , 43 , 28 , 12 ] N = len (arr) sortArr(arr, N) # This code is contributed by ukasp. |
C#
// C# program of the above approach using System; using System.Collections.Generic; class GFG{ // Function to sort array in ascending order // after replacing array elements by nearest // perfect square of decreasing order of digits static void sortArr( int []arr, int N) { // Traverse the array of strings for ( int i = 0; i < N; i++) { // Convert the current array // element to a string int num = arr[i]; string s = num.ToString(); // Sort each string in descending order char []temp = s.ToCharArray(); Array.Sort(temp); int st = 0; int ed = temp.Length-1; while (st<ed){ char ch = temp[st]; temp[st] = temp[ed]; temp[ed] = ch; st++; ed--; } string charsStr = new string (temp); // sort(s.begin(), s.end(), greater<char>()); // Convert the string to // equivalent integer arr[i] = Int32.Parse(charsStr); // Calculate square root of // current array element int sr = ( int )Math.Sqrt(arr[i]); // Calculate perfect square int a = sr * sr; int b = (sr + 1) * (sr + 1); // Find the nearest perfect square if ((arr[i] - a) < (b - arr[i])) arr[i] = a; else arr[i] = b; } // Sort the array in ascending order Array.Sort(arr); // Print the array for ( int i = 0; i < N; i++) { Console.Write(arr[i]+ " " ); } } // Driver Code public static void Main() { int []arr = { 29, 43, 28, 12 }; int N = arr.Length; sortArr(arr, N); } } // This code is contributed by ipg2016107. |
Javascript
<script> // JavaScript program of the above approach // Function to sort array in ascending order // after replacing array elements by nearest // perfect square of decreasing order of digits function sortArr(arr, N) { // Traverse the array of strings for ( var i = 0; i < N; i++) { // Convert the current array // element to a string var num = arr[i]; var s = num.toString(); // Sort each string in descending order var temp = s.split( "" ); temp.sort((a, b) => b - a); s = temp.join( "" ); // Convert the string to // equivalent integer arr[i] = parseInt(s); // Calculate square root of // current array element var sr = parseInt(Math.sqrt(arr[i])); // Calculate perfect square var a = sr * sr; var b = (sr + 1) * (sr + 1); // Find the nearest perfect square if (arr[i] - a < b - arr[i]) arr[i] = a; else arr[i] = b; } // Sort the array in ascending order arr.sort((a, b) => a - b); // Print the array for ( var i = 0; i < N; i++) { document.write(arr[i] + " " ); } } // Driver Code var arr = [29, 43, 28, 12]; var N = arr.length; sortArr(arr, N); </script> |
25 49 81 100
Time Complexity: O(N*logN+N*M*logM) where N is the size of the array and M is the maximum number of digits present in any element of the array.
Auxiliary Space: O(1)
Please Login to comment...