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

📅  最后修改于: 2021-09-06 06:44:51             🧑  作者: Mango

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

例子:

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

  1. 计算给定数组arr[]01的数量,并将它们分别存储在变量中,例如count_0count_1
  2. 如果奇数和偶数索引处的元素之和相等,则无需删除任何数组元素。打印原始数组作为答案。
  3. 如果count_0 ≥ N/2 ,则删除所有1并打印所有零count_0次。
  4. 否则,如果count_0 < N/2 ,通过删除所有0 s,可以根据以下条件使偶数和奇数索引的总和相同:
    • 如果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


Javascript


输出:
1 1

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

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