📌  相关文章
📜  通过将数组元素重复除以 2 最多 K 次来最大化数组中 1 的计数

📅  最后修改于: 2021-09-04 11:20:52             🧑  作者: Mango

给定一个大小为N的数组arr[]和一个整数K ,任务是通过重复将任何元素除以 2 至多K次来找到可以减少到1的数组元素的最大数量。
注意:对于奇数数组元素,取其除法的ceil值。

例子:

方法:为了最大化元素数量,想法是按升序对数组进行排序,并从第一个索引开始减少元素,并按照减少第i元素所需的操作次数减少K。请按照以下步骤解决问题:

  • 初始化一个变量,比如cnt ,以存储所需数量的元素。
  • 按升序对数组arr[]进行排序。
  • 使用变量i遍历数组arr[] ,并执行以下步骤:
    • 存储将arr[i]减少到1所需的操作数是opr = ceil( log2(arr[i]) )
    • opr递减K。
    • 如果K的值小于0 ,则跳出循环。否则,将cnt增加1
  • 完成以上步骤后,打印cnt的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the maximum number of
// array elements that can be reduced to 1
// by repeatedly dividing array elements by 2
void findMaxNumbers(int arr[], int n, int k)
{
    // Sort the array in ascending order
    sort(arr, arr + n);
 
    // Store the count of array elements
    int cnt = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // Store the number of operations
        // required to reduce arr[i] to 1
        int opr = ceil(log2(arr[i]));
 
        // Decrement k by opr
        k -= opr;
 
        // If k becomes less than 0,
        // then break out of the loop
        if (k < 0) {
            break;
        }
 
        // Increment cnt by 1
        cnt++;
    }
 
    // Print the answer
    cout << cnt;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 8, 4, 7 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 5;
 
    findMaxNumbers(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG
{
 
// Function to count the maximum number of
// array elements that can be reduced to 1
// by repeatedly dividing array elements by 2
static void findMaxNumbers(int arr[], int n, int k)
{
   
    // Sort the array in ascending order
    Arrays.sort(arr);
 
    // Store the count of array elements
    int cnt = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
        // Store the number of operations
        // required to reduce arr[i] to 1
        int opr = (int)Math.ceil((Math.log(arr[i]) / Math.log(2)));
 
        // Decrement k by opr
        k -= opr;
 
        // If k becomes less than 0,
        // then break out of the loop
        if (k < 0) {
            break;
        }
 
        // Increment cnt by 1
        cnt++;
    }
 
    // Print the answer
    System.out.println(cnt);
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 5, 8, 4, 7 };
    int N = arr.length;
    int K = 5;
    findMaxNumbers(arr, N, K);
}
}
 
// This code is contributed by jana_sayantan.


Python3
# Python3 program to implement
# the above approach
import math
 
# Function to count the maximum number of
# array elements that can be reduced to 1
# by repeatedly dividing array elements by 2
def findMaxNumbers(arr, n, k) :
     
    # Sort the array in ascending order
    arr.sort()
 
    # Store the count of array elements
    cnt = 0
 
    # Traverse the array
    for i in range(n):
 
        # Store the number of operations
        # required to reduce arr[i] to 1
        opr = math.ceil(math.log2(arr[i]))
 
        # Decrement k by opr
        k -= opr
 
        # If k becomes less than 0,
        # then break out of the loop
        if (k < 0) :
            break
         
        # Increment cnt by 1
        cnt += 1
     
    # Prthe answer
    print(cnt)
 
# Driver Code
arr = [ 5, 8, 4, 7 ]
N = len(arr)
K = 5
 
findMaxNumbers(arr, N, K)
 
# This code is contributed by splevel62.


C#
// C# program for the above approach
using System;
public class GFG
{
   
// Function to count the maximum number of
// array elements that can be reduced to 1
// by repeatedly dividing array elements by 2
static void findMaxNumbers(int[] arr, int n, int k)
{
   
    // Sort the array in ascending order
    Array.Sort(arr);
 
    // Store the count of array elements
    int cnt = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
        // Store the number of operations
        // required to reduce arr[i] to 1
        int opr = (int)Math.Ceiling((Math.Log(arr[i]) / Math.Log(2)));
 
        // Decrement k by opr
        k -= opr;
 
        // If k becomes less than 0,
        // then break out of the loop
        if (k < 0) {
            break;
        }
 
        // Increment cnt by 1
        cnt++;
    }
 
    // Print the answer
    Console.Write(cnt);
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] arr = { 5, 8, 4, 7 };
    int N = arr.Length;
    int K = 5;
    findMaxNumbers(arr, N, K);
}
}
 
// This code is contributed by susmitakundugoaldanga.


Javascript


输出:
2

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

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