Maximum number of times Array can be reduced in half when its all elements are even
Last Updated :
23 Aug, 2021
Given an array arr, the task is to perform operations on the array when all elements in the array are even. In one operation, replace each integer X in the array by X/2. Find maximum possible number of operations that you can perform.
Examples:
Input: arr[] = {8, 12, 40}
Output: 2
Explanation: Initially, {8, 12, 40} are present in array. Since all those integers are even,
you can perform the operation. After the operation is performed once, array becomes
{4, 6, 20}. Since all those integers are again even, we can perform the operation again.
After the operation is performed twice, array becomes {2, 3, 10}. Now, there is an odd
number “3” in the array, so no operation can be performed anymore.
Thus, you can perform the operation at most twice.
Input: arr[] = {5, 6, 8, 10}
Output: 0
Explanation: Since there is an odd number 5 in the initial array, we cant perform the
operation even once.
Approach: Given problem can be solved by making some simple observations:
- If all integers in the array at present are even, then we divide all the numbers by 2.
- Thus, the problem reduces to finding the number of times an element arr[i] can be divided by 2. Let it be times[i] . The answer is the minimum value of times[i] for all i.
- Instead of using an additional array times[], we can update the answer at each stage by simply keeping a variable, this reduces the space complexity to O(1) as we are not using any additional space.
Below is the implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int arrayDivisionByTwo( int arr[], int n)
{
int cnt = 0;
int ans = INT_MAX;
for ( int i = 0; i < n; i++) {
cnt = 0;
while (arr[i] % 2 == 0) {
arr[i] = arr[i] / 2;
cnt++;
}
ans = min(ans, cnt);
}
return ans;
}
int main()
{
int arr[] = { 8, 12, 40 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << arrayDivisionByTwo(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int arrayDivisionByTwo( int arr[], int n)
{
int cnt = 0 ;
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++) {
cnt = 0 ;
while (arr[i] % 2 == 0 ) {
arr[i] = arr[i] / 2 ;
cnt++;
}
ans = Math.min(ans, cnt);
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 8 , 12 , 40 };
int n = arr.length;
System.out.print(arrayDivisionByTwo(arr, n));
}
}
|
Python3
import sys
def arrayDivisionByTwo(arr, n):
cnt = 0
ans = sys.maxsize
for i in range (n):
cnt = 0
while (arr[i] % 2 = = 0 ):
arr[i] = arr[i] / / 2
cnt + = 1
ans = min (ans, cnt)
return ans
if __name__ = = '__main__' :
arr = [ 8 , 12 , 40 ]
n = len (arr)
print (arrayDivisionByTwo(arr, n))
|
C#
using System;
class GFG{
static int arrayDivisionByTwo( int []arr, int n)
{
int cnt = 0;
int ans = Int32.MaxValue;
for ( int i = 0; i < n; i++) {
cnt = 0;
while (arr[i] % 2 == 0) {
arr[i] = arr[i] / 2;
cnt++;
}
ans = Math.Min(ans, cnt);
}
return ans;
}
public static void Main(String[] args)
{
int []arr = { 8, 12, 40 };
int n = arr.Length;
Console.Write(arrayDivisionByTwo(arr, n));
}
}
|
Javascript
<script>
function arrayDivisionByTwo(arr, n) {
let cnt = 0;
let ans = Number.MAX_VALUE;
for (let i = 0; i < n; i++) {
cnt = 0;
while (arr[i] % 2 == 0) {
arr[i] = Math.floor(arr[i] / 2);
cnt++;
}
ans = Math.min(ans, cnt);
}
return ans;
}
let arr = [8, 12, 40];
let n = arr.length;
document.write(arrayDivisionByTwo(arr, n));
</script>
|
Time Complexity: O(32 * n)
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...