Minimize steps to make Array elements equal by deleting adjacent pair and inserting bitwise OR
Last Updated :
15 Jun, 2022
Given an array arr[], consisting of N integers, the task is to find the minimum number of moves required to make all numbers in the resulting array equal. In one move,
- Take two adjacent numbers arr[i] and arr[i+1], and delete them.
- Insert the bitwise OR of arr[i] and arr[i+1] at the deleted position.
Examples:
Input: arr[] = {1, 1, 1, 0, 1}
Output: 1
Explanation: After OR operation on arr[2] and arr[3] we get, 1 | 0 = 1.
Since, array arr[] becomes {1, 1, 1, 1} and here all elements are equal.
Thus, it takes only 1 move to make all the numbers equal.
Input: arr[] = {1, 2, 3}
Output: 1
Explanation: After OR operation on arr[0] and arr[1] we get, 1 | 2 = 3.
Now, array arr[] becomes {3, 3} and here both elements are equal.
Thus, it takes only 1 move to make all the numbers equal.
Approach: The problem can be solved based on the following observation of bitwise properties:
It is known that x | x = x, [where ‘|’ means bitwise OR].
Lets say that the array has all the elements as X at the end and total M elements.
Based on this we can assume that there were M subarrays such that bitwise OR of the subarray elements are X.
The closer the value of M to N, the less operations are done.
So the task reduces to find the maximum number of subarray such that they have bitwise OR = X
If there are M such subarrays, then the number of operations performed = N – M (because in each step size reduces by 1)
Follow the below steps to solve the problem:
- Find bitwise OR of all array elements and store it (say Total_Or).
- Initialize a variable (say Group = 0) to count how many subarrays have the OR value of Total_Or.
- Run a loop in the array from i = 0 to N:
- Calculate the bitwise OR of the elements (say Current_Or).
- If Current_Or is the same as Total_Or, Increment Group by 1 and set Current_Or = 0;
- After executing the loop, return N – Group.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinMoves( int * arr, int n)
{
int Total_Or = 0;
int Current_Or = 0;
int Group = 0;
for ( int i = 0; i < n; i++) {
Total_Or |= arr[i];
}
for ( int i = 0; i < n; i++) {
Current_Or |= arr[i];
if (Current_Or == Total_Or) {
Group++;
Current_Or = 0;
}
}
return n - Group;
}
int main()
{
int arr[] = { 1, 1, 1, 0, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findMinMoves(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int findMinMoves( int arr[], int n)
{
int Total_Or = 0 ;
int Current_Or = 0 ;
int Group = 0 ;
for ( int i = 0 ; i < n; i++) {
Total_Or |= arr[i];
}
for ( int i = 0 ; i < n; i++) {
Current_Or |= arr[i];
if (Current_Or == Total_Or) {
Group++;
Current_Or = 0 ;
}
}
return n - Group;
}
public static void main(String args[])
{
int arr[] = { 1 , 1 , 1 , 0 , 1 };
int N = arr.length;
System.out.println(findMinMoves(arr, N));
}
}
|
Python3
def findMinMoves(arr,n):
Total_Or = 0 ;
Current_Or = 0 ;
Group = 0 ;
for i in range ( 0 ,n):
Total_Or = Total_Or|arr[i]
for i in range ( 0 ,n):
Current_Or | = arr[i]
if (Current_Or = = Total_Or):
Group = Group + 1
Current_Or = 0
return n - Group
N = 5
arr = [ 1 , 1 , 1 , 0 , 1 ]
print (findMinMoves(arr, N))
|
C#
using System;
public class GFG{
static int findMinMoves( int [] arr, int n)
{
int Total_Or = 0;
int Current_Or = 0;
int Group = 0;
for ( int i = 0; i < n; i++) {
Total_Or |= arr[i];
}
for ( int i = 0; i < n; i++) {
Current_Or |= arr[i];
if (Current_Or == Total_Or) {
Group++;
Current_Or = 0;
}
}
return n - Group;
}
public static void Main ()
{
int N = 5;
int [] arr = { 1, 1, 1, 0, 1 };
int ans = findMinMoves(arr, N);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function findMinMoves(arr, n) {
let Total_Or = 0;
let Current_Or = 0;
let Group = 0;
for (let i = 0; i < n; i++) {
Total_Or |= arr[i];
}
for (let i = 0; i < n; i++) {
Current_Or |= arr[i];
if (Current_Or == Total_Or) {
Group++;
Current_Or = 0;
}
}
return n - Group;
}
let arr = [1, 1, 1, 0, 1];
let N = arr.length;
document.write(findMinMoves(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...