📌  相关文章
📜  以最小成本使数组元素相等

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

给定一个整数数组arr[] ,任务是通过以下两个操作找到使数组元素相等的最小步数 –

  • 从成本为0的元素中加或减 2
  • 从具有1 个成本的元素中添加或减去 1

例子:

方法:这个想法是利用这样一个事实,即向数组的任何元素添加 2 将花费 0。第二个观察结果是,将 2 添加到任何奇数将导致奇数,类似地将 2 添加到偶数将导致只有偶数。因此,任何元素都可以在零成本下等于最接近的整数,但具有相同的属性,即如果它作为初始值是奇数,它将保持奇数,或者即使它作为初始值也将保持偶数。因此,为了找到最小成本,我们可以找到数组中奇数或偶数元素的最小数量。

算法:

  • 查找数组中奇数或偶数元素的计数(例如count )。
  • 找到 count 和 (length – count) 之间的最小值,其中length是数组的长度。
  • 最小值将是使所有数组元素相等的所需成本。

举例说明:

Given Array be  - {4, 2, 3, 1}
Count of Odd Elements - 2 ({3, 1})
Count of Even Elements - 2 ({4, 2})

Hence, the minimum cost required is 2 and
the array elements can be made equal 
to any integer of even or odd value

下面是上述方法的实现:

C++
// C++ implementation to make
// array elements equal with
// minimum cost
 
#include 
using namespace std;
 
// Function to find the minimum
// cost required to make array
// elements equal
void makearrayequal(int arr[], int n)
{
    int x = 0;
     
    // Loop to find the
    // count of odd elements
    for (int i = 0; i < n; i++) {
        x += arr[i] & 1;
    }
 
    cout << min(x, n - x) << endl;
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 3, 2, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    makearrayequal(arr, n);
    return 0;
}


Java
// Java implementation to make
// array elements equal with
// minimum cost
class GFG {
 
    // Function to find the minimum
    // cost required to make array
    // elements equal
    static void makearrayequal(int arr[], int n)
    {
        int x = 0;
         
        // Loop to find the
        // count of odd elements
        for (int i = 0; i < n; i++) {
            x += (arr[i] & 1);
        }
     
        System.out.println(Math.min(x, n - x));
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int arr[] = { 4, 3, 2, 1 };
        int n = arr.length;
        makearrayequal(arr, n);
        
    }
}
 
// This code is contributed by Yash_R


Python3
# Python3 implementation to make
# array elements equal with
# minimum cost
 
# Function to find the minimum
# cost required to make array
# elements equal
def makearrayequal(arr,  n) :
    x = 0;
     
    # Loop to find the
    # count of odd elements
    for i in range(n) :
        x += arr[i] & 1;
 
    print(min(x, n - x));
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 4, 3, 2, 1 ];
    n = len(arr);
    makearrayequal(arr, n);
   
# This code is contributed by Yash_R


C#
// C# implementation to make
// array elements equal with
// minimum cost
using System;
 
class GFG {
 
    // Function to find the minimum
    // cost required to make array
    // elements equal
    static void makearrayequal(int []arr, int n)
    {
        int x = 0;
         
        // Loop to find the
        // count of odd elements
        for (int i = 0; i < n; i++) {
            x += (arr[i] & 1);
        }
     
        Console.WriteLine(Math.Min(x, n - x));
    }
     
    // Driver Code
    public static void Main (string[] args)
    {
        int []arr = { 4, 3, 2, 1 };
        int n = arr.Length;
        makearrayequal(arr, n);
        
    }
}
 
// This code is contributed by Yash_R


Javascript


输出:
2

性能分析:

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

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