Count of Subarray with B times Sum equal to C times Length
Last Updated :
19 Sep, 2023
Given, an array A[] of N integers, and also given two positive integers B and C, the task is to find the number of subarrays, such that B* Sum of elements of the subarray = C * Length of the subarray (here * describes multiplication).
Examples:
Input: N = 3, A[] = {3, -1, 1}, B = 1, C = 1
Output: 3
Explanation: The subarrays satisfying the conditions are [3, - 1], [3, - 1, 1], and [1].
- For the subarray [3, -1], sum of the subarray is equal to 2 and length of the subarray is 2. So the condition holds good for it i.e., (1 * 2 = 1 * 2).
- For the subarray [3, -1, 1], sum of the subarray is equal to 3 and length of the subarray is 3. So the condition holds good for it i.e., (1 * 3 = 1 * 3).
- For the subarray [1], sum of the subarray is equal to 1 and length of the subarray is 1. So the condition holds good for it i.e., (1 * 1 = 1 * 1).
Input: N = 3, A[] = {1, 2, 3}, B = 1, C = 2
Output: 2
Explanation: The subarrays satisfying the conditions are [1, 2, 3] and [2].
Approach: To solve the problem follow the below idea:
The approach is using a map to hash value, and using prefix sum for quick sum calculation of ranges. For example, if a subarray from [l, r] satisfies the conditions, then according to the question,
- B * (prefix[r] - prefix[l - 1]) = C * (r - l + 1). On Rearranging,
- B * prefix[r] - C * r = B * prefix[l - 1] - C * l + C, so we have all the r terms come on the left side and l terms on the right side So, we can use a map to hash the value on the right side and then search for the value of left side as key in the map.
Steps that were to follow the above approach:
- Create a prefix array to store the prefix sums of array A.
- Create a Map to store the value of the right side as a key, So that we can search for the value of the left side in the Map.
- Iterate over the array and store the right side value in the map and check if the left side value is already present in the Map, if so increment the ans variable that many times.
- Return the answer.
Below is the code to implement the above steps:
C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// This function returns the count of all
// the subarrays satisfying the conditions
int countSubarrays(int N, int A[], int B, int C)
{
// Declaring the prefix vector to
// store prefix sums of the array.
vector<long long> prefix(N + 1);
// Computing the prefix sums
for (int i = 1; i <= N; i++) {
prefix[i] = prefix[i - 1] + A[i - 1];
}
// ans will store the count of subarrays
long long ans = 0;
map<long long, int> m;
// Hashing the value on the right side
// and then search for the value of
// left side as key in the map
for (int l = 1; l <= N; l++) {
m[B * prefix[l - 1] - C * l + C] += 1;
ans += m[B * prefix[l] - C * l];
}
// Returning the result
return ans;
}
// Driver's code
int main()
{
int N = 3;
int A[] = { 1, 2, 3 };
int B = 1, C = 2;
// function call
cout << countSubarrays(N, A, B, C);
return 0;
}
Java
// Java code for the above approach
import java.io.*;
import java.util.*;
class GFG {
static int countSubarrays(int N, int[] A, int B, int C)
{
// declaring the prefix array to store prefix sums
// of the array
int[] prefix = new int[N + 1];
// computing the prefix sums
for (int i = 1; i <= N; i++) {
prefix[i] = prefix[i - 1] + A[i - 1];
}
// ans will store the count of subarrays
int ans = 0;
Map<Integer, Integer> m = new HashMap<>();
// Hashing the value on the right side and then
// search for the value of left side as key in the
// map
for (int l = 1; l <= N; l++) {
m.put(B * prefix[l - 1] - C * l + C,
m.getOrDefault(
B * prefix[l - 1] - C * l + C, 0)
+ 1);
ans += m.getOrDefault(B * prefix[l] - C * l, 0);
}
// returning the result
return ans;
}
public static void main(String[] args)
{
int N = 3;
int[] A = { 1, 2, 3 };
int B = 1, C = 2;
// Function call
System.out.print(countSubarrays(N, A, B, C));
}
}
// This code is contributed by sankar.
Python3
# This function returns the count of all
# the subarrays satisfying the conditions
def countSubarrays(N, A, B, C):
# Declaring the prefix vector to
# store prefix sums of the array.
prefix = [0] * (N + 1)
# Computing the prefix sums
for i in range(1, N + 1):
prefix[i] = prefix[i - 1] + A[i - 1]
# ans will store the count of subarrays
ans = 0
m = {}
# Hashing the value on the right side
# and then search for the value of
# left side as key in the map
for l in range(1, N + 1):
m[B * prefix[l - 1] - C * l + C] = m.get(B * prefix[l - 1] - C * l + C, 0) + 1
ans += m.get(B * prefix[l] - C * l, 0)
# Returning the result
return ans
# Driver code
N = 3
A = [1, 2, 3]
B = 1
C = 2
print(countSubarrays(N, A, B, C))
C#
// c# code for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
static int CountSubarrays(int N, int[] A, int B, int C)
{
// declaring the prefix array to store prefix sums
int[] prefix = new int[N + 1];
// computing the prefix sums
for (int i = 1; i <= N; i++)
{
prefix[i] = prefix[i - 1] + A[i - 1];
}
// ans will store the count of subarrays
int ans = 0;
Dictionary<int, int> m = new Dictionary<int, int>();
// Hashing the value on the right side and then
// search for the value of left side as key in the map
for (int l = 1; l <= N; l++)
{
int rightValue = B * prefix[l - 1] - C * l + C;
m[rightValue] = m.GetValueOrDefault(rightValue, 0) + 1;
ans += m.GetValueOrDefault(B * prefix[l] - C * l, 0);
}
// returning the result
return ans;
}
public static void Main(string[] args)
{
int N = 3;
int[] A = { 1, 2, 3 };
int B = 1, C = 2;
// Function call
Console.Write(CountSubarrays(N, A, B, C));
}
}
JavaScript
function countSubarrays(N, A, B, C) {
// Declaring the prefix vector to
// store prefix sums of the array.
let prefix = new Array(N + 1).fill(0);
// Computing the prefix sums
for (let i = 1; i <= N; i++) {
prefix[i] = prefix[i - 1] + A[i - 1];
}
// ans will store the count of subarrays
let ans = 0;
let m = {};
// Hashing the value on the right side
// and then search for the value of
// left side as key in the map
for (let l = 1; l <= N; l++) {
m[B * prefix[l - 1] - C * l + C] = (m[B * prefix[l - 1] - C * l + C] || 0) + 1;
ans += m[B * prefix[l] - C * l] || 0;
}
// Returning the result
return ans;
}
// Driver code
let N = 3;
let A = [1, 2, 3];
let B = 1;
let C = 2;
console.log(countSubarrays(N, A, B, C));
Time Complexity: O(n*logn), where n is the length of the array.
Auxiliary Space: O(n).
Similar Reads
Count of subarrays having sum equal to its length Given an array arr[] of size N, the task is to find the number of subarrays having the sum of its elements equal to the number of elements in it. Examples: Input: N = 3, arr[] = {1, 0, 2}Output: 3Explanation:Total number of subarrays are 6 i.e., {1}, {0}, {2}, {1, 0}, {0, 2}, {1, 0, 2}.Out of 6 only
7 min read
Count of subsets with sum equal to target Given an array arr[] of length n and an integer target, the task is to find the number of subsets with a sum equal to target.Examples: Input: arr[] = [1, 2, 3, 3], target = 6 Output: 3 Explanation: All the possible subsets are [1, 2, 3], [1, 2, 3] and [3, 3]Input: arr[] = [1, 1, 1, 1], target = 1 Ou
15+ min read
Sum of Subarrays with Unique elements count Given an array arr[] of length N. The task is to calculate the sum of all subarrays where each subarray value is the number of elements in the subarray that occurs exactly once. Examples: Input: N = 3, arr[ ] = {2, 4, 2}Output: 8Explanation: All possible subarrays are {2}: beauty is 1.{4}: beauty is
7 min read
Count of subarrays with digit sum equals to X Given an array arr[] of length N and integer X, the task is to count no of subarrays having digit sum equal to X. Examples: Input: arr[] = {10, 5, 13, 20, 9}, X = 6Output: 2Explanation: There are two subarrays which is having digit sum equal to 6. {10, 5} => (1 + 0) + 5 = 6 and {13 , 20} => (1
9 min read
Count of subarrays having sum equal to its length | Set 2 Given an array arr[] of size N, the task is to find the number of subarrays having sum of its elements equal to the number of elements in it. Examples: Input: N = 3, arr[] = {1, 0, 2}Output: 3Explanation:Total number of subarrays are 6 i.e., {1}, {0}, {2}, {1, 0}, {0, 2}, {1, 0, 2}.Out of the 6 suba
7 min read
Count of subarrays with unique sum with sum at most K Given an array arr[] of size N and an integer K., The task is to count the number of subarrays with unique sum with sum at most K. Examples: Input: N = 3, arr[] = {1, 0, 1}, K = 1Output: 2Explanation: All Subarrays are [1], [0], [1], [1, 0], [0, 1], [1, 0, 1] & The sum of these subarrays are {1,
4 min read
Count of subarrays with sum at least K Given an array arr[] of size N and an integer K > 0. The task is to find the number of subarrays with sum at least K.Examples: Input: arr[] = {6, 1, 2, 7}, K = 10 Output: 2 {6, 1, 2, 7} and {1, 2, 7} are the only valid subarrays.Input: arr[] = {3, 3, 3}, K = 5 Output: 3 Approach: For a fixed left
6 min read
Count subarrays with sum equal to its XOR value Given an array arr[] containing N elements, the task is to count the number of sub-arrays whose XOR of all the elements is equal to the sum of all the elements in the subarray. Examples: Input: arr[] = {2, 5, 4, 6} Output: 5 Explanation: All the subarrays {{2}, {5}, {4}, {6}} satisfies the above con
10 min read
Count of K-length subarray with each element less than X times the next one Given an array A[] of length N and two integers X and K, The task is to count the number of indices i (0 ⤠i < Nâk) such that:X0â
ai < X1â
ai + 1 < X2â
ai+2 < . . . < Xkâ
ai+k. Examples: Input: A[] = {9, 5, 3, 4, 1}, X = 4, K = 1.Output: 3.Explanation: Three Subarrays satisfy the conditio
9 min read
Count of Subarrays which Contain the Length of that Subarray Given an array A[] of length N, the task is to count the number of subarrays of A[] that contain the length of that subarray. Examples: Input: A = {10, 11, 1}, N = 3Output: 1Explanation: Only the subarray {1}, with a length 1, contains its own length. Input: A = [1, 2, 3, 4, 5], N = 5Output: 9Explan
8 min read