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++

`// C++ program to sort all the elements that are ` `// perfect squares in their relative positions ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to sort all the elements that are ` `// perfect squares in their relative positions ` `void` `sortPerfectSquare(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `a; ` ` ` `float` `b; ` ` ` ` ` `// v1 will contain index of perfect squares ` ` ` `// v2 will contain each perfect square ` ` ` `vector<` `int` `> v1; ` ` ` `vector<` `int` `> v2; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `b = ` `sqrt` `(arr[i]); ` ` ` `a = b; ` ` ` ` ` `// if both a and b are equal then ` ` ` `// arr[i] is a perfect square ` ` ` `if` `(a == b) { ` ` ` `v1.push_back(i); ` ` ` `v2.push_back(arr[i]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// sort second vector ` ` ` `sort(v2.begin(), v2.end()); ` ` ` ` ` `// put the sorted perfect square ` ` ` `// back into the array ` ` ` `int` `j = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(v1[j] == i) { ` ` ` `arr[i] = v2[j]; ` ` ` `j++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// print final array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `cout << arr[i] << ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 9, 44, 100, 81, 21, 64 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `sortPerfectSquare(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to sort all ` `# the elements that are perfect ` `# squares in their relative positions ` ` ` `# import sqrt() from math lib ` `from` `math ` `import` `sqrt ` ` ` `# function to sort all the elements ` `# that are perfect squares in their ` `# relative positions ` `def` `sortPerfectSquare(arr, n) : ` ` ` ` ` `# v1 will contain index of ` ` ` `# perfect squares and v2 will ` ` ` `# contain each perfect square ` ` ` `v1 ` `=` `[] ` ` ` `v2 ` `=` `[] ` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `b ` `=` `sqrt(arr[i]) ` ` ` `a ` `=` `int` `(b) ` ` ` ` ` `# if both a and b are equal then ` ` ` `# arr[i] is a perfect square ` ` ` `if` `a ` `=` `=` `b : ` ` ` `v1.append(i) ` ` ` `v2.append(arr[i]) ` ` ` ` ` `# sort second list ` ` ` `v2.sort() ` ` ` ` ` `j ` `=` `0` ` ` ` ` `# put the sorted perfect square ` ` ` `# back into the array ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `if` `v1[j] ` `=` `=` `i : ` ` ` `arr[i] ` `=` `v2[j] ` ` ` `j ` `+` `=` `1` ` ` ` ` `# print final array ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `print` `(arr[i], end ` `=` `" "` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `arr ` `=` `[` `9` `, ` `44` `, ` `100` `, ` `81` `, ` `21` `, ` `64` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` ` ` `sortPerfectSquare(arr, n); ` ` ` `# This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

9 44 64 81 21 100

## Recommended Posts:

- Check if the sum of perfect squares in an array is divisible by x
- Largest sub-array whose all elements are perfect squares
- Positive elements at even and negative at odd positions (Relative order not maintained)
- Sort array after converting elements to their squares
- Number of perfect squares between two given numbers
- Count number less than N which are product of perfect squares
- Print the last occurrence of elements in array in relative order
- Find first and last positions of an element in a sorted array
- Count numbers upto N which are both perfect square and perfect cube
- Check whether the given array is perfect or not
- Largest number in an array that is not a perfect cube
- Largest perfect square number in an Array
- Largest perfect cube number in an Array
- Count of pairs in an array whose sum is a perfect square
- Smallest perfect square divisible by all elements of an array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.