📌  相关文章
📜  通过删除数组元素来使奇数和偶数索引元素之和相等的计数方法

📅  最后修改于: 2021-04-24 22:06:50             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是查找数组索引的数量,以使从这些索引中删除元素使偶数索引和奇数索引的数组元素之和相等。

例子:

天真的方法:解决此问题的最简单方法是遍历数组,对于每个数组元素,检查从数组中删除元素是否使偶数索引和奇数索引的数组元素之和相等。如果发现为真,则增加计数。最后,打印计数。

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

高效方法:可以基于以下观察条件来优化上述方法从给定数组中删除任何元素,使后续元素的偶数索引为奇数,而后续元素的奇数索引为偶数。请按照以下步骤解决问题:

  • 初始化两个变量,例如evenSumoddSum ,分别存储给定数组的奇数索引元素和偶数索引元素的总和。
  • 使用变量i遍历数组。
  • 根据上面的观察,删除数组的i元素,并更新其余的偶数索引元素和奇数索引元素的总和。检查总和是否相等。如果发现为真,则增加计数。
  • 最后,打印获得的计数。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to count array indices
// whose removal makes sum of odd and
// even indexed elements equal
int cntIndexesToMakeBalance(int arr[], int n)
{
 
    // If size of the array is 1
    if (n == 1) {
        return 1;
    }
 
    // If size of the array is 2
    if (n == 2)
        return 0;
 
    // Stores sum of even-indexed
    // elements of the given array
    int sumEven = 0;
 
    // Stores sum of odd-indexed
    // elements of the given array
    int sumOdd = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // If i is an even number
        if (i % 2 == 0) {
 
            // Update sumEven
            sumEven += arr[i];
        }
 
        // If i is an odd number
        else {
 
            // Update sumOdd
            sumOdd += arr[i];
        }
    }
 
    // Stores sum of even-indexed
    // array elements till i-th index
    int currOdd = 0;
 
    // Stores sum of odd-indexed
    // array elements till i-th index
    int currEven = arr[0];
 
    // Stores count of indices whose
    // removal makes sum of odd and
    // even indexed elements equal
    int res = 0;
 
    // Stores sum of even-indexed elements
    // after removing the i-th element
    int newEvenSum = 0;
 
    // Stores sum of odd-indexed elements
    // after removing the i-th element
    int newOddSum = 0;
 
    // Traverse the array
    for (int i = 1; i < n - 1; i++) {
 
        // If i is an odd number
        if (i % 2) {
 
            // Update currOdd
            currOdd += arr[i];
 
            // Update newEvenSum
            newEvenSum = currEven + sumOdd
                         - currOdd;
 
            // Update newOddSum
            newOddSum = currOdd + sumEven
                        - currEven - arr[i];
        }
 
        // If i is an even number
        else {
 
            // Update currEven
            currEven += arr[i];
 
            // Update newOddSum
            newOddSum = currOdd + sumEven
                        - currEven;
 
            // Update newEvenSum
            newEvenSum = currEven + sumOdd
                         - currOdd - arr[i];
        }
 
        // If newEvenSum is equal to newOddSum
        if (newEvenSum == newOddSum) {
 
            // Increase the count
            res++;
        }
    }
 
    // If sum of even-indexed and odd-indexed
    // elements is equal by removing the last element
    if (sumOdd == sumEven - arr[0]) {
 
        // Increase the count
        res++;
    }
 
    // If length of the array
    // is an odd number
    if (n % 2 == 1) {
 
        // If sum of even-indexed and odd-indexed
        // elements is equal by removing the last element
        if (sumOdd == sumEven - arr[n - 1]) {
 
            // Increase the count
            res++;
        }
    }
 
    // If length of the array
    // is an even number
    else {
 
        // If sum of even-indexed and odd-indexed
        // elements is equal by removing the last element
        if (sumEven == sumOdd - arr[n - 1]) {
 
            // Increase the count
            res++;
        }
    }
 
    return res;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 1, 1, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << cntIndexesToMakeBalance(arr, n);
    return 0;
}


Java
// Java program to implement
// the above approach
class GFG {
     
