📌  相关文章
📜  计算偶数和奇数位置元素总和相等的子数组

📅  最后修改于: 2021-09-07 02:04:56             🧑  作者: 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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live