Given an array arr[] of size N and an array range[], the task is to check if the sum of the subarray {range[0], .. , range[1]} is a perfect square or not. If the sum is a perfect square, then print the square root of the sum. Otherwise, print -1.
Example :
Input: arr[] = {2, 19, 33, 48, 90, 100}, range = [1, 3]
Output: 10
Explanation:
The sum of element from position 1 to position 3 is 19 + 33 + 48 = 100, which is a perfect square of 10.
Input: arr[] = {13, 15, 30, 55, 87}, range = [0, 1]
Output: -1
Naive Approach: The simplest approach is to iterate over the subarray and check if the sum of the subarray is a perfect square or not.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use Binary Search to calculate the square root of the sum of the subarray. Follow the steps below:
- Calculate the sum of the subarray in the given range[].
- Now, use binary search to find the square root of the sum in the range 0 to sum.
- Find the mid element from the start and last value and compare the value of mid2 with the sum.
- If the value of mid2 is equal to the sum, a perfect square is found, then return mid.
- If the value of mid2 is greater than the sum, then the answer can lie only on the right side of the range after the mid element. So recur for right and reduce the search space to 0 to mid – 1.
- If the value of mid2 is less than the sum, reduce the search space to mid + 1 to sum.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int checkForPerfectSquare(vector< int > arr,
int i, int j)
{
int mid, sum = 0;
for ( int m = i; m <= j; m++) {
sum += arr[m];
}
int low = 0, high = sum / 2;
while (low <= high) {
mid = low + (high - low) / 2;
if (mid * mid == sum) {
return mid;
}
else if (mid * mid > sum) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
return -1;
}
int main()
{
vector< int > arr;
arr = { 2, 19, 33, 48, 90, 100 };
int L = 1, R = 3;
cout << checkForPerfectSquare(arr, L, R);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int checkForPerfectSquare( int []arr,
int i, int j)
{
int mid, sum = 0 ;
for ( int m = i; m <= j; m++)
{
sum += arr[m];
}
int low = 0 , high = sum / 2 ;
while (low <= high)
{
mid = low + (high - low) / 2 ;
if (mid * mid == sum)
{
return mid;
}
else if (mid * mid > sum)
{
high = mid - 1 ;
}
else
{
low = mid + 1 ;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int []arr = { 2 , 19 , 33 ,
48 , 90 , 100 };
int L = 1 , R = 3 ;
System.out.print(
checkForPerfectSquare(arr, L, R));
}
}
|
Python3
def checkForPerfectSquare(arr, i, j):
mid, sum = 0 , 0
for m in range (i, j + 1 ):
sum + = arr[m]
low = 0
high = sum / / 2
while (low < = high):
mid = low + (high - low) / / 2
if (mid * mid = = sum ):
return mid
elif (mid * mid > sum ):
high = mid - 1
else :
low = mid + 1
return - 1
if __name__ = = '__main__' :
arr = [ 2 , 19 , 33 , 48 , 90 , 100 ]
L = 1
R = 3
print (checkForPerfectSquare(arr, L, R))
|
C#
using System;
class GFG{
static int checkForPerfectSquare( int []arr,
int i, int j)
{
int mid, sum = 0;
for ( int m = i; m <= j; m++)
{
sum += arr[m];
}
int low = 0, high = sum / 2;
while (low <= high)
{
mid = low + (high - low) / 2;
if (mid * mid == sum)
{
return mid;
}
else if (mid * mid > sum)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return -1;
}
public static void Main(String[] args)
{
int []arr = {2, 19, 33,
48, 90, 100};
int L = 1, R = 3;
Console.Write(checkForPerfectSquare(arr,
L, R));}
}
|
Javascript
<script>
function checkForPerfectSquare(arr, i, j)
{
let mid, sum = 0;
for (let m = i; m <= j; m++)
{
sum += arr[m];
}
let low = 0, high = parseInt(sum / 2);
while (low <= high)
{
mid = low + parseInt((high - low) / 2);
if (mid * mid == sum)
{
return mid;
}
else if (mid * mid > sum)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return -1;
}
let arr = [ 2, 19, 33, 48, 90, 100 ];
let L = 1, R = 3;
document.write(checkForPerfectSquare(arr, L, R));
</script>
|
Time Complexity: O(max(log(sum), N))
Auxiliary Space: O(1)