📌  相关文章
📜  通过将数组元素的数字右移为超过 K 的值来修改数组

📅  最后修改于: 2021-09-03 13:49:33             🧑  作者: Mango

给定一个大小为N的数组arr[]和一个整数K ,任务是通过对数组元素执行右移操作使所有数组元素> K。
注意:如果不可能使所有数组元素> K ,则打印-1

例子:

处理方法:按照以下步骤解决问题:

  1. 遍历数组
  2. 对每个数组元素arr[i] 的数字执行右移操作。
  3. 如果arr[i] > k ,更新arr[i]并继续。
  4. 如果任何数组元素arr[i] ≤ K,则打印-1
  5. 否则,打印数组arr[]

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find
// MSB of a number
int setBitNumber(int n)
{
 
    // Stores the position of the
    // most significant set bit
    int k = log2(n);
 
    // Return the position of the
    // most significant set bit
    return k;
}
 
// Function to check if all array
// elements are exceeding k or not
bool check(int arr[], int k, int n)
{
 
    // Traverse the array arr
    for (int i = 0; i < n; i++) {
 
        // If current element
        // does not exceed k
        if (arr[i] <= k)
            return false;
    }
 
    return true;
}
 
// Function to modify array by
// right shifting digits such that
// all array elements are exceeding k
void modifyArray(int arr[], int k, int n)
{
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // If current element
        // exceeds k
        if (arr[i] > k)
            continue;
 
        else {
 
            // Count bits in the binary
            // representation of arr[i]
 
            // MSB of arr[i]
            int bits = setBitNumber(arr[i]);
 
            int el = arr[i];
 
            // Perform right shift operations
            for (int j = 0; j < bits; j++) {
 
                // If the current element is odd
                if (el & 1) {
 
                    // Right shift the element
                    el >>= 1;
 
                    // Perform OR with the bitmask
                    el |= (1 << bits);
                }
 
                // If the current element is even
                else {
 
                    // Right shift the element
                    el >>= 1;
                }
 
                // If current element exceeds k
                if (el > k) {
                    arr[i] = el;
                    break;
                }
            }
        }
    }
 
    // Check if all array elements
    // are greater than k or not
    if (check(arr, k, n)) {
        for (int i = 0; i < n; i++)
            cout << arr[i] << " ";
    }
    else
        cout << -1;
}
 
// Driver Code
int main()
{
    int arr[] = { 21, 22, 23, 19 };
 
    // Size of array
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int k = 24;
 
    modifyArray(arr, k, n);
    return 0;
}
 
// This code is contributed by subhammahato348.


Java
// Java program to implement
// the above approach
public class GFG
{
 
  // Function to find
  // MSB of a number
  static int setBitNumber(int n)
  {
 
    // Stores the position of the
    // most significant set bit
    int k = (int)(Math.log(n) / Math.log(2));
 
    // Return the position of the
    // most significant set bit
    return k;
  }
 
  // Function to check if all array
  // elements are exceeding k or not
  static boolean check(int[] arr, int k, int n)
  {
 
    // Traverse the array arr
    for (int i = 0; i < n; i++)
    {
 
      // If current element
      // does not exceed k
      if (arr[i] <= k)
        return false;
    }
 
    return true;
  }
 
  // Function to modify array by
  // right shifting digits such that
  // all array elements are exceeding k
  static void modifyArray(int[] arr, int k, int n)
  {
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
      // If current element
      // exceeds k
      if (arr[i] > k)
        continue;
      else
      {
 
        // Count bits in the binary
        // representation of arr[i]
 
        // MSB of arr[i]
        int bits = setBitNumber(arr[i]);
        int el = arr[i];
 
        // Perform right shift operations
        for (int j = 0; j < bits; j++)
        {
 
          // If the current element is odd
          if ((el & 1) != 0)
          {
 
            // Right shift the element
            el >>= 1;
 
            // Perform OR with the bitmask
            el |= (1 << bits);
          }
 
          // If the current element is even
          else
          {
 
            // Right shift the element
            el >>= 1;
          }
 
          // If current element exceeds k
          if (el > k)
          {
            arr[i] = el;
            break;
          }
        }
      }
    }
 
