Minimize count of given operations required to be performed to make all array elements equal to 1
Last Updated :
22 Apr, 2021
Given an array, arr[] consisting of N positive integers, the task is to make all array elements equal to 1 by performing the following operations minimum number of times:
- Increment the value of all elements of a subarray by any positive integers.
- If all the array elements are even then divide all the array elements by 2.
Print the count of minimum operations required.
Examples:
Input: arr[] = { 3, 1, 2, 4 }
Output: 3
Explanation:
Incrementing arr[0] by 1, arr[1] by 3 and arr[2] by 2 modifies arr[] to { 4, 4, 4, 4 }.
Dividing all the array elements by 2 modifies arr[] to { 2, 2, 2, 2 }.
Dividing all the array elements by 2 modifies arr[] to { 1, 1, 1, 1 }.
Therefore, the required output is 3.
Input: arr[] = { 2, 4 }
Output: 3
Explanation:
Dividing all the array elements by 2 modifies arr[] to { 1, 2 }.
Incrementing the value of arr[0] by 1 modifies arr[] to { 2, 2 }.
Dividing all the array elements by 2 modifies arr[] to { 1, 2 }.
Therefore, the required output is 3.
Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:
- Find the largest element of the array say, Max.
- If all the array elements are equal and also in the form of power of 2, then print log2(Max).
- Otherwise, make all the array elements equal to the closest power of 2 which is greater than or equal to Max and print log2(Max) + 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool CheckAllEqual( int arr[], int N)
{
for ( int i = 1; i < N; i++) {
if (arr[0] != arr[i]) {
return false ;
}
}
return true ;
}
int minCntOperations( int arr[], int N)
{
int Max = *max_element(arr, arr + N);
bool isPower2 = !(Max && (Max & (Max - 1)));
if (isPower2 && CheckAllEqual(arr, N)) {
return log2(Max);
}
else {
return ceil (log2(Max)) + 1;
}
}
int main()
{
int arr[] = { 2, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minCntOperations(arr, N);
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static boolean CheckAllEqual( int arr[], int N)
{
for ( int i = 1 ; i < N; i++)
{
if (arr[ 0 ] != arr[i])
{
return false ;
}
}
return true ;
}
static int minCntOperations( int arr[], int N)
{
int Max = Arrays.stream(arr).max().getAsInt();
boolean isPower2;
if (( int )(Math.ceil((Math.log(N) / Math.log(N))))
== ( int )(Math.floor(((Math.log(N) / Math.log( 2 ))))))
{
isPower2 = true ;
}
else
{
isPower2 = false ;
}
if (isPower2 && CheckAllEqual(arr, N))
{
return ( int )(Math.log(Max) / Math.log( 2 ));
}
else
{
return ( int )Math.ceil(Math.log(Max)
/ Math.log( 2 )) + 1 ;
}
}
public static void main(String[] args)
{
int [] arr = new int [] { 2 , 4 };
int N = arr.length;
System.out.println(minCntOperations(arr, N));
}
}
|
Python3
import math
def CheckAllEqual(arr, N):
for i in range ( 1 , N):
if (arr[ 0 ] ! = arr[i]):
return False
return True
def minCntOperations(arr, N):
Max = max (arr)
isPower2 = not ( Max and ( Max & ( Max - 1 )))
if (isPower2 and CheckAllEqual(arr, N)):
return log2( Max )
else :
return math.ceil(math.log2( Max )) + 1
if __name__ = = "__main__" :
arr = [ 2 , 4 ]
N = len (arr)
print (minCntOperations(arr, N))
|
C#
using System;
using System.Linq;
class GFG {
static bool CheckAllEqual( int [] arr, int N)
{
for ( int i = 1; i < N; i++)
{
if (arr[0] != arr[i])
{
return false ;
}
}
return true ;
}
static int minCntOperations( int [] arr, int N)
{
int Max = arr.Max();
bool isPower2;
if (( int )(Math.Ceiling((Math.Log(N) / Math.Log(N))))
== ( int )(Math.Floor(((Math.Log(N) / Math.Log(2))))))
{
isPower2 = true ;
}
else
{
isPower2 = false ;
}
if (isPower2 && CheckAllEqual(arr, N))
{
return ( int )(Math.Log(Max) / Math.Log(2));
}
else
{
return ( int )Math.Ceiling(Math.Log(Max)
/ Math.Log(2)) + 1;
}
}
static public void Main()
{
int [] arr = new int [] { 2, 4 };
int N = arr.Length;
Console.WriteLine(minCntOperations(arr, N));
}
}
|
Javascript
<script>
function CheckAllEqual(arr, N)
{
for (i = 1; i < N; i++)
{
if (arr[0] != arr[i])
{
return false ;
}
}
return true ;
}
function minCntOperations(arr, N)
{
var Max =Math.max.apply(Math,arr);
var isPower2;
if (parseInt( (Math.ceil((Math.log(N) / Math.log(N))))) == parseInt( (Math.floor(((Math.log(N) / Math.log(2))))))) {
isPower2 = true ;
} else {
isPower2 = false ;
}
if (isPower2 && CheckAllEqual(arr, N))
{
return parseInt( (Math.log(Max) / Math.log(2)));
}
else
{
return parseInt( Math.ceil(Math.log(Max) / Math.log(2))) + 1;
}
}
var arr = [ 2, 4 ];
var N = arr.length;
document.write(minCntOperations(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...