Given an array arr[], the task is to count all sub-array whose sum can be represented as the difference of squares of any two numbers.
Examples:
Input: arr[] = {1, 2, 3}
Output: 4
Explanation:
Required sub-arrays are {1}, {3}, {1, 2} and {2, 3}
As 12 – 02 = 1, 22 – 12 = 3, 2+3=5=> 32 – 22 = 5
Input: arr[] = {2, 1, 3, 7}
Output: 7
Required sub-arrays are –
{1}, {3}, {7}, {2, 1}, {2, 1, 3, 7}, {1, 3} and {1, 3, 7}
Approach: The idea is to use the fact that the number which is odd or divisible by 4 can only be represented as the difference of squares of 2 numbers. Below is the illustration of the steps:
- Run nested loops and check for every sub-array whether sum can be written as the difference of squares of 2 numbers or not.
- If the sum of any subarray can be represented as the difference of the square of two numbers then increment the count of such subarrays by 1
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int countSubarray( int * arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 != 0 || arr[i] % 4 == 0)
count++;
ll sum = arr[i];
for ( int j = i + 1; j < n; j++) {
sum += arr[j];
if (sum % 2 != 0 || sum % 4 == 0)
count++;
}
}
return count;
}
int main()
{
int arr[] = { 2, 1, 3, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countSubarray(arr, n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int countSubarray( int [] arr, int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] % 2 != 0 || arr[i] % 4 == 0 )
count++;
long sum = arr[i];
for ( int j = i + 1 ; j < n; j++) {
sum += arr[j];
if (sum % 2 != 0 || sum % 4 == 0 )
count++;
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 2 , 1 , 3 , 7 };
int n = arr.length;
System.out.println(countSubarray(arr, n));
}
}
|
Python3
def countSubarray(arr, n):
count = 0
for i in range (n):
if arr[i] % 2 ! = 0 or arr[i] % 4 = = 0 :
count + = 1
tot = arr[i]
for j in range (i + 1 , n):
tot + = arr[j]
if tot % 2 ! = 0 or tot % 4 = = 0 :
count + = 1
return count
if __name__ = = "__main__" :
arr = [ 2 , 1 , 3 , 7 ]
n = len (arr)
print (countSubarray(arr, n))
|
C#
using System;
class GFG {
static int countSubarray( int [] arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 != 0 || arr[i] % 4 == 0)
count++;
long sum = arr[i];
for ( int j = i + 1; j < n; j++) {
sum += arr[j];
if (sum % 2 != 0 || sum % 4 == 0)
count++;
}
}
return count;
}
public static void Main()
{
int [] arr = { 2, 1, 3, 7 };
int n = arr.Length;
Console.WriteLine(countSubarray(arr, n));
}
}
|
PHP
<?php
function countSubarray( $arr , $n )
{
$count =0;
for ( $i =0; $i < $n ; $i ++)
{
if ( $arr [ $i ]%2!=0 || $arr [ $i ]%4==0)
$count ++;
$sum = $arr [ $i ];
for ( $j = $i +1; $j < $n ; $j ++)
{
$sum += $arr [ $j ];
if ( $sum %2!=0 || $sum %4==0)
$count ++;
}
}
return $count ;
}
$arr = array ( 2, 1, 3, 7 );
$n = count ( $arr );
echo countSubarray( $arr , $n );
?>
|
Javascript
<script>
function countSubarray(arr, n)
{
var count = 0;
for ( var i = 0; i < n; i++)
{
if (arr[i] % 2 != 0 || arr[i] % 4 == 0)
count++;
var sum = arr[i];
for ( var j = i + 1; j < n; j++)
{
sum += arr[j];
if (sum % 2 != 0 || sum % 4 == 0)
count++;
}
}
return count;
}
var arr = [ 2, 1, 3, 7 ];
var n = arr.length;
document.write(countSubarray(arr, n));
</script>
|
Time Complexity: O(N2)