📌  相关文章
📜  计数在偶数和奇数位置具有相等的元素之和的子数组

📅  最后修改于: 2021-05-14 08:57:14             🧑  作者: Mango

给定整数数组arr [] ,任务是找到子数组的总数,以使偶数位置的元素之和与奇数位置的元素之和相等。

例子:

方法:想法是生成所有可能的子数组。对于形成的每个子数组,找到偶数索引处的元素之和,然后减去奇数索引处的元素。如果总和为0,则计数此子数组,否则检查下一个子数组。

下面是上述方法的实现:

C++
// C program for the above approach
#include 
using namespace std;
 
// Function to count subarrays in
// which sum of elements at even
// and odd positions are equal
void countSubarrays(int arr[], int n)
{
     
    // Initialize variables
    int count = 0;
 
    // Iterate over the array
    for(int i = 0; i < n; i++)
    {
        int sum = 0;
 
        for(int j = i; j < n; j++)
        {
             
            // Check if position is
            // even then add to sum
            // then add it to sum
            if ((j - i) % 2 == 0)
                sum += arr[j];
 
            // Else subtract it to sum
            else
                sum -= arr[j];
 
            // Increment the count
            // if the sum equals 0
            if (sum == 0)
                count++;
        }
    }
 
    // Print the count of subarrays
    cout << " " << count ;
}
 
// Driver Code
int main()
{
     
    // Given array arr[]
    int arr[] = { 2, 4, 6, 4, 2 };
 
    // Size of the array
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    countSubarrays(arr, n);
    return 0;
}
 
// This code is contributed by shivanisinghss2110


C
// C program for the above approach
#include 
 
// Function to count subarrays in
// which sum of elements at even
// and odd positions are equal
void countSubarrays(int arr[], int n)
{
     
    // Initialize variables
    int count = 0;
 
    // Iterate over the array
    for(int i = 0; i < n; i++)
    {
        int sum = 0;
 
        for(int j = i; j < n; j++)
        {
             
            // Check if position is
            // even then add to sum
            // then add it to sum
            if ((j - i) % 2 == 0)
                sum += arr[j];
 
            // Else subtract it to sum
            else
                sum -= arr[j];
 
            // Increment the count
            // if the sum equals 0
            if (sum == 0)
                count++;
        }
    }
 
    // Print the count of subarrays
    printf("%d", count);
}
 
// Driver Code
int main()
{
     
    // Given array arr[]
    int arr[] = { 2, 4, 6, 4, 2 };
 
    // Size of the array
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    countSubarrays(arr, n);
    return 0;
}
 
// This code is contributed by piyush3010


Java
// Java program for the above approach
import java.util.*;
class GFG {
 
    // Function to count subarrays in
    // which sum of elements at even
    // and odd positions are equal
    static void countSubarrays(int arr[],
                               int n)
    {
        // Initialize variables
        int count = 0;
 
        // Iterate over the array
        for (int i = 0; i < n; i++) {
            int sum = 0;
 
            for (int j = i; j < n; j++) {
 
                // Check if position is
                // even then add to sum
                // then add it to sum
                if ((j - i) % 2 == 0)
                    sum += arr[j];
 
                // else subtract it to sum
                else
                    sum -= arr[j];
 
                // Increment the count
                // if the sum equals 0
                if (sum == 0)
 
                    count++;
            }
        }
 
        // Print the count of subarrays
        System.out.println(count);
    }
 
    // Driver Code
    public static void
        main(String[] args)
    {
        // Given array arr[]
        int arr[] = { 2, 4, 6, 4, 2 };
 
        // Size of the array
        int n = arr.length;
 
        // Function call
        countSubarrays(arr, n);
    }
}


Python3
# Python3 program for the above approach
 
# Function to count subarrays in
# which sum of elements at even
# and odd positions are equal
def countSubarrays(arr, n):
 
    # Initialize variables
    count = 0
 
    # Iterate over the array
    for i in range(n):
        sum = 0
         
        for j in range(i, n):
 
            # Check if position is
            # even then add to sum
            # hen add it to sum
            if ((j - i) % 2 == 0):
                sum += arr[j]
 
            # else subtract it to sum
            else:
                sum -= arr[j]
 
            # Increment the count
            # if the sum equals 0
            if (sum == 0):
                count += 1
                 
    # Print the count of subarrays
    print(count)
 
# Driver Code
if __name__ == '__main__':
   
    # Given array arr[]
    arr = [ 2, 4, 6, 4, 2 ]
 
    # Size of the array
    n = len(arr)
 
    # Function call
    countSubarrays(arr, n)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count subarrays in
// which sum of elements at even
// and odd positions are equal
static void countSubarrays(int []arr, int n)
{
     
    // Initialize variables
    int count = 0;
 
    // Iterate over the array
    for(int i = 0; i < n; i++)
    {
        int sum = 0;
 
        for(int j = i; j < n; j++)
        {
             
            // Check if position is
            // even then add to sum
            // then add it to sum
            if ((j - i) % 2 == 0)
                sum += arr[j];
 
            // else subtract it to sum
            else
                sum -= arr[j];
 
            // Increment the count
            // if the sum equals 0
            if (sum == 0)
                count++;
        }
    }
 
    // Print the count of subarrays
    Console.WriteLine(count);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array []arr
    int []arr = { 2, 4, 6, 4, 2 };
 
    // Size of the array
    int n = arr.Length;
 
    // Function call
    countSubarrays(arr, n);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
2

时间复杂度: O(N 2 )
辅助空间: O(1)