Count of elements such that difference between sum of left and right sub arrays is equal to a multiple of k
Last Updated :
02 Jun, 2022
Given an array arr[] of length n and an integer k, the task is to find the number of indices from 2 to n-1 in an array having a difference of the sum of the left and right sub arrays equal to the multiple of the given number k.
Examples:
Input: arr[] = {1, 2, 3, 3, 1, 1}, k = 4
Output: 2
Explanation: The only possible indices are 4 and 5
Input: arr[] = {1, 2, 3, 4, 5}, k = 1
Output: 3
Approach:
- Create a prefix array that contains the sum of the elements on the left and the suffix array which contains the sum of elements on the right.
- Check for every index the difference of the sum on the left and right and increase the counter if it is divisible by k.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
int noOfElement( int a[], int n, int k)
{
int prefix[n];
prefix[0] = a[0];
for ( int i = 1; i < n; i++) {
prefix[i] = prefix[i - 1] + a[i];
}
int suffix[n];
suffix[n - 1] = a[n - 1];
for ( int i = n - 2; i >= 0; i--) {
suffix[i] = suffix[i + 1] + a[i];
}
int cnt = 0;
for ( int i = 1; i < n - 1; i++) {
if ((prefix[i] - suffix[i]) % k == 0
|| (suffix[i] - prefix[i]) % k == 0) {
cnt = cnt + 1;
}
}
return cnt;
}
int main()
{
int a[] = { 1, 2, 3, 3, 1, 1 };
int k = 4;
int n = sizeof (a) / sizeof (a[0]);
cout << noOfElement(a, n, k);
return 0;
}
|
Java
class GFG
{
static int noOfElement( int a[], int n, int k)
{
int []prefix = new int [n];
prefix[ 0 ] = a[ 0 ];
for ( int i = 1 ; i < n; i++)
{
prefix[i] = prefix[i - 1 ] + a[i];
}
int []suffix = new int [n];
suffix[n - 1 ] = a[n - 1 ];
for ( int i = n - 2 ; i >= 0 ; i--)
{
suffix[i] = suffix[i + 1 ] + a[i];
}
int cnt = 0 ;
for ( int i = 1 ; i < n - 1 ; i++)
{
if ((prefix[i] - suffix[i]) % k == 0
|| (suffix[i] - prefix[i]) % k == 0 )
{
cnt = cnt + 1 ;
}
}
return cnt;
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 3 , 3 , 1 , 1 };
int k = 4 ;
int n = a.length;
System.out.print(noOfElement(a, n, k));
}
}
|
Python
def noOfElement(a, n, k):
prefix = [ 0 ] * n
prefix[ 0 ] = a[ 0 ]
for i in range ( 1 , n):
prefix[i] = prefix[i - 1 ] + a[i]
suffix = [ 0 ] * n
suffix[n - 1 ] = a[n - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
suffix[i] = suffix[i + 1 ] + a[i]
cnt = 0
for i in range ( 1 , n - 1 ):
if ((prefix[i] - suffix[i]) % k = = 0 or (suffix[i] - prefix[i]) % k = = 0 ):
cnt = cnt + 1
return cnt
a = [ 1 , 2 , 3 , 3 , 1 , 1 ]
k = 4
n = len (a)
print (noOfElement(a, n, k))
|
C#
using System;
class GFG
{
static int noOfElement( int []a, int n, int k)
{
int []prefix = new int [n];
prefix[0] = a[0];
for ( int i = 1; i < n; i++)
{
prefix[i] = prefix[i - 1] + a[i];
}
int []suffix = new int [n];
suffix[n - 1] = a[n - 1];
for ( int i = n - 2; i >= 0; i--)
{
suffix[i] = suffix[i + 1] + a[i];
}
int cnt = 0;
for ( int i = 1; i < n - 1; i++)
{
if ((prefix[i] - suffix[i]) % k == 0
|| (suffix[i] - prefix[i]) % k == 0)
{
cnt = cnt + 1;
}
}
return cnt;
}
public static void Main()
{
int []a = { 1, 2, 3, 3, 1, 1 };
int k = 4;
int n = a.Length;
Console.Write(noOfElement(a, n, k));
}
}
|
Javascript
<script>
function noOfElement(a,n,k)
{
let prefix = new Array(n);
prefix[0] = a[0];
for (let i = 1; i < n; i++)
{
prefix[i] = prefix[i - 1] + a[i];
}
let suffix = new Array(n);
suffix[n - 1] = a[n - 1];
for (let i = n - 2; i >= 0; i--)
{
suffix[i] = suffix[i + 1] + a[i];
}
let cnt = 0;
for (let i = 1; i < n - 1; i++)
{
if ((prefix[i] - suffix[i]) % k == 0
|| (suffix[i] - prefix[i]) % k == 0)
{
cnt = cnt + 1;
}
}
return cnt;
}
let a=[1, 2, 3, 3, 1, 1];
let k = 4;
let n = a.length;
document.write(noOfElement(a, n, k));
</script>
|
Time Complexity: O(n), where n is the size of the array
Auxiliary Space: O(n), as extra space of size n is used to create prefix and suffix array
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...