    // Check if all array elements
    // are greater than k or not
    if (check(arr, k, n)) {
      for (int i = 0; i < n; i++)
        System.out.print(arr[i] + " ");
    }
    else
      System.out.print("-1");
  }
 
  // Driver code
  public static void main(String[] args) {
    int[] arr = { 21, 22, 23, 19 };
 
    // Size of array
    int n = arr.length;
    int k = 24;
    modifyArray(arr, k, n);
  }
}
 
// This code is conntrributed by divyesh072019.


Python3
# Python program to implement
# the above approach
import math
 
# Function to find
# MSB of a number
def setBitNumber(n):
 
    # Stores the position of the
    # most significant set bit
    k = int(math.log(n, 2))
 
    # Return the position of the
    # most significant set bit
    return k
 
# Function to check if all array
# elements are exceeding k or not
def check(arr, k):
 
    # Traverse the array arr
    for el in arr:
 
        # If current element
        # does not exceed k
        if el <= k:
            return False
    return True
 
# Function to modify array by
# right shifting digits such that
# all array elements are exceeding k
def modifyArray(arr, k):
 
    # Traverse the array
    for i in range(len(arr)):
 
        # If current element
        # exceeds k
        if arr[i] > k:
            continue
        else:
 
            # Count bits in the binary
            # representation of arr[i]
 
            # MSB of arr[i]
            bits = setBitNumber(arr[i])
            el = arr[i]
 
            # Perform right shift operations
            for j in range(bits):
 
                # If the current element is odd
                if el & 1:
 
                    # Right shift the element
                    el >>= 1
 
                    # Perform OR with the bitmask
                    el |= (1 << bits)
 
                # If the current element is even
                else:
 
                    # Right shift the element
                    el >>= 1
 
                # If current element exceeds k
                if el > k:
                    arr[i] = el
                    break
 
    # Check if all array elements
    # are greater than k or not
    if check(arr, k):
        return arr
    else:
        return -1
 
# Driver Code
 
arr = [21, 22, 23, 19]
k = 24
 
print(modifyArray(arr, k))


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
  // Function to find
  // MSB of a number
  static int setBitNumber(int n)
  {
 
    // Stores the position of the
    // most significant set bit
    int k = (int)(Math.Log(n, 2));
 
    // Return the position of the
    // most significant set bit
    return k;
  }
 
  // Function to check if all array
  // elements are exceeding k or not
  static bool check(int[] arr, int k, int n)
  {
 
    // Traverse the array arr
    for (int i = 0; i < n; i++)
    {
 
      // If current element
      // does not exceed k
      if (arr[i] <= k)
        return false;
    }
 
    return true;
  }
 
  // Function to modify array by
  // right shifting digits such that
  // all array elements are exceeding k
  static void modifyArray(int[] arr, int k, int n)
  {
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
      // If current element
      // exceeds k
      if (arr[i] > k)
        continue;
      else
      {
 
        // Count bits in the binary
        // representation of arr[i]
 
        // MSB of arr[i]
        int bits = setBitNumber(arr[i]);
        int el = arr[i];
 
        // Perform right shift operations
        for (int j = 0; j < bits; j++)
        {
 
          // If the current element is odd
          if ((el & 1) != 0)
          {
 
            // Right shift the element
            el >>= 1;
 
            // Perform OR with the bitmask
            el |= (1 << bits);
          }
 
          // If the current element is even
          else
          {
 
            // Right shift the element
            el >>= 1;
          }
 
          // If current element exceeds k
          if (el > k)
          {
            arr[i] = el;
            break;
          }
        }
      }
    }
 
    // Check if all array elements
    // are greater than k or not
    if (check(arr, k, n)) {
      for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
    }
    else
      Console.Write("-1");
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 21, 22, 23, 19 };
 
    // Size of array
    int n = arr.Length;
    int k = 24;
    modifyArray(arr, k, n);
  }
}
 
// This code is contributed by chitranayal.


Javascript


输出:
[26, 26, 27, 25]

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

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