# Largest number that is not a perfect square

Given n integers, find the largest number is not a perfect square. Print -1 if there is no number that is perfect square.

Examples:

```Input : arr[] = {16, 20, 25, 2, 3, 10|
Output : 20
Explanation: 20 is the largest number
that is not a perfect square

Input : arr[] = {36, 64, 10, 16, 29, 25|
Output : 29
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

 `// CPP program to find the largest non perfect  ` `// square number among n numbers ` `#include ` `using` `namespace` `std; ` `bool` `check(``int` `n) ` `{ ` `    ``// takes the sqrt of the number ` `    ``int` `d = ``sqrt``(n); ` ` `  `    ``// checks if it is a perfect square number ` `    ``if` `(d * d == n) ` `        ``return` `true``; ` ` `  `    ``return` `false``; ` `} ` ` `  `// fucntion to find the largest non perfect square number ` `int` `largestNonPerfectSquareNumber(``int` `a[], ``int` `n) ` `{ ` `    ``// stores the maximum of all non perfect square numbers ` `    ``int` `maxi = -1; ` ` `  `    ``// traverse for all elements in the array ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// store the maximum if not a perfect square ` `        ``if` `(!check(a[i])) ` `            ``maxi = max(a[i], maxi); ` `    ``} ` `    ``return` `maxi; ` `} ` ` `  `// driver code to check the above functions ` `int` `main() ` `{ ` `    ``int` `a[] = { 16, 20, 25, 2, 3, 10 }; ` ` `  `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` `    ``// function call ` `    ``cout << largestNonPerfectSquareNumber(a, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find the ` `// largest non perfect  ` `// square number among n numbers ` ` `  `import` `java.io.*; ` `  `  `class` `GfG{ ` `      `  `static` `Boolean check(``int` `n) ` `{ ` `    ``// takes the sqrt of the number ` `    ``int` `d = (``int``)Math.sqrt(n); ` `  `  `    ``// checks if it is a perfect square number ` `    ``if` `(d * d == n) ` `        ``return` `true``; ` `  `  `    ``return` `false``; ` `} ` `  `  `// fucntion to find the largest ` `// non perfect square number ` `static` `int` `largestNonPerfectSquareNumber(``int` `a[], ``int` `n) ` `{ ` `    ``// stores the maximum of all ` `    ``// non perfect square numbers ` `    ``int` `maxi = -``1``; ` `  `  `    ``// traverse for all elements in the array ` `    ``for` `(``int` `i = ``0``; i < n; i++) { ` `  `  `        ``// store the maximum if ` `        ``// not a perfect square ` `        ``if` `(!check(a[i])) ` `            ``maxi = Math.max(a[i], maxi); ` `    ``} ` `    ``return` `maxi; ` `} ` ` `  `    ``public` `static` `void` `main (String[] args) { ` ` `  `        ``int` `a[] = { ``16``, ``20``, ``25``, ``2``, ``3``, ``10` `}; ` `        ``int` `n = a.length; ` ` `  `        ``// function call ` `        ``System.out.println(largestNonPerfectSquareNumber(a, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Gitanjali. `

## Python3

 `# python program to find ` `# the largest non perfect  ` `# square number among n numbers ` ` `  `import` `math ` `def` `check( n): ` ` `  `    ``# takes the sqrt of the number ` `    ``d ``=` `int``(math.sqrt(n)) ` `  `  `    ``# checks if it is a ` `    ``# perfect square number ` `    ``if` `(d ``*` `d ``=``=` `n): ` `        ``return` `True` `  `  `    ``return` `False` ` `  `  `  `# function to find the largest ` `# non perfect square number ` `def` `largestNonPerfectSquareNumber(a, n): ` ` `  `    ``# stores the maximum of all ` `    ``# non perfect square numbers ` `    ``maxi ``=` `-``1` `  `  `    ``# traverse for all elements ` `    ``# in the array ` `    ``for` `i ``in` `range``(``0``,n): ` `  `  `        ``# store the maximum if ` `        ``# not a perfect square ` `        ``if` `(check(a[i])``=``=``False``): ` `            ``maxi ``=` `max``(a[i], maxi) ` `     `  `    ``return` `maxi ` ` `  `# driver code ` `a ``=` `[ ``16``, ``20``, ``25``, ``2``, ``3``, ``10` `] ` `n``=` `len``(a) ` ` `  `# function call ` `print` `(largestNonPerfectSquareNumber(a, n)) ` ` `  `# This code is contributed by Gitanjali. `

## C#

 `// C# program to find the largest non perfect  ` `// square number among n numbers ` `using` `System; ` ` `  `class` `GfG { ` `     `  `    ``static` `bool` `check(``int` `n) ` `    ``{ ` `         `  `        ``// takes the sqrt of the number ` `        ``int` `d = (``int``)Math.Sqrt(n); ` `     `  `        ``// checks if it is a perfect  ` `        ``// square number ` `        ``if` `(d * d == n) ` `            ``return` `true``; ` `     `  `        ``return` `false``; ` `    ``} ` `     `  `    ``// fucntion to find the largest ` `    ``// non perfect square number ` `    ``static` `int` `largestNonPerfectSquareNumber( ` `                               ``int` `[]a, ``int` `n) ` `    ``{ ` `         `  `        ``// stores the maximum of all ` `        ``// non perfect square numbers ` `        ``int` `maxi = -1; ` `     `  `        ``// traverse for all elements in  ` `        ``// the array ` `        ``for` `(``int` `i = 0; i < n; i++) { ` `     `  `            ``// store the maximum if ` `            ``// not a perfect square ` `            ``if` `(!check(a[i])) ` `                ``maxi = Math.Max(a[i], maxi); ` `        ``} ` `         `  `        ``return` `maxi; ` `    ``} ` ` `  `    ``// driver code to check the above functions ` `    ``public` `static` `void` `Main () ` `    ``{ ` ` `  `        ``int` `[]a = { 16, 20, 25, 2, 3, 10 }; ` `        ``int` `n = a.Length; ` ` `  `        ``// function call ` `        ``Console.WriteLine( ` `           ``largestNonPerfectSquareNumber(a, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output:

```20
```

Time complexity can be considered as O(n) as sqrt() function can be implemented in O(1) time for fixed size (32 bit or 64 bit) integers [Refer Wiki for details]

My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.

Improved By : vt_m