Subarrays whose sum is a perfect square
Given an array, arr[] of size N, the task is to print the start and end indices of all subarrays whose sum is a perfect square.
Examples:
Input: arr[] = {65, 79, 81}
Output: (0, 1) (0, 2) (2, 2)
Explanation:
Subarray sum whose start and end index is (0, 1) = 65 + 79 = 144 = 122
Subarray sum whose start and end index is (0, 2} = 65 + 79 + 81 = 225 = 152
Subarray sum whose start and end index is {2, 2} = 81 = 92
Input: arr[] = {1, 2, 3, 4, 5}
Output: (0, 0) (1, 3) (3, 3) (3, 4)
Approach: The problem can be solved using the Prefix Sum Array technique. The idea is to find the sum of all subarrays using the Prefix Sum Array. For each subarray, check if the sum of the subarray is a perfect square or not. If found to be true for any subarray, then print the start and end indices of that subarray. Follow the steps below to solve the problem.
- Initialize a variable, say currSubSum to store the current subarray sum.
- Iterate over the array to generate all possible subarrays of the given array.
- Calculate the sum of each subarray and for each subarray sum, check if it is a perfect square or not.
- If found to be true for any subarray, then print the start and end indices of the subarray.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void PrintIndexes( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
int currSubSum = 0;
for ( int j = i; j < N; j++) {
currSubSum += arr[j];
int sq = sqrt (currSubSum);
if (sq * sq == currSubSum) {
cout << "(" << i << ", "
<< j << ") " ;
}
}
}
}
int main()
{
int arr[] = { 65, 79, 81 };
int N = sizeof (arr) / sizeof (arr[0]);
PrintIndexes(arr, N);
}
|
Java
import java.io.*;
class GFG{
static void PrintIndexes( int arr[], int N)
{
for ( int i = 0 ; i < N; i++)
{
int currSubSum = 0 ;
for ( int j = i; j < N; j++)
{
currSubSum += arr[j];
int sq = ( int )Math.sqrt(currSubSum);
if (sq * sq == currSubSum)
{
System.out.print( "(" + i + "," +
j + ")" + " " );
}
}
}
}
public static void main (String[] args)
throws java.lang.Exception
{
int arr[] = { 65 , 79 , 81 };
PrintIndexes(arr, arr.length);
}
}
|
Python3
import math
def PrintIndexes(arr, N):
for i in range (N):
currSubSum = 0
for j in range (i, N, 1 ):
currSubSum + = arr[j]
sq = int (math.sqrt(currSubSum))
if (sq * sq = = currSubSum):
print ( "(" , i, "," ,
j, ")" , end = " " )
arr = [ 65 , 79 , 81 ]
N = len (arr)
PrintIndexes(arr, N)
|
C#
using System;
class GFG{
static void PrintIndexes( int []arr,
int N)
{
for ( int i = 0; i < N; i++)
{
int currSubSum = 0;
for ( int j = i; j < N; j++)
{
currSubSum += arr[j];
int sq = ( int )Math.Sqrt(currSubSum);
if (sq * sq == currSubSum)
{
Console.Write( "(" + i + "," +
j + ")" + " " );
}
}
}
}
public static void Main(String[] args)
{
int []arr = {65, 79, 81};
PrintIndexes(arr, arr.Length);
}
}
|
Javascript
<script>
function PrintIndexes(arr, N)
{
for (let i = 0; i < N; i++)
{
let currSubSum = 0;
for (let j = i; j < N; j++)
{
currSubSum += arr[j];
let sq = Math.floor(Math.sqrt(currSubSum));
if (sq * sq == currSubSum)
{
document.write( "(" + i + "," +
j + ")" + " " );
}
}
}
}
let arr = [ 65, 79, 81 ];
PrintIndexes(arr, arr.length);
</script>
|
Output:
(0, 1) (0, 2) (2, 2)
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
21 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...