📌  相关文章
📜  即使使用给定的操作,也可以使数组的所有元素

📅  最后修改于: 2021-09-02 07:29:26             🧑  作者: Mango

给定一个由正整数组成的数组arr[] ,找出使所有数组元素即使在以下情况下所需的最少操作次数:

  1. 如果存在奇数,则将该元素和下一个相邻元素加 1。
  2. 每个增量花费一个操作。

注意:如果arr[] 中有任何数字在所有操作后都是奇数,则打印 -1。

例子:

方法:
这个问题可以用贪心方法解决。以下是步骤:

  1. 遍历给定的数组arr[]
  2. 如果出现奇数元素,则将该元素增加 1 使其成为偶数,并将下一个相邻元素增加 1。
  3. 对给定数组arr[] 的所有奇数元素重复上述步骤。
  4. 如果arr[]中的所有元素都是偶数,则打印操作次数。
  5. 否则打印-1。

下面是上述方法的实现:

C++
// C++ program to make all array
// element even
#include "bits/stdc++.h"
using namespace std;
 
// Function to count the total
// number of operations needed to make
// all array element even
int countOperations(int arr[], int n)
{
    int count = 0;
 
    // Traverse the given array
    for (int i = 0; i < n - 1; i++) {
 
        // If an odd element occurs
        // then increment that element
        // and next adjacent element
        // by 1
        if (arr[i] & 1) {
            arr[i]++;
            arr[i + 1]++;
            count += 2;
        }
    }
 
    // Traverse the array if any odd
    // element occurs then return -1
    for (int i = 0; i < n; i++) {
        if (arr[i] & 1)
            return -1;
    }
 
    // Returns the count of operations
    return count;
}
 
int main()
{
    int arr[] = { 2, 3, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(int);
    cout << countOperations(arr, n);
    return 0;
}


Java
// Java program to make all array
// element even
class GFG
{
 
// Function to count the total
// number of operations needed to make
// all array element even
static int countOperations(int arr[], int n)
{
    int count = 0;
 
    // Traverse the given array
    for (int i = 0; i < n - 1; i++)
    {
 
        // If an odd element occurs
        // then increment that element
        // and next adjacent element
        // by 1
        if (arr[i] % 2 == 1)
        {
            arr[i]++;
            arr[i + 1]++;
            count += 2;
        }
    }
 
    // Traverse the array if any odd
    // element occurs then return -1
    for (int i = 0; i < n; i++)
    {
        if (arr[i] % 2 == 1)
            return -1;
    }
 
    // Returns the count of operations
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 3, 4, 5, 6 };
    int n = arr.length;
    System.out.print(countOperations(arr, n));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to make all array
# element even
 
# Function to count the total
# number of operations needed to make
# all array element even
def countOperations(arr, n) :
 
    count = 0;
 
    # Traverse the given array
    for i in range(n - 1) :
 
        # If an odd element occurs
        # then increment that element
        # and next adjacent element
        # by 1
        if (arr[i] & 1) :
            arr[i] += 1;
            arr[i + 1] += 1;
            count += 2;
 
    # Traverse the array if any odd
    # element occurs then return -1
    for i in range(n) :
        if (arr[i] & 1) :
            return -1;
 
    # Returns the count of operations
    return count;
 
if __name__ == "__main__" :
 
    arr = [ 2, 3, 4, 5, 6 ];
    n = len(arr);
    print(countOperations(arr, n));
     
    # This code is contributed by AnkitRai01


C#
// C# program to make all array
// element even
using System;
 
class GFG
{
 
// Function to count the total
// number of operations needed to make
// all array element even
static int countOperations(int []arr, int n)
{
    int count = 0;
 
    // Traverse the given array
    for (int i = 0; i < n - 1; i++)
    {
 
        // If an odd element occurs
        // then increment that element
        // and next adjacent element
        // by 1
        if (arr[i] % 2 == 1)
        {
            arr[i]++;
            arr[i + 1]++;
            count += 2;
        }
    }
 
    // Traverse the array if any odd
    // element occurs then return -1
    for (int i = 0; i < n; i++)
    {
        if (arr[i] % 2 == 1)
            return -1;
    }
 
    // Returns the count of operations
    return count;
}
 
// Driver code
public static void Main()
{
    int []arr = { 2, 3, 4, 5, 6 };
    int n = arr.Length;
    Console.Write(countOperations(arr, n));
}
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
4

时间复杂度: O(N),其中 N 是数组中的元素数。

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