元素的计数,使得左右子数组之和之间的差等于 k 的倍数
给定一个长度为n的数组arr[]和一个整数k ,任务是找到一个数组中从 2 到 n-1 的索引数,该数组的左右子数组之和的差等于给定的数字 k。
例子:
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
方法:
- 创建一个包含左侧元素总和的前缀数组和包含右侧元素总和的后缀数组。
- 检查每个索引左右和的差异,如果它可以被 k 整除,则增加计数器。
下面是上述方法的实现:
CPP
// C++ code to count of elements such that
// difference between the sum of left and right
// sub-arrays are equal to a multiple of k
#include
using namespace std;
// Functions to find the no of elements
int noOfElement(int a[], int n, int k)
{
// Creating a prefix array
int prefix[n];
// Starting element of prefix array
// will be the first element
// of given array
prefix[0] = a[0];
for (int i = 1; i < n; i++) {
prefix[i] = prefix[i - 1] + a[i];
}
// Creating a suffix array;
int suffix[n];
// Last element of suffix array will
// be the last element of given array
suffix[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--) {
suffix[i] = suffix[i + 1] + a[i];
}
// Checking difference of left and right half
// is divisible by k or not.
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;
}
// Driver code
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
// Java code to count of elements such that
// difference between the sum of left and right
// sub-arrays are equal to a multiple of k
class GFG
{
// Functions to find the no of elements
static int noOfElement(int a[], int n, int k)
{
// Creating a prefix array
int []prefix = new int[n];
// Starting element of prefix array
// will be the first element
// of given array
prefix[0] = a[0];
for (int i = 1; i < n; i++)
{
prefix[i] = prefix[i - 1] + a[i];
}
// Creating a suffix array;
int []suffix = new int[n];
// Last element of suffix array will
// be the last element of given array
suffix[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--)
{
suffix[i] = suffix[i + 1] + a[i];
}
// Checking difference of left and right half
// is divisible by k or not.
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;
}
// Driver code
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));
}
}
// This code is contributed by Rajput-Ji
Python
# Python3 code to count of elements such that
# difference between the sum of left and right
# sub-arrays are equal to a multiple of k
# Functions to find the no of elements
def noOfElement(a, n, k):
# Creating a prefix array
prefix = [0] * n
# Starting element of prefix array
# will be the first element
# of given array
prefix[0] = a[0]
for i in range(1, n):
prefix[i] = prefix[i - 1] + a[i]
# Creating a suffix array
suffix = [0] * n
# Last element of suffix array will
# be the last element of given array
suffix[n - 1] = a[n - 1]
for i in range(n - 2, -1, -1):
suffix[i] = suffix[i + 1] + a[i]
# Checking difference of left and right half
# is divisible by k or not.
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
# Driver code
a = [ 1, 2, 3, 3, 1, 1 ]
k = 4
n = len(a)
print(noOfElement(a, n, k))
# This code is contributed by mohit kumar 29
C#
// C# code to count of elements such that
// difference between the sum of left and right
// sub-arrays are equal to a multiple of k
using System;
class GFG
{
// Functions to find the no of elements
static int noOfElement(int []a, int n, int k)
{
// Creating a prefix array
int []prefix = new int[n];
// Starting element of prefix array
// will be the first element
// of given array
prefix[0] = a[0];
for (int i = 1; i < n; i++)
{
prefix[i] = prefix[i - 1] + a[i];
}
// Creating a suffix array;
int []suffix = new int[n];
// Last element of suffix array will
// be the last element of given array
suffix[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--)
{
suffix[i] = suffix[i + 1] + a[i];
}
// Checking difference of left and right half
// is divisible by k or not.
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;
}
// Driver code
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));
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
2