    // Function to count array indices
    // whose removal makes sum of odd and
    // even indexed elements equal
    static int cntIndexesToMakeBalance(int arr[], int n)
    {
     
        // If size of the array is 1
        if (n == 1)
        {
            return 1;
        }
     
        // If size of the array is 2
        if (n == 2)
            return 0;
     
        // Stores sum of even-indexed
        // elements of the given array
        int sumEven = 0;
     
        // Stores sum of odd-indexed
        // elements of the given array
        int sumOdd = 0;
     
        // Traverse the array
        for (int i = 0; i < n; i++)
        {
     
            // If i is an even number
            if (i % 2 == 0)
            {
     
                // Update sumEven
                sumEven += arr[i];
            }
     
            // If i is an odd number
            else
            {
     
                // Update sumOdd
                sumOdd += arr[i];
            }
        }
     
        // Stores sum of even-indexed
        // array elements till i-th index
        int currOdd = 0;
     
        // Stores sum of odd-indexed
        // array elements till i-th index
        int currEven = arr[0];
     
        // Stores count of indices whose
        // removal makes sum of odd and
        // even indexed elements equal
        int res = 0;
     
        // Stores sum of even-indexed elements
        // after removing the i-th element
        int newEvenSum = 0;
     
        // Stores sum of odd-indexed elements
        // after removing the i-th element
        int newOddSum = 0;
     
        // Traverse the array
        for (int i = 1; i < n - 1; i++)
        {
     
            // If i is an odd number
            if (i % 2 != 0)
            {
     
                // Update currOdd
                currOdd += arr[i];
     
                // Update newEvenSum
                newEvenSum = currEven + sumOdd
                             - currOdd;
     
                // Update newOddSum
                newOddSum = currOdd + sumEven
                            - currEven - arr[i];
            }
     
            // If i is an even number
            else
            {
     
                // Update currEven
                currEven += arr[i];
     
                // Update newOddSum
                newOddSum = currOdd + sumEven
                            - currEven;
     
                // Update newEvenSum
                newEvenSum = currEven + sumOdd
                             - currOdd - arr[i];
            }
     
            // If newEvenSum is equal to newOddSum
            if (newEvenSum == newOddSum)
            {
     
                // Increase the count
                res++;
            }
        }
     
        // If sum of even-indexed and odd-indexed
        // elements is equal by removing the last element
        if (sumOdd == sumEven - arr[0])
        {
     
            // Increase the count
            res++;
        }
     
        // If length of the array
        // is an odd number
        if (n % 2 == 1)
        {
     
            // If sum of even-indexed and odd-indexed
            // elements is equal by removing the last element
            if (sumOdd == sumEven - arr[n - 1])
            {
     
                // Increase the count
                res++;
            }
        }
     
        // If length of the array
        // is an even number
        else
        {
     
            // If sum of even-indexed and odd-indexed
            // elements is equal by removing the last element
            if (sumEven == sumOdd - arr[n - 1])
            {
     
                // Increase the count
                res++;
            }
        }
     
        return res;
    }
     
