VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-operations-for-array-divisibility-by-power-of-2/

⇱ Find operations for Array Divisibility by Power of 2 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find operations for Array Divisibility by Power of 2

Last Updated : 26 Apr, 2023

Given an array of size n, the task is to find the minimum number of operations required in the array such that the product of all array elements is divisible by nth power of 2. In one operation, you can select any index i and multiply ai by i, you can perform an operation on a particular index at most 1 time. (Follow 1-based indexing)

Examples:

Input: arr[] = { 10, 6, 11 }
Output: 1
Explanation: We can apply operation at index 2.
 

Input: arr[] = {13, 17, 1, 1 }
Output: -1
Explanation: We can not make the product such that divisible by 2n

Approach: To solve the problem follow the below idea:

We will apply operation till the product of the array is not divisible by 2n and apply operation on that index first which give us maximum 2s.

Illustration:

Lets take an example :  arr[] = { 6, 1, 5, 1 }

  • The product of the array is 30 which is not divisible by 24 here n=4
  • We will apply an operation on index 4 because index 4 gives us maximum 2s than index 1, 2, 3. Then the product of the array becomes 120 which is not divisible by 24
  • Then, we will apply an operation on index 2, because index 4 is already operated and index 2 gives a maximum 2 than index 1 and 3.
  • Then, the product of the array becomes 240 which is divisible by 24.
  • In total, it requires 2 operations to make the product of the array such that it is divisible by 2n.

Below are the steps for the above approach:

  • Iterate the array to find how many 2s are present in the array initially.
  • Iterate the array from the index from 1 to n and find how many 2s each number has and store the count in a vector say v and sort the vector in descending order because we want a maximum of 2s first.
  • Initialize a boolean variable say flag = false.
  • If the product of the array is divisible by the nth power of 2. The number of 2s in the product of the array should be greater than n.
  • Iterate the vector v,
    • Check if count2 >= n, update flag = true, and break the loop.
    • Else perform the given operation such that, in minimum operation, we have the maximum number of 2s. In each iteration, add numbers of 2s present in that index stored in the vector v and increase operation by 1.
  • Check if flag == true or count2 >= n which means the product if the array is divisible by 2n, returns the number of operations performed.
  • If the product of the array is not divisible by 2n, return -1.

Below is the code for the above approach:


Output
1

Time Complexity: O(n*log2n)
Auxiliary Space: O(n)

Comment