给定一个由N 个正整数组成的数组arr[] ,任务是通过执行以下操作的最少次数使所有数组元素等于1 :
- 将子数组的所有元素的值增加任何正整数。
- 如果所有数组元素都是偶数,则将所有数组元素除以2 。
打印所需的最少操作数。
例子:
Input: arr[] = { 3, 1, 2, 4 }
Output: 3
Explanation:
Incrementing arr[0] by 1, arr[1] by 3 and arr[2] by 2 modifies arr[] to { 4, 4, 4, 4 }.
Dividing all the array elements by 2 modifies arr[] to { 2, 2, 2, 2 }.
Dividing all the array elements by 2 modifies arr[] to { 1, 1, 1, 1 }.
Therefore, the required output is 3.
Input: arr[] = { 2, 4 }
Output: 3
Explanation:
Dividing all the array elements by 2 modifies arr[] to { 1, 2 }.
Incrementing the value of arr[0] by 1 modifies arr[] to { 2, 2 }.
Dividing all the array elements by 2 modifies arr[] to { 1, 2 }.
Therefore, the required output is 3.
方法:该问题可以使用贪心技术解决。请按照以下步骤解决问题:
- 找到数组的最大元素,比如Max 。
- 如果所有数组元素都相等并且也是2的幂的形式,则打印log 2 (Max) 。
- 否则,使所有数组元素等于大于或等于Max的最接近的2 次幂并打印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
Javascript
3
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live