Sort perfect squares in an array at their relative positions
Given an integer array , the task is to sort only the elements which are perfect squares at their relative positions in the array (positions of other elements must not be affected).
Examples:
Input: arr[] = {2, 64, 9, 8, 1, 4}
Output: 2 1 4 8 9 64
1, 4, 9 and 64 are the only perfect squares from the array.
Input: arr[] = {1, 49, 2, 36}
Output: 1 36 2 49
Approach:
- Initialize two empty vectors and traverse the array from left to right.
- Take an integer and a float variable and for every element of the array store it’s square root in both of these variables.
- If both the variables are equal then push the index of this element in the first vector and push the element itself in the second vector.
- Sort the second vector.
- Now, we have the index of all the required elements in the first vector and also all of the required elements in sorted order in the second vector.
- So, insert the elements of the second vector into the array at the indices present in the first vector one by one.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sortPerfectSquare( int arr[], int n)
{
int a;
float b;
vector< int > v1;
vector< int > v2;
for ( int i = 0; i < n; i++) {
b = sqrt (arr[i]);
a = b;
if (a == b) {
v1.push_back(i);
v2.push_back(arr[i]);
}
}
sort(v2.begin(), v2.end());
int j = 0;
for ( int i = 0; i < n; i++) {
if (v1[j] == i) {
arr[i] = v2[j];
j++;
}
}
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = { 9, 44, 100, 81, 21, 64 };
int n = sizeof (arr) / sizeof (arr[0]);
sortPerfectSquare(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void sortPerfectSquare( int arr[], int n)
{
int a;
float b;
Vector<Integer> v1 = new Vector<Integer>();
Vector<Integer> v2 = new Vector<Integer>();
for ( int i = 0 ; i < n; i++)
{
b = ( float ) Math.sqrt(arr[i]);
a = ( int ) b;
if (a == b)
{
v1.add(i);
v2.add(arr[i]);
}
}
Collections.sort(v2);
int j = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (v1.get(j) == i)
{
arr[i] = v2.get(j);
j++;
}
}
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i]+ " " );
}
public static void main(String[] args)
{
int arr[] = { 9 , 44 , 100 , 81 , 21 , 64 };
int n = arr.length;
sortPerfectSquare(arr, n);
}
}
|
Python3
from math import sqrt
def sortPerfectSquare(arr, n) :
v1 = []
v2 = []
for i in range (n):
b = sqrt(arr[i])
a = int (b)
if a = = b :
v1.append(i)
v2.append(arr[i])
v2.sort()
j = 0
for i in range (n) :
if v1[j] = = i :
arr[i] = v2[j]
j + = 1
for i in range (n) :
print (arr[i], end = " " )
if __name__ = = "__main__" :
arr = [ 9 , 44 , 100 , 81 , 21 , 64 ]
n = len (arr)
sortPerfectSquare(arr, n);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void sortPerfectSquare( int []arr, int n)
{
int a;
float b;
List< int > v1 = new List< int >();
List< int >v2 = new List< int >();
for ( int i = 0; i < n; i++)
{
b = ( float ) Math.Sqrt(arr[i]);
a = ( int ) b;
if (a == b)
{
v1.Add(i);
v2.Add(arr[i]);
}
}
v2.Sort();
int j = 0;
for ( int i = 0; i < n; i++)
{
if (v1[j] == i)
{
arr[i] = v2[j];
j++;
}
}
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
public static void Main(String[] args)
{
int []arr = { 9, 44, 100, 81, 21, 64 };
int n = arr.Length;
sortPerfectSquare(arr, n);
}
}
|
PHP
<?php
function sortPerfectSquare( $arr , $n )
{
$v1 = array ();
$v2 = array ();
for ( $i = 0; $i < $n ; $i ++)
{
$b = sqrt( $arr [ $i ]);
$a = (int) $b ;
if ( $a == $b )
{
array_push ( $v1 , $i );
array_push ( $v2 , $arr [ $i ]);
}
}
sort( $v2 );
$j = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $v1 [ $j ] == $i )
{
$arr [ $i ] = $v2 [ $j ];
$j ++;
}
}
for ( $i = 0; $i < $n ; $i ++)
echo $arr [ $i ] . " " ;
}
$arr = array ( 9, 44, 100, 81, 21, 64 );
$n = count ( $arr );
sortPerfectSquare( $arr , $n );
?>
|
Javascript
<script>
function sortPerfectSquare(arr, n)
{
var a;
var b;
var v1 = [];
var v2 = [];
for ( var i = 0; i < n; i++) {
b = Math.sqrt(arr[i]);
a = parseInt(b);
if (a == b) {
v1.push(i);
v2.push(arr[i]);
}
}
v2.sort((a,b) => a-b)
var j = 0;
for ( var i = 0; i < n; i++) {
if (v1[j] == i) {
arr[i] = v2[j];
j++;
}
}
for ( var i = 0; i < n; i++)
document.write( arr[i] + " " );
}
var arr = [9, 44, 100, 81, 21, 64 ];
var n = arr.length;
sortPerfectSquare(arr, n);
</script>
|
Last Updated :
09 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...