📌  相关文章
📜  使所有成对相邻数组元素的绝对差异甚至所需的最小增量

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

给定一个由N 个整数组成的数组arr[] ,任务是找到需要递增的最小数组元素数,以使所有成对连续元素之间的绝对差相等。

例子:

方法:给定的问题可以通过使用两个数字之间的差是偶数这一事实来解决,当且仅当两个数字都是奇数或偶数时。因此,这个想法是增加所有的奇数或偶数。两个数字都是偶数,对于增量的最小计数,打印奇数计数或偶数计数的最小值是结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number
// of increments of array elements
// required to make difference between
// all pairwise adjacent elements even
int minOperations(int arr[], int n)
{
    // Stores the count of
    // odd and even elements
    int oddcount = 0, evencount = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // Increment odd count
        if (arr[i] % 2 == 1)
            oddcount++;
 
        // Increment even count
        else
            evencount++;
    }
 
    // Return the minimum number
    // of operations required
    return min(oddcount, evencount);
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 4, 3, 1, 8 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << minOperations(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
 
class GFG{
     
// Function to find the minimum number
// of increments of array elements
// required to make difference between
// all pairwise adjacent elements even
static int minOperations(int arr[], int n)
{
     
    // Stores the count of
    // odd and even elements
    int oddcount = 0, evencount = 0;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // Increment odd count
        if (arr[i] % 2 == 1)
            oddcount++;
 
        // Increment even count
        else
            evencount++;
    }
 
    // Return the minimum number
    // of operations required
    return Math.min(oddcount, evencount);
}
 
// Driver code
public static void main (String[] args)
{
    int arr[] = { 2, 4, 3, 1, 8 };
    int N = arr.length;
     
    System.out.println(minOperations(arr, N));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to find the minimum number
# of increments of array elements
# required to make difference between
# all pairwise adjacent elements even
def minOperations(arr, n):
     
    # Stores the count of
    # odd and even elements
    oddcount, evencount = 0, 0
 
    # Traverse the array
    for i in range(n):
         
        # Increment odd count
        if (arr[i] % 2 == 1):
            oddcount += 1
             
        # Increment even count
        else:
            evencount += 1
 
    # Return the minimum number
    # of operations required
    return min(oddcount, evencount)
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 2, 4, 3, 1, 8 ]
    N = len(arr)
     
    print (minOperations(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG {
 
    // Function to find the minimum number
    // of increments of array elements
    // required to make difference between
    // all pairwise adjacent elements even
    static int minOperations(int[] arr, int n)
    {
        // Stores the count of
        // odd and even elements
        int oddcount = 0, evencount = 0;
 
        // Traverse the array
        for (int i = 0; i < n; i++) {
 
            // Increment odd count
            if (arr[i] % 2 == 1)
                oddcount++;
 
            // Increment even count
            else
                evencount++;
        }
 
        // Return the minimum number
        // of operations required
        return Math.Min(oddcount, evencount);
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 2, 4, 3, 1, 8 };
        int N = (arr.Length);
        Console.WriteLine(minOperations(arr, N));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
2

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

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