Given an array arr[] of size N and an integer K, the task is to make all array elements > K by performing right shift operations on array elements.
Note: If it is not possible to make all array elements > K, then print -1.
Examples:
Input: arr[] = { 21, 22, 23, 19 }, K = 24
Output: { 26, 26, 27, 25 }
Explanation:Â
arr[0] = 10101. Performing 1 right shift -→ 11010 → 26
arr[1] = 10110. Performing 3 right shift → 11010 → 26
arr[2] = 10111. Performing 1 right shift → 11011 → 27
arr[3] = 10011. Performing 2 right shift → 11001 → 25Input: arr[] = { 45, 37, 54, 46, 62 }, K = 48
Output: { 54, 50, 54, 53, 62 }
Approach: Follow the steps below to solve the problem :
- Traverse the array
- Perform right shift operation on digits of each array element arr[i].
- If arr[i] > k, update arr[i] and continue.
- If any array element arr[i] ≤ K, then print -1
- Otherwise, print the array arr[].
Below is the implementation of the above approach:
C++
// C++ program to implement// the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to find// MSB of a numberint 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 notbool 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 kvoid 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 Codeint 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 approachpublic 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 approachimport mathÂ
# Function to find# MSB of a numberdef 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 notdef 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 kdef 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 approachusing 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
<script>// Javascript program to implement// the above approachÂ
// Function to find// MSB of a numberfunction setBitNumber(n){Â
    // Stores the position of the    // most significant set bit    let k = Math.log2(n);Â
    // Return the position of the    // most significant set bit    return k;}Â
// Function to check if all array// elements are exceeding k or notfunction check(arr, k, n){Â
    // Traverse the array arr    for (let 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 kfunction modifyArray(arr, k, n){Â
    // Traverse the array    for (let 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]            let bits = setBitNumber(arr[i]);            let el = arr[i];Â
            // Perform right shift operations            for (let 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 (let i = 0; i < n; i++)            document.write(arr[i] + " ");    }    else        document.write(-1);}Â
// Driver Codelet arr = [ 21, 22, 23, 19 ];Â
// Size of arraylet n = arr.length;let k = 24;modifyArray(arr, k, n);Â
// This code is contributed by subhammahato348.</script> |
[26, 26, 27, 25]
Â
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
