📌  相关文章
📜  修改给定数组以使奇数和偶数索引元素的总和相同

📅  最后修改于: 2021-05-17 02:14:19             🧑  作者: Mango

给定大小为N的二进制数组arr [] ,请从数组中删除最多N / 2个元素,以使奇数和偶数索引处的元素之和变得相等。任务是打印修改后的数组。
注意: N始终为偶数。可能有多个可能的结果,请打印其中的任何一个。

例子:

方法:想法是计算给定数组中10的数量,然后通过以下步骤使结果总和相等:

  1. 计算给定数组arr []中01的数目,并将它们分别存储在变量count_0count_1中
  2. 如果在奇数和偶数索引处的元素之和相等,则无需删除任何数组元素。打印原始数组作为答案。
  3. 如果count_0≥N / 2 ,则删除所有1 s并打印所有零数字count_0次。
  4. 否则,如果count_0 ,则通过删除所有0 ,可以使偶数和奇数索引的总和与以下条件相同:
    • 如果count_1是奇数,则将1打印为新数组(count_1 – 1)的元素的次数。
    • 否则,将1打印为新数组count_1的元素。
  5. 根据上述条件打印结果数组。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to modify array to make
// sum of odd and even indexed
// elements equal
void makeArraySumEqual(int a[], int N)
{
    // Stores the count of 0s, 1s
    int count_0 = 0, count_1 = 0;
 
    // Stores sum of odd and even
    // indexed elements respectively
    int odd_sum = 0, even_sum = 0;
 
    for (int i = 0; i < N; i++) {
 
        // Count 0s
        if (a[i] == 0)
            count_0++;
 
        // Count 1s
        else
            count_1++;
 
        // Calculate odd_sum and even_sum
        if ((i + 1) % 2 == 0)
            even_sum += a[i];
 
        else if ((i + 1) % 2 > 0)
            odd_sum += a[i];
    }
 
    // If both are equal
    if (odd_sum == even_sum) {
 
        // Print the original array
        for (int i = 0; i < N; i++)
            printf("%d ", a[i]);
    }
 
    // Otherwise
    else {
 
        if (count_0 >= N / 2) {
 
            // Print all the 0s
            for (int i = 0; i < count_0; i++)
                printf("0 ");
        }
        else {
 
            // For checking even or odd
            int is_Odd = count_1 % 2;
 
            // Update total count of 1s
            count_1 -= is_Odd;
 
            // Print all 1s
            for (int i = 0; i < count_1; i++)
                printf("1 ");
        }
    }
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 1, 1, 0 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    makeArraySumEqual(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
 
class GFG {
 
    // Function to modify array to make
    // sum of odd and even indexed
    // elements equal
    static void makeArraySumEqual(int a[], int N)
    {
        // Stores the count of 0s, 1s
        int count_0 = 0, count_1 = 0;
 
        // Stores sum of odd and even
        // indexed elements respectively
        int odd_sum = 0, even_sum = 0;
 
        for (int i = 0; i < N; i++) {
 
            // Count 0s
            if (a[i] == 0)
                count_0++;
 
            // Count 1s
            else
                count_1++;
 
            // Calculate odd_sum and even_sum
            if ((i + 1) % 2 == 0)
                even_sum += a[i];
 
            else if ((i + 1) % 2 > 0)
                odd_sum += a[i];
        }
 
        // If both are equal
        if (odd_sum == even_sum) {
 
            // Print the original array
            for (int i = 0; i < N; i++)
                System.out.print(a[i] + " ");
        }
 
        else
        {
            if (count_0 >= N / 2) {
 
                // Print all the 0s
                for (int i = 0; i < count_0; i++)
                    System.out.print("0 ");
            }
            else {
 
                // For checking even or odd
                int is_Odd = count_1 % 2;
 
                // Update total count of 1s
                count_1 -= is_Odd;
 
                // Print all 1s
                for (int i = 0; i < count_1; i++)
                    System.out.print("1 ");
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Given array arr[]
        int arr[] = { 1, 1, 1, 0 };
 
        int N = arr.length;
 
        // Function Call
        makeArraySumEqual(arr, N);
    }
}


Python3
# Python3 program for the above approach
  
# Function to modify array to make
# sum of odd and even indexed
# elements equal
def makeArraySumEqual(a, N):
     
    # Stores the count of 0s, 1s
    count_0 = 0
    count_1 = 0
  
    # Stores sum of odd and even
    # indexed elements respectively
    odd_sum = 0
    even_sum = 0
  
    for i in range(N):
  
        # Count 0s
        if (a[i] == 0):
            count_0 += 1
  
        # Count 1s
        else:
            count_1 += 1
  
        # Calculate odd_sum and even_sum
        if ((i + 1) % 2 == 0):
            even_sum += a[i]
  
        elif ((i + 1) % 2 > 0):
            odd_sum += a[i]
     
    # If both are equal
    if (odd_sum == even_sum):
  
        # Print the original array
        for i in range(N):
            print(a[i], end = " ")
     
    # Otherwise
    else:
        if (count_0 >= N / 2):
  
            # Print all the 0s
            for i in range(count_0):
                print("0", end = " ")
         
        else:
  
            # For checking even or odd
            is_Odd = count_1 % 2
  
            # Update total count of 1s
            count_1 -= is_Odd
  
            # Print all 1s
            for i in range(count_1):
                print("1", end = " ")
         
# Driver Code
 
# Given array arr[]
arr = [ 1, 1, 1, 0 ]
  
N = len(arr)
  
# Function call
makeArraySumEqual(arr, N)
 
# This code is contributed by code_hunt


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Function to modify array to make
// sum of odd and even indexed
// elements equal
static void makeArraySumEqual(int []a,
                              int N)
{
  // Stores the count of 0s, 1s
  int count_0 = 0, count_1 = 0;
 
  // Stores sum of odd and even
  // indexed elements respectively
  int odd_sum = 0, even_sum = 0;
 
  for (int i = 0; i < N; i++)
  {
    // Count 0s
    if (a[i] == 0)
      count_0++;
 
    // Count 1s
    else
      count_1++;
 
    // Calculate odd_sum and even_sum
    if ((i + 1) % 2 == 0)
      even_sum += a[i];
 
    else if ((i + 1) % 2 > 0)
      odd_sum += a[i];
  }
 
  // If both are equal
  if (odd_sum == even_sum)
  {
    // Print the original array
    for (int i = 0; i < N; i++)
      Console.Write(a[i] + " ");
  }
  else
  {
    if (count_0 >= N / 2)
    {
      // Print all the 0s
      for (int i = 0; i < count_0; i++)
        Console.Write("0 ");
    }
    else
    {
      // For checking even or odd
      int is_Odd = count_1 % 2;
 
      // Update total count of 1s
      count_1 -= is_Odd;
 
      // Print all 1s
      for (int i = 0; i < count_1; i++)
        Console.Write("1 ");
    }
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array []arr
  int []arr = {1, 1, 1, 0};
 
  int N = arr.Length;
 
  // Function Call
  makeArraySumEqual(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


输出:
1 1







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