📌  相关文章
📜  最小化使所有数组元素等于1所需执行的给定操作的计数

📅  最后修改于: 2021-04-26 10:50:24             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] ,任务是通过执行以下最小次数的运算来使所有数组元素等于1

  • 将子数组的所有元素的值增加任何正整数。
  • 如果所有数组元素均是偶数,则将所有数组元素除以2

打印所需的最少操作数。

例子:

方法:可以使用贪婪技术解决问题。请按照以下步骤解决问题:

  • 找到数组中最大的元素,例如Max
  • 如果所有数组元素都相等并且也为2的幂,则输出log 2 (Max)
  • 否则,使所有数组元素等于最接近的2的幂(大于或等于Max),并输出log 2 (Max) + 1。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if all array
// elements are equal or not
bool CheckAllEqual(int arr[], int N)
{
 
    // Traverse the array
    for (int i = 1; i < N; i++) {
 
        // If all array elements
        // are not equal
        if (arr[0] != arr[i]) {
 
            return false;
        }
    }
 
    return true;
}
 
// Function to find minimum count of operation
// to make all the array elements equal to 1
int minCntOperations(int arr[], int N)
{
 
    // Stores largest element of the array
    int Max = *max_element(arr, arr + N);
 
    // Check if a number is a power of 2 or not
    bool isPower2 = !(Max && (Max & (Max - 1)));
 
    // If Max is a power of 2 and all
    // array elements are equal
    if (isPower2 && CheckAllEqual(arr, N)) {
 
        return log2(Max);
    }
    else {
 
        return ceil(log2(Max)) + 1;
    }
}
 
// Driver Code
int main()
{
 
    int arr[] = { 2, 4 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << minCntOperations(arr, N);
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
    // Function to check if all array
    // elements are equal or not
    static boolean CheckAllEqual(int arr[], int N)
    {
 
        // Traverse the array
        for (int i = 1; i < N; i++)
        {
 
            // If all array elements
            // are not equal
            if (arr[0] != arr[i])
            {
                return false;
            }
        }
        return true;
    }
 
    // Function to find minimum count of operation
    // to make all the array elements equal to 1
    static int minCntOperations(int arr[], int N)
    {
 
        // Stores largest element of the array
        int Max = Arrays.stream(arr).max().getAsInt();
 
        // Check if a number is a power of 2 or not
        boolean isPower2;
        if ((int)(Math.ceil((Math.log(N) / Math.log(N))))
            == (int)(Math.floor(((Math.log(N) / Math.log(2))))))
        {
            isPower2 = true;
        }
        else
        {
            isPower2 = false;
        }
 
        // If Max is a power of 2 and all
        // array elements are equal
        if (isPower2 && CheckAllEqual(arr, N))
        {
            return (int)(Math.log(Max) / Math.log(2));
        }
        else
        {
            return (int)Math.ceil(Math.log(Max)
                                  / Math.log(2)) + 1;
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = new int[] { 2, 4 };
        int N = arr.length;
        System.out.println(minCntOperations(arr, N));
    }
}
 
// This code is contributed by Dharanendra L V


Python3
# Python 3 program to implement
# the above approach
import math
 
# Function to check if all array
# elements are equal or not
def CheckAllEqual(arr, N):
 
    # Traverse the array
    for i in range(1, N):
 
        # If all array elements
        # are not equal
        if (arr[0] != arr[i]):
            return False
    return True
 
# Function to find minimum count of operation
# to make all the array elements equal to 1
def minCntOperations(arr, N):
 
    # Stores largest element of the array
    Max = max(arr)
 
    # Check if a number is a power of 2 or not
    isPower2 = not (Max and (Max & (Max - 1)))
 
    # If Max is a power of 2 and all
    # array elements are equal
    if (isPower2 and CheckAllEqual(arr, N)):
        return log2(Max)
    else:
        return math.ceil(math.log2(Max)) + 1
 
# Driver Code
if __name__ == "__main__":
    arr = [2, 4]
    N = len(arr)
    print(minCntOperations(arr, N))
 
    # This code is contributed by chitranayal.


C#
// C# program to implement
// the above approach
using System;
using System.Linq;
class GFG {
 
  // Function to check if all array
  // elements are equal or not
  static bool CheckAllEqual(int[] arr, int N)
  {
 
    // Traverse the array
    for (int i = 1; i < N; i++)
    {
 
      // If all array elements
      // are not equal
      if (arr[0] != arr[i])
      {
        return false;
      }
    }
    return true;
  }
 
  // Function to find minimum count of operation
  // to make all the array elements equal to 1
  static int minCntOperations(int[] arr, int N)
  {
 
    // Stores largest element of the array
    int Max = arr.Max();
 
    // Check if a number is a power of 2 or not
    bool isPower2;
    if ((int)(Math.Ceiling((Math.Log(N) / Math.Log(N))))
        == (int)(Math.Floor(((Math.Log(N) / Math.Log(2))))))
    {
      isPower2 = true;
    }
    else
    {
      isPower2 = false;
    }
 
    // If Max is a power of 2 and all
    // array elements are equal
    if (isPower2 && CheckAllEqual(arr, N))
    {
      return (int)(Math.Log(Max) / Math.Log(2));
    }
    else
    {
      return (int)Math.Ceiling(Math.Log(Max)
                               / Math.Log(2)) + 1;
    }
  }
 
  // Driver Code
  static public void Main()
  {
    int[] arr = new int[] { 2, 4 };
    int N = arr.Length;
    Console.WriteLine(minCntOperations(arr, N));
  }
}
 
// This code is contributed by Dharanendra L V


输出:
3

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