Minimize multiply by position so that Array product is divisible by 2^K
Last Updated :
09 Dec, 2022
Given an array nums [] of size N containing positive integers and a positive integer K, the task is to find the minimum number of operations such that the array product is divisible by 2K where in each operation nums[i] is multiplied by its position (i.e. i+1).
Note: Return -1 if it is not possible to make the array product divisible 2K.
Examples:
Input: N = 2, K = 2, nums[] = { 3, 2 }
Output: 1
Explanation: The product of nums is 6 which is not divisible by 22 = 4. Make nums[2] = 2 * 2 = 4, then the product of the array becomes 12 which is divisible by 22.
Input: N = 3, K = 3, nums[] = { 2, 1, 3 }
Output: -1
Explanation: The product of nums is 6 which is not divisible by 23 = 8. If we make nums[2] = 1 * 2 = 2, then also the product will become 12 which is not divisible by 23.
Approach: This can be solved using the following idea:
Count the number of times each element of array and index i is divisible by 2 and store the count of each in a variable and check whether count of two is more than K or not.
Follow the below steps to solve the problem:
- Initialize a variable (say curr = 0).
- Traverse the array nums[] and store the number of 2s in the factorization of each element in curr variable and the number of 2s in index i and store them in a vector v.
- If the curr variable is greater than or equal to K then return 0.
- Sort the vector v in increasing order.
- Initialize res = 0 to count the number of operations required.
- While curr value is less than K and v is not empty, optimally increase the curr by v.back() i.e. maximum number of two’s in indices [1, n], pop back from v and increment in res by 1.
- When the traversal is over, check if the current value is still less than K then return -1.
- Otherwise, return res as the number of operations required.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int countTwos( int n)
{
int count = 0;
while (n % 2 == 0) {
count++;
n /= 2;
}
return count;
}
int minimumOperationsReq(vector< int >& nums, int k)
{
int curr = 0;
vector< int > v;
for ( int i = 0; i < nums.size(); i++) {
curr += countTwos(nums[i]);
int cnt = countTwos(i + 1);
v.push_back(cnt);
}
if (curr >= k)
return 0;
sort(v.begin(), v.end());
int res = 0;
while (curr < k and !v.empty()) {
curr += v.back();
v.pop_back();
res++;
}
if (curr < k)
return -1;
return res;
}
int main()
{
int K = 2;
vector< int > nums = { 3, 2 };
int N = nums.size();
cout << minimumOperationsReq(nums, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countTwos( int n)
{
int count = 0 ;
while (n % 2 == 0 ) {
count++;
n /= 2 ;
}
return count;
}
static int minimumOperationsReq( int [] nums, int k)
{
int curr = 0 ;
ArrayList<Integer> v = new ArrayList<>();
for ( int i = 0 ; i < nums.length; i++) {
curr += countTwos(nums[i]);
int cnt = countTwos(i + 1 );
v.add(cnt);
}
if (curr >= k)
return 0 ;
Collections.sort(v);
int res = 0 ;
while (curr < k && !v.isEmpty()) {
curr += v.get(v.size()- 1 );
v.remove(v.size()- 1 );
res++;
}
if (curr < k)
return - 1 ;
return res;
}
public static void main(String[] args)
{
int K = 2 ;
int [] nums = { 3 , 2 };
int N = nums.length;
System.out.println(minimumOperationsReq(nums, K));
}
}
|
Python3
def countTwos(n) :
count = 0 ;
while (n % 2 = = 0 ) :
count + = 1 ;
n / / = 2 ;
return count;
def minimumOperationsReq(nums, k) :
curr = 0 ;
v = [];
for i in range ( len (nums)) :
curr + = countTwos(nums[i]);
cnt = countTwos(i + 1 );
v.append(cnt);
if (curr > = k) :
return 0 ;
v.sort();
res = 0 ;
while (curr < k and len (v) ! = 0 ) :
curr + = v[ - 1 ];
v.pop();
res + = 1 ;
if (curr < k) :
return - 1 ;
return res;
if __name__ = = "__main__" :
K = 2 ;
nums = [ 3 , 2 ];
N = len (nums);
print (minimumOperationsReq(nums, K));
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
static int countTwos( int n)
{
int count = 0;
while (n % 2 == 0) {
count++;
n /= 2;
}
return count;
}
static int minimumOperationsReq( int [] nums, int k)
{
int curr = 0;
ArrayList v = new ArrayList();
for ( int i = 0; i < nums.Length; i++) {
curr += countTwos(nums[i]);
int cnt = countTwos(i + 1);
v.Add(cnt);
}
if (curr >= k)
return 0;
v.Sort();
int res = 0;
while (curr < k && v.Count > 0) {
curr += ( int )v[v.Count - 1];
v.RemoveAt(v.Count - 1);
res++;
}
if (curr < k)
return -1;
return res;
}
public static void Main()
{
int K = 2;
int [] nums = { 3, 2 };
int N = nums.Length;
Console.WriteLine(minimumOperationsReq(nums, K));
}
}
|
Javascript
<script>
function countTwos(n) {
let count = 0;
while (n % 2 == 0) {
count++;
n /= 2;
}
return count;
}
function minimumOperationsReq(nums, k) {
let curr = 0;
let v = [];
for (let i = 0; i < nums.length; i++) {
curr += countTwos(nums[i]);
let cnt = countTwos(i + 1);
v.push(cnt);
}
if (curr >= k)
return 0;
v.sort( function (a, b) { return a - b })
let res = 0;
while (curr < k && v.length != 0) {
curr += v[v.length - 1];
v.pop();
res++;
}
if (curr < k)
return -1;
return res;
}
let K = 2;
let nums = [3, 2];
let N = nums.length;
document.write(minimumOperationsReq(nums, K));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...