    // Driver Code
   public static void main (String[] args)
    {  
        int arr[] = { 1, 1, 1 };
        int n = arr.length;
        System.out.println(cntIndexesToMakeBalance(arr, n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program to implement
# the above approach
 
# Function to count array indices
# whose removal makes sum of odd and
# even indexed elements equal
def cntIndexesToMakeBalance(arr, n):
 
    # If size of the array is 1
    if (n == 1):
        return 1
 
    # If size of the array is 2
    if (n == 2):
        return 0
 
    # Stores sum of even-indexed
    # elements of the given array
    sumEven = 0
 
    # Stores sum of odd-indexed
    # elements of the given array
    sumOdd = 0
 
    # Traverse the array
    for i in range(n):
 
        # If i is an even number
        if (i % 2 == 0):
 
            # Update sumEven
            sumEven += arr[i]
 
        # If i is an odd number
        else:
 
            # Update sumOdd
            sumOdd += arr[i]
 
    # Stores sum of even-indexed
    # array elements till i-th index
    currOdd = 0
 
    # Stores sum of odd-indexed
    # array elements till i-th index
    currEven = arr[0]
 
    # Stores count of indices whose
    # removal makes sum of odd and
    # even indexed elements equal
    res = 0
     
    # Stores sum of even-indexed elements
    # after removing the i-th element
    newEvenSum = 0
 
    # Stores sum of odd-indexed elements
    # after removing the i-th element
    newOddSum = 0
 
    # Traverse the array
    for i in range(1, n - 1):
 
        # If i is an odd number
        if (i % 2):
             
            # Update currOdd
            currOdd += arr[i]
 
            # Update newEvenSum
            newEvenSum = (currEven + sumOdd -
                          currOdd)
 
            # Update newOddSum
            newOddSum = (currOdd + sumEven -
                        currEven - arr[i])
 
        # If i is an even number
        else:
 
            # Update currEven
            currEven += arr[i]
 
            # Update newOddSum
            newOddSum = (currOdd + sumEven -
                         currEven)
 
            # Update newEvenSum
            newEvenSum = (currEven + sumOdd -
                           currOdd - arr[i])
         
        # If newEvenSum is equal to newOddSum
        if (newEvenSum == newOddSum):
 
            # Increase the count
            res += 1
 
    # If sum of even-indexed and odd-indexed
    # elements is equal by removing the last
    # element
    if (sumOdd == sumEven - arr[0]):
 
        # Increase the count
        res += 1
 
    # If length of the array
    # is an odd number
    if (n % 2 == 1):
 
        # If sum of even-indexed and odd-indexed
        # elements is equal by removing the last
        # element
        if (sumOdd == sumEven - arr[n - 1]):
 
            # Increase the count
            res += 1
 
    # If length of the array
    # is an even number
    else:
 
        # If sum of even-indexed and odd-indexed
        # elements is equal by removing the last
        # element
        if (sumEven == sumOdd - arr[n - 1]):
 
            # Increase the count
            res += 1
 
    return res
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 1, 1, 1 ]
    n = len(arr)
     
    print(cntIndexesToMakeBalance(arr, n))
     
# This code is contributed by AnkitRai01


C#
// C# program to implement
// the above approach 
using System;
 
class GFG {
      
    // Function to count array indices
    // whose removal makes sum of odd and
    // even indexed elements equal
    static int cntIndexesToMakeBalance(int[] arr, int n)
    {
      
        // If size of the array is 1
        if (n == 1)
        {
            return 1;
        }
      
        // If size of the array is 2
        if (n == 2)
            return 0;
      
        // Stores sum of even-indexed
        // elements of the given array
        int sumEven = 0;
      
        // Stores sum of odd-indexed
        // elements of the given array
        int sumOdd = 0;
      
        // Traverse the array
        for (int i = 0; i < n; i++)
        {
      
            // If i is an even number
            if (i % 2 == 0)
            {
      
                // Update sumEven
                sumEven += arr[i];
            }
      
            // If i is an odd number
            else
            {
      
                // Update sumOdd
                sumOdd += arr[i];
            }
        }
      
        // Stores sum of even-indexed
        // array elements till i-th index
        int currOdd = 0;
      
        // Stores sum of odd-indexed
        // array elements till i-th index
        int currEven = arr[0];
      
        // Stores count of indices whose
        // removal makes sum of odd and
        // even indexed elements equal
        int res = 0;
      
        // Stores sum of even-indexed elements
        // after removing the i-th element
        int newEvenSum = 0;
      
        // Stores sum of odd-indexed elements
        // after removing the i-th element
        int newOddSum = 0;
      
        // Traverse the array
        for (int i = 1; i < n - 1; i++)
        {
      
            // If i is an odd number
            if (i % 2 != 0)
            {
      
                // Update currOdd
                currOdd += arr[i];
      
                // Update newEvenSum
                newEvenSum = currEven + sumOdd
                             - currOdd;
      
                // Update newOddSum
                newOddSum = currOdd + sumEven
                            - currEven - arr[i];
            }
      
            // If i is an even number
            else
            {
      
                // Update currEven
                currEven += arr[i];
      
                // Update newOddSum
                newOddSum = currOdd + sumEven
                            - currEven;
      
                // Update newEvenSum
                newEvenSum = currEven + sumOdd
                             - currOdd - arr[i];
            }
      
            // If newEvenSum is equal to newOddSum
            if (newEvenSum == newOddSum)
            {
      
                // Increase the count
                res++;
            }
        }
      
        // If sum of even-indexed and odd-indexed
        // elements is equal by removing the last element
        if (sumOdd == sumEven - arr[0])
        {
      
            // Increase the count
            res++;
        }
      
        // If length of the array
        // is an odd number
        if (n % 2 == 1)
        {
      
            // If sum of even-indexed and odd-indexed
            // elements is equal by removing the last element
            if (sumOdd == sumEven - arr[n - 1])
            {
      
                // Increase the count
                res++;
            }
        }
      
        // If length of the array
        // is an even number
        else
        {
      
            // If sum of even-indexed and odd-indexed
            // elements is equal by removing the last element
            if (sumEven == sumOdd - arr[n - 1])
            {
      
                // Increase the count
                res++;
            }
        }
      
        return res;
    }
      
    // Drivers Code
    public static void Main ()
    {
        int[] arr = { 1, 1, 1 };
        int n = arr.Length;
        Console.WriteLine(cntIndexesToMakeBalance(arr, n));   
    }
  
}
 
// This code is contributed by susmitakundugoaldanga


Javascript


输出:
3

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