Editorialist: Harsh Savergaonkar
To accept a list of numbers and sort it according to given criteria which were:
- Square numbers must come first and should be sorted
- Non-Square numbers are placed after and should also be sorted
This problem is rather simple and self explanatory and the requirements are that we must
- Find the square terms
- Sort them
- Sort the remaining numbers and place them after the sorted list of squares
- Print the list
First, we need to accept the input and store it according to the necessity. We then need to identify the square terms in the list, hence, the best way to do this is with a for loop, which will check each input number and see if it’s square root is a natural number (that way we can tell if it is a perfect square or not)
Next, the squares need to be brought in the beginning of the list.
We can also check if each number is greater than its preceding number in each of the iterations of the same for loop.
The numbers can first be sorted in ascending order and then the perfect squares can be found and put befor ethe list as and when found, automatically going in correct order
#AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution can be found here.
Tester’s solution can be found here.