GeeksforGeeks App
Open App
Browser
Continue

# Modify array by right shifting digits of array elements to values exceeding K

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 → 25

Input: arr[] = { 45, 37, 54, 46, 62 }, K = 48
Output: { 54, 50, 54, 53, 62 }

Approach: Follow the steps below to solve the problem :

1. Traverse the array
2. Perform right shift operation on digits of each array element arr[i].
3. If arr[i] > k, update arr[i] and continue.
4. If any array element arr[i] ≤ K, then print -1
5. Otherwise, print the array arr[].

Below is the implementation of the above approach:

## 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

 ``

Output:

`[26, 26, 27, 25]`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up