Minimize division by 2 to make Array of alternate odd and even elements
Given an array arr[] of N positive integers. The task is to find the minimum number of operations required to make the array containing alternating odd and even numbers. In one operation, any array element can be replaced by its half (i.e arr[i]/2).
Examples:
Input: N=6, arr = [4, 10, 6, 6, 2, 7]
Output: 2
Explanation: We can divide elements at index 1 and 3
by 2 to get array [4, 5, 6, 3, 2, 7], which is off alternate parity.Input: N=6, arr = [3, 10, 7, 18, 9, 66]
Output: 0
Explanation: No operations are needed as array is already alternative.
Approach: To solve the problem use the following idea:
Try both possibilities of the array start with an odd number as well as even number and print the minimum of the two possibilities as the answer.
Follow the steps to solve the problem:
- Declare and Initialize two variables result1 and result2 with 0.
- Iterate the array for all indices from 0 till N – 1.
- If the element at the even index is odd then
- Divide the element by 2 and increment the result1 until it becomes even.
- If the element at the odd index is even then
- Divide the element by 2 and increment the result1 until it becomes odd.
- If the element at the even index is odd then
- Iterate the array for all indices from 0 till N – 1.
- If the element at the even index is even then
- Divide the element by 2 and increment the result2 until it becomes odd.
- If the element at the odd index is odd then
- Divide the element by 2 and increment the result2 until it becomes even.
- If the element at the even index is even then
- print minimum of result1 and result2.
Below is the implementation for the above approach:
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to find the minimum number of operations int minOperations( int arr[], int n) { // Two variables to count number of operations int result1 = 0, result2 = 0; // For array starting with even element for ( int i = 0; i < n; i++) { int element = arr[i]; // For even indices if (i % 2 == 0) { // If element is already even if (element % 2 == 0) continue ; // Otherwise keep dividing by 2 // till element becomes even else { while (element % 2 == 1) { element /= 2; result1++; } } } // For odd indices else { // If element is already odd if (element % 2 == 1) continue ; // Otherwise keep dividing by 2 // till element becomes odd else { while (element % 2 == 0) { element /= 2; result1++; } } } } // For array starting from odd element for ( int i = 0; i < n; i++) { int element = arr[i]; // For even indices if (i % 2 == 0) { // If element is already odd if (element % 2 == 1) continue ; // Otherwise keep dividing by 2 // till element becomes odd else { while (element % 2 == 0) { element /= 2; result2++; } } } // For odd indices else { // If element is already even if (element % 2 == 0) continue ; // Otherwise keep dividing by 2 // till element becomes even else { while (element % 2 == 1) { element /= 2; result2++; } } } } return min(result1, result2); } // Driver code int main() { int N = 6; int arr[] = { 4, 10, 6, 6, 2, 3 }; // Function call cout << minOperations(arr, N); return 0; } |
Java
// Java code to implement the approach import java.io.*; class GFG { // Function to find the minimum number of operations public static int minOperations( int arr[], int n) { // Two variables to count number of operations int result1 = 0 , result2 = 0 ; // For array starting with even element for ( int i = 0 ; i < n; i++) { int element = arr[i]; // For even indices if (i % 2 == 0 ) { // If element is already even if (element % 2 == 0 ) continue ; // Otherwise keep dividing by 2 // till element becomes even else { while (element % 2 == 1 ) { element /= 2 ; result1++; } } } // For odd indices else { // If element is already odd if (element % 2 == 1 ) continue ; // Otherwise keep dividing by 2 // till element becomes odd else { while (element % 2 == 0 ) { element /= 2 ; result1++; } } } } // For array starting from odd element for ( int i = 0 ; i < n; i++) { int element = arr[i]; // For even indices if (i % 2 == 0 ) { // If element is already odd if (element % 2 == 1 ) continue ; // Otherwise keep dividing by 2 // till element becomes odd else { while (element % 2 == 0 ) { element /= 2 ; result2++; } } } // For odd indices else { // If element is already even if (element % 2 == 0 ) continue ; // Otherwise keep dividing by 2 // till element becomes even else { while (element % 2 == 1 ) { element /= 2 ; result2++; } } } } return Math.min(result1, result2); } // Driver Code public static void main(String[] args) { int N = 6 ; int arr[] = { 4 , 10 , 6 , 6 , 2 , 3 }; // Function call System.out.print(minOperations(arr, N)); } } // This code is contributed by Rohit Pradhan |
Python3
# python3 code to implement the approach # Function to find the minimum number of operations def minOperations(arr, n): # Two variables to count number of operations result1 = 0 result2 = 0 # For array starting with even element for i in range ( 0 , n): element = arr[i] # For even indices if (i % 2 = = 0 ): # If element is already even if (element % 2 = = 0 ): continue # Otherwise keep dividing by 2 # till element becomes even else : while (element % 2 = = 1 ): element / / = 2 result1 + = 1 # For odd indices else : # If element is already odd if (element % 2 = = 1 ): continue # Otherwise keep dividing by 2 # till element becomes odd else : while (element % 2 = = 0 ): element / / = 2 result1 + = 1 # For array starting from odd element for i in range ( 0 , n): element = arr[i] # For even indices if (i % 2 = = 0 ): # If element is already odd if (element % 2 = = 1 ): continue # Otherwise keep dividing by 2 # till element becomes odd else : while (element % 2 = = 0 ): element / / = 2 result2 + = 1 # For odd indices else : # If element is already even if (element % 2 = = 0 ): continue # Otherwise keep dividing by 2 # till element becomes even else : while (element % 2 = = 1 ): element / / = 2 result2 + = 1 return min (result1, result2) # Driver code if __name__ = = "__main__" : N = 6 arr = [ 4 , 10 , 6 , 6 , 2 , 3 ] # Function call print (minOperations(arr, N)) # This code is contributed by rakeshsahni |
C#
// C# code to implement the approach using System; class GFG { // Function to find the minimum number of operations public static int minOperations( int [] arr, int n) { // Two variables to count number of operations int result1 = 0, result2 = 0; // For array starting with even element for ( int i = 0; i < n; i++) { int element = arr[i]; // For even indices if (i % 2 == 0) { // If element is already even if (element % 2 == 0) continue ; // Otherwise keep dividing by 2 // till element becomes even else { while (element % 2 == 1) { element /= 2; result1++; } } } // For odd indices else { // If element is already odd if (element % 2 == 1) continue ; // Otherwise keep dividing by 2 // till element becomes odd else { while (element % 2 == 0) { element /= 2; result1++; } } } } // For array starting from odd element for ( int i = 0; i < n; i++) { int element = arr[i]; // For even indices if (i % 2 == 0) { // If element is already odd if (element % 2 == 1) continue ; // Otherwise keep dividing by 2 // till element becomes odd else { while (element % 2 == 0) { element /= 2; result2++; } } } // For odd indices else { // If element is already even if (element % 2 == 0) continue ; // Otherwise keep dividing by 2 // till element becomes even else { while (element % 2 == 1) { element /= 2; result2++; } } } } return Math.Min(result1, result2); } // Driver code public static void Main( string [] args) { int N = 6; int [] arr = { 4, 10, 6, 6, 2, 3 }; // Function call Console.Write(minOperations(arr, N)); } } // This code is contributed by code_hunt. |
Javascript
<script> // JS code to implement the approach // Function to find the minimum number of operations function minOperations(arr, n) { // Two variables to count number of operations let result1 = 0, result2 = 0; // For array starting with even element for (let i = 0; i < n; i++) { let element = arr[i]; // For even indices if (i % 2 == 0) { // If element is already even if (element % 2 == 0) continue ; // Otherwise keep dividing by 2 // till element becomes even else { while (element % 2 == 1) { element = Math.floor(element / 2); result1++; } } } // For odd indices else { // If element is already odd if (element % 2 == 1) continue ; // Otherwise keep dividing by 2 // till element becomes odd else { while (element % 2 == 0) { element = Math.floor(element / 2); result1++; } } } } // For array starting from odd element for (let i = 0; i < n; i++) { let element = arr[i]; // For even indices if (i % 2 == 0) { // If element is already odd if (element % 2 == 1) continue ; // Otherwise keep dividing by 2 // till element becomes odd else { while (element % 2 == 0) { element = Math.floor(element / 2); result2++; } } } // For odd indices else { // If element is already even if (element % 2 == 0) continue ; // Otherwise keep dividing by 2 // till element becomes even else { while (element % 2 == 1) { element = Math.floor(element / 2); result2++; } } } } return Math.min(result1, result2); } // Driver code let N = 6; let arr = [4, 10, 6, 6, 2, 3]; // Function call document.write(minOperations(arr, N)); </script> |
PHP
<?php // Function to find the minimum number of operations function minOperations( $arr , $n ) { // Two variables to count number of operations $result1 = 0; $result2 = 0; // For array starting with even element for ( $i = 0; $i < $n ; $i ++) { $element = $arr [ $i ]; // For even indices if ( $i % 2 == 0) { // If element is already even if ( $element % 2 == 0) continue ; // Otherwise keep dividing by 2 // till element becomes even else { while ( $element % 2 == 1) { $element /= 2; $result1 ++; } } } // For odd indices else { // If element is already odd if ( $element % 2 == 1) continue ; // Otherwise keep dividing by 2 // till element becomes odd else { while ( $element % 2 == 0) { $element /= 2; $result1 ++; } } } } // For array starting from odd element for ( $i = 0; $i < $n ; $i ++) { $element = $arr [ $i ]; // For even indices if ( $i % 2 == 0) { // If element is already odd if ( $element % 2 == 1) continue ; // Otherwise keep dividing by 2 // till element becomes odd else { while ( $element % 2 == 0) { $element /= 2; $result2 ++; } } } // For odd indices else { // If element is already even if ( $element % 2 == 0) continue ; // Otherwise keep dividing by 2 // till element becomes even else { while ( $element % 2 == 1) { $element /= 2; $result2 ++; } } } } return min( $result1 , $result2 ); } // Driver code $N = 6; $arr = array (4, 10, 6, 6, 2, 3); // Function call echo minOperations( $arr , $N ); // This code is contributed by Kanishka Gupta ?> |
2
Time Complexity: O(N * log(max(arr[i]))), where max(arr[i]) is maximum element in array.
Auxiliary Space: O(1)
Using Brute Force In Python:
Approach:
- We initialize a variable min_ops to inf (infinity) and n to the length of the array.
- We iterate through all pairs of indices (i, j) in the array and create a copy of the original array arr as temp_arr. We also initialize a variable count to keep track of the number of operations required.
- We check if the elements at indices i and j are even. If they are, we divide them by 2 and increment the count variable.
- We check if the resulting temp_arr has alternate parity (i.e., if the parity of the element at index k is the same as k%2 for all k in the range 0 to n-1). If it does, we update the min_ops variable to the minimum of its current value and the count variable.
- We return the min_ops variable if it has been updated during the iteration, else we return 0 indicating that no operations were required.
Python3
def alternate_array(arr): n = len (arr) min_ops = float ( 'inf' ) for i in range (n): for j in range (i + 1 , n): temp_arr = arr[:] count = 0 if temp_arr[i] % 2 = = 0 : temp_arr[i] / / = 2 count + = 1 if temp_arr[j] % 2 = = 0 : temp_arr[j] / / = 2 count + = 1 if all (temp_arr[k] % 2 = = k % 2 for k in range (n)): min_ops = min (min_ops, count) return min_ops if min_ops ! = float ( 'inf' ) else 0 # Example usage arr1 = [ 4 , 10 , 6 , 6 , 2 , 7 ] n1 = len (arr1) print (alternate_array(arr1)) # Output: 2 arr2 = [ 3 , 10 , 7 , 18 , 9 , 66 ] n2 = len (arr2) print (alternate_array(arr2)) # Output: 0 |
2 0
time complexity: O(N^3)
space complexity: O(N)
Please Login to comment...