Count all subarrays whose sum can be split as difference of squares of two Integers
Last Updated :
16 Sep, 2022
Given an array arr[], the task is to count all subarrays whose sum can be split as the difference of the squares of two integers.
Examples:
Input: arr[] = {1, 3, 5}
Output: 6
Explanation:
There are six subarrays which can be formed from the array whose sum can be split as the difference of squares of two integers. They are:
Sum of the subarray {1} = 1 = 12 – 02
Sum of the subarray {3} = 3 = 22 – 12
Sum of the subarray {5} = 5 = 32 – 22
Sum of the subarray {1, 3} = 4 = 22 – 02
Sum of the subarray {3, 5} = 8 = 32 – 12
Sum of the subarray {1, 3, 5} = 9 = 52 – 42
Input: arr[] = {1, 2, 3, 4, 5}
Output: 11
Approach: In order to solve this problem, an observation needs to be made. Any number can be represented as the difference of two squares except those which can be represented in the form ((4 * N) + 2) where N is an integer. Therefore, the following steps can be followed to compute the answer:
- Iterate over the array to find all the possible subarrays of the given array.
- If a number K can be expressed as ((4 * N) + 2) where N is some integer, then K + 2 is always a multiple of 4.
- Therefore, for every sum of the subarray K, check if (K + 2) is a multiple of 4 or not.
- If it is, then that particular value cannot be expressed as the difference of the squares.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Solve( int arr[], int n)
{
int temp = 0, count = 0;
for ( int i = 0; i < n; i++) {
temp = 0;
for ( int j = i; j < n; j++) {
temp += arr[j];
if ((temp + 2) % 4 != 0)
count++;
}
}
return count;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5,
6, 7, 8, 9, 10 };
int N = sizeof (arr) / sizeof ( int );
cout << Solve(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int Solve( int arr[], int n)
{
int temp = 0 , count = 0 ;
for ( int i = 0 ; i < n; i++)
{
temp = 0 ;
for ( int j = i; j < n; j++)
{
temp += arr[j];
if ((temp + 2 ) % 4 != 0 )
count++;
}
}
return count;
}
public static void main(String [] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 ,
6 , 7 , 8 , 9 , 10 };
int N = arr.length;
System.out.println(Solve(arr, N));
}
}
|
Python3
def Solve(arr, n):
temp = 0 ; count = 0 ;
for i in range ( 0 , n):
temp = 0 ;
for j in range (i, n):
temp = temp + arr[j];
if ((temp + 2 ) % 4 ! = 0 ):
count + = 1 ;
return count;
arr = [ 1 , 2 , 3 , 4 , 5 ,
6 , 7 , 8 , 9 , 10 ];
N = len (arr);
print (Solve(arr, N));
|
C#
using System;
class GFG{
static int Solve( int []arr, int n)
{
int temp = 0, count = 0;
for ( int i = 0; i < n; i++)
{
temp = 0;
for ( int j = i; j < n; j++)
{
temp += arr[j];
if ((temp + 2) % 4 != 0)
count++;
}
}
return count;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5,
6, 7, 8, 9, 10 };
int N = arr.Length;
Console.Write(Solve(arr, N));
}
}
|
Javascript
<script>
function Solve(arr, n)
{
let temp = 0, count = 0;
for (let i = 0; i < n; i++) {
temp = 0;
for (let j = i; j < n; j++) {
temp += arr[j];
if ((temp + 2) % 4 != 0)
count++;
}
}
return count;
}
let arr = [ 1, 2, 3, 4, 5,
6, 7, 8, 9, 10 ];
let N = arr.length;
document.write(Solve(arr, N));
</script>
|
Time Complexity: O(N)2 where N is the size of the array
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...