Rearrange given Array such that all set bit positions have higher value than others
Last Updated :
09 Jun, 2022
Given an array B1[] and a binary array B2[] each of size N, the task is to rearrange array B1[] in a way such that for all setbit position i of B2[] the value of B1[i] will be greater than the values where bit is not set in B2[] i.e for all (i, j) B1[i] > B1[j] when B2[i] = 1, B2[j] = 0 (0 ≤ i, j < N).
Note: If multiple arrangements are possible, print any one of them.
Examples:
Input: N = 7, B1[] = {1, 2, 3, 4, 5, 6, 7}, B2[] = {0, 1, 0, 1, 0, 1, 0}
Output: 1 5 2 6 3 7 4
Explanation: Values having 1 in B2[] array are = {2, 4, 6} and values with 0s are = {1, 3, 5, 7}.
So, in correspondent to B2[] array, B1[] can be rearranged = {1, 5, 2, 6, 3, 7, 4}
B1[] = {1, 5, 2, 6, 3, 7, 4}
B2[] = {0, 1, 0, 1, 0, 1, 0}, now all places in B1[i] > B1[j] where B2[i] = 1, and B2[j] = 0.
It also can be rearranged as {1, 7, 2, 6, 3, 5, 4}
Input: N = 3, B1[] = {4, 3, 5}, B2[] = {1, 1, 1}
Output: 5 4 3
Approach: The problem can be solved based on the following idea:
To arrange B1[] as per set bit positions of B2[], sort the values of B1[] and arrange the higher values in positions with bits set and the lower values in positions in other positions.
Follow the steps mentioned below to implement the above idea:
- Make a list of pairs (say v) with the bit value of B2[] and the corresponding value at B1[],
- Sort the list of pairs in ascending order of bits value of B2[].
- Rearrange B1[] using two pointer approach.
- Declare left pointer with 0 and right pointer with N-1.
- When B2[i] is 0 (0 ≤ i < N), set B1[] as v[left] and increment left.
- Otherwise, set B1[i] as v[right] and decrement right.
- Return B1[] as the final answer.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int n, vector< int >& b1, int b2[])
{
vector<pair< int , int > > v;
int cnt = 1;
for ( int i = 0; i < n; i++) {
v.push_back({ b2[i], b1[i] });
}
sort(v.begin(), v.end());
int left = 0, right = n - 1;
for ( int i = 0; i < n; i++) {
if (b2[i] == 0)
b1[i] = v[left++].second;
else
b1[i] = v[right--].second;
}
}
int main()
{
int N = 3;
vector< int > B1 = { 3, 4, 5 };
int B2[] = { 1, 1, 1 };
solve(N, B1, B2);
for ( int x : B1)
cout << x << " " ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Pair {
int x;
int y;
public Pair( int x, int y)
{
this .x = x;
this .y = y;
}
}
class Compare {
static void compare(Pair arr[], int n)
{
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
return p1.x - p2.x;
}
});
}
}
class GFG {
static void solve( int n, int [] b1, int [] b2)
{
Pair v[] = new Pair[n];
int cnt = 1 ;
for ( int i = 0 ; i < n; i++) {
v[i] = new Pair(b2[i], b1[i]);
}
Compare obj = new Compare();
obj.compare(v, n);
int left = 0 , right = n - 1 ;
for ( int i = 0 ; i < n; i++) {
if (b2[i] == 0 )
b1[i] = v[left++].y;
else
b1[i] = v[right--].y;
}
}
public static void main (String[] args) {
int N = 3 ;
int B1[] = { 3 , 4 , 5 };
int B2[] = { 1 , 1 , 1 };
solve(N, B1, B2);
for ( int i = 0 ; i <B1.length; i++)
System.out.print(B1[i] + " " );
}
}
|
Python3
def solve(n, b1, b2):
v = []
cnt = 1
for i in range (n):
v.append([b2[i], b1[i]])
v.sort()
left = 0
right = n - 1
for i in range (n):
if b2[i] = = 0 :
b1[i] = v[left][ 1 ]
left + = 1
else :
b1[i] = v[right][ 1 ]
right - = 1
N = 3
b1 = [ 3 , 4 , 5 ]
b2 = [ 1 , 1 , 1 ]
solve(N, b1, b2)
for x in b1:
print (x, end = " " )
|
C#
using System;
using System.Collections.Generic;
public class GFG{
class pair : IComparable<pair>
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
public int CompareTo(pair b)
{
if ( this .first != b.first)
return ( this .first < b.first)?-1:1;
else
return this .second > b.second?-1:1;
}
}
static void solve( int n, int [] b1, int [] b2)
{
List<pair > v = new List<pair > ();
for ( int i = 0; i < n; i++) {
v.Add( new pair(b2[i],
b1[i]));
}
v.Sort();
int left = 0, right = n - 1;
for ( int i = 0; i < n; i++) {
if (b2[i] == 0)
b1[i] = v[left++].second;
else
b1[i] = v[right--].second;
}
}
public static void Main (){
int N = 3;
int [] B1 = { 3, 4, 5 };
int [] B2 = { 1, 1, 1 };
solve(N, B1, B2);
for ( int i = B1.Length-1; i >=0 ; i--)
{
Console.Write(B1[i]);
Console.Write( " " );
}
}
}
|
Javascript
<script>
const solve = (n, b1, b2) => {
let v = [];
let cnt = 1;
for (let i = 0; i < n; i++) {
v.push([b2[i], b1[i]]);
}
v.sort();
let left = 0, right = n - 1;
for (let i = 0; i < n; i++) {
if (b2[i] == 0)
b1[i] = v[left++][1];
else
b1[i] = v[right--][1];
}
}
let N = 3;
let B1 = [3, 4, 5];
let B2 = [1, 1, 1];
solve(N, B1, B2);
for (let x in B1)
document.write(`${B1[x]} `);
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...