Modify array by sorting nearest perfect squares of array elements having their digits sorted in decreasing order
Last Updated :
11 Jul, 2022
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:
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void sortArr( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
string s = to_string(arr[i]);
sort(s.begin(), s.end(), greater< char >());
arr[i] = stoi(s);
int sr = sqrt (arr[i]);
int a = sr * sr;
int b = (sr + 1) * (sr + 1);
if ((arr[i] - a) < (b - arr[i]))
arr[i] = a;
else
arr[i] = b;
}
sort(arr, arr + N);
for ( int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 29, 43, 28, 12 };
int N = sizeof (arr) / sizeof (arr[0]);
sortArr(arr, N);
return 0;
}
|
Java
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;
}
}
static void sortArr( int arr[], int N)
{
for ( int i = 0 ; i < N; i++) {
String s = Integer.toString(arr[i]);
char [] str = s.toCharArray();
Arrays.sort(str);
reverse(str);
String string = new String(str);
arr[i] = Integer.parseInt(string);
int sr = ( int )Math.sqrt(arr[i]);
int a = sr * sr;
int b = (sr + 1 ) * (sr + 1 );
if ((arr[i] - a) < (b - arr[i]))
arr[i] = a;
else
arr[i] = b;
}
Arrays.sort(arr);
for ( int i = 0 ; i < N; i++) {
System.out.print(arr[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 29 , 43 , 28 , 12 };
int N = arr.length;
sortArr(arr, N);
}
}
|
Python3
import math
def sortArr(arr, N):
for i in range (N):
s = str (arr[i])
list1 = list (s)
list1.sort(reverse = True )
s = ''.join(list1)
arr[i] = int (s)
sr = int (math.sqrt(arr[i]))
a = sr * sr
b = (sr + 1 ) * (sr + 1 )
if ((arr[i] - a) < (b - arr[i])):
arr[i] = a
else :
arr[i] = b
arr.sort()
for i in range (N):
print (arr[i], end = " " )
if __name__ = = "__main__" :
arr = [ 29 , 43 , 28 , 12 ]
N = len (arr)
sortArr(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void sortArr( int []arr, int N)
{
for ( int i = 0; i < N; i++) {
int num = arr[i];
string s = num.ToString();
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);
arr[i] = Int32.Parse(charsStr);
int sr = ( int )Math.Sqrt(arr[i]);
int a = sr * sr;
int b = (sr + 1) * (sr + 1);
if ((arr[i] - a) < (b - arr[i]))
arr[i] = a;
else
arr[i] = b;
}
Array.Sort(arr);
for ( int i = 0; i < N; i++) {
Console.Write(arr[i]+ " " );
}
}
public static void Main()
{
int []arr = { 29, 43, 28, 12 };
int N = arr.Length;
sortArr(arr, N);
}
}
|
Javascript
<script>
function sortArr(arr, N) {
for ( var i = 0; i < N; i++) {
var num = arr[i];
var s = num.toString();
var temp = s.split( "" );
temp.sort((a, b) => b - a);
s = temp.join( "" );
arr[i] = parseInt(s);
var sr = parseInt(Math.sqrt(arr[i]));
var a = sr * sr;
var b = (sr + 1) * (sr + 1);
if (arr[i] - a < b - arr[i]) arr[i] = a;
else arr[i] = b;
}
arr.sort((a, b) => a - b);
for ( var i = 0; i < N; i++) {
document.write(arr[i] + " " );
}
}
var arr = [29, 43, 28, 12];
var N = arr.length;
sortArr(arr, N);
</script>
|
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...