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
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 <bits/stdc++.h> 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
<?php // PHP program to find // the largest non perfect // square number among n // numbers function check($n) { // takes the sqrt // of the number $d = 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 function largestNonPerfectSquareNumber($a, $n) { // stores the maximum of // all non perfect square // numbers $maxi = -1; // traverse for all // elements in the array for ($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 $a = array(16, 20, 25, 2, 3, 10); $n = count($a); // function call echo largestNonPerfectSquareNumber($a, $n); // This code is contributed by anuj_67. ?> |
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]
Recommended Posts:
- Largest factor of a given number which is a perfect square
- Largest perfect square number in an Array
- Largest Divisor of a Number not divisible by a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Check if a number is perfect square without finding square root
- Check if given number is perfect square
- Find minimum number to be divided to make a number a perfect square
- Find the Next perfect square greater than a given number
- Count numbers upto N which are both perfect square and perfect cube
- Check whether the number can be made perfect square after adding 1
- Minimum digits to remove to make a number Perfect Square
- Largest perfect cube number in an Array
- Largest number in an array that is not a perfect cube
- Perfect Square String
- Print n numbers such that their sum is a perfect square
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



