Subarrays whose sum is a perfect square | C ++ program to implement the above approach ; Function to print the start and end indices of all subarrays whose sum is a perfect square ; Stores the current subarray sum ; Update current subarray sum ; Stores the square root of currSubSum ; Check if currSubSum is a perfect square or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ) ; }