Modify array by sorting nearest perfect squares of array elements having their digits sorted in decreasing order | C # 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 ; Traverse the array of strings ; Convert the current array element to a string ; Sort each string in descending order ; Convert the string to equivalent integer ; Calculate square root of current array element ; Calculate perfect square ; Find the nearest perfect square ; Sort the array in ascending order ; Print the array ; Driver Code
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 ) ; sort ( s . begin ( ) , s . end ( ) , greater < char > ( ) ) ; 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 ) ; } }