A normal solution is to sort the elements and sort the n numbers and start checking from back for a non perfect square number using sqrt() function. The first number from the end which is not a perfect square number is our answer. The complexity of sorting is O(n log n) and of sqrt() function is log n, so at the worst case the complexity is O(n log n log n).
The efficient solution is to iterate for all the elements using traversal in O(n) and compare every time with the maximum element, and store the maximum of all non perfect squares. The number stored in maximum will be our answer.
Below is the illustration of the above approach:
// CPP program to find the largest non perfect
// square number among n numbers
// takes the sqrt of the number
intd = sqrt(n);
// checks if it is a perfect square number
if(d * d == n)
// function to find the largest non perfect square number
// stores the maximum of all non perfect square numbers