Construct lexicographically smallest Binary array of size N with A 0s and X inversion count
Given three numbers N, A, and X, the task is to construct the lexicographically smallest binary array of size N, containing A 0s and having an inversion count of X.
Examples:
Input: N=5, A=2, X=1
Output: 0 1 0 1 1
Explanation:
The number of inversions in this array is 1(2nd and 3rd index).
Input: N=5, A=2, X=3
Output: 0 1 1 1 0
Approach: The given problem can be solved using two pointer technique based on the following observations:
- The array with A 0s having 0 inversion is the array with all 0s to the beginning and then the all the 1s.
- If an element 0 at index i and an element 1 at index j is swapped, then inversion count increases by count of 1s in the range [i, j].
- The maximum possible inversion count is A*(N-A).
Follow the steps below to solve the problem:
- If X is greater than A*(N-A), print -1 and then return.
- Initialize an array say arr[] of size N and fill the first A Indices with 0s and the remaining with 1s.
- Initialize two variables curr as A-1 and prev as N-1 to iterate over the array.
- Iterate until X is greater than 0 and curr, is not less than 0, and perform the following steps:
- If X is greater than or equal prev-cur, then do the following:
- Swap the two elements at arr[prev], and arr[curr].
- Subtract prev-cur from X.
- Decrement prev and curr by 1.
- Otherwise, do the following:
- Swap the two elements arr[curr] and arr[cur+1].
- Increment curr by 1 and decrement X by 1.
- Print the array arr.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void binaryArrayInversions( int N, int A, int X)
{
if (A * (N - A) < X) {
cout << "-1" ;
return ;
}
int Arr[N] = { 0 };
fill(Arr + A, Arr + N, 1);
int cur = A - 1;
int prev = N - 1;
while (X && cur >= 0) {
if (X >= prev - cur) {
swap(Arr[prev], Arr[cur]);
X -= prev - cur;
prev--;
cur--;
}
else {
swap(Arr[cur], Arr[cur + 1]);
cur++;
X--;
}
}
for ( auto u : Arr)
cout << u << " " ;
}
int main()
{
int N = 5;
int A = 2;
int X = 1;
binaryArrayInversions(N, A, X);
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
static void binaryArrayInversions( int N, int A, int X)
{
if (A * (N - A) < X)
{
System.out.println( "-1" );
return ;
}
int []Arr = new int [N];
Arrays.fill(Arr, 0 );
for ( int i = A; i < N; i++)
Arr[i] = 1 ;
int cur = A - 1 ;
int prev = N - 1 ;
while (X != 0 && cur >= 0 )
{
if (X >= prev - cur)
{
int temp = Arr[prev];
Arr[prev] = Arr[cur];
Arr[cur] = temp;
X -= prev - cur;
prev--;
cur--;
}
else
{
int temp = Arr[cur];
Arr[cur] = Arr[cur + 1 ];
Arr[cur + 1 ] = temp;
cur++;
X--;
}
}
for ( int i = 0 ; i < Arr.length; i++)
System.out.print(Arr[i] + " " );
}
public static void main(String args[])
{
int N = 5 ;
int A = 2 ;
int X = 1 ;
binaryArrayInversions(N, A, X);
}
}
|
Python3
def binaryArrayInversions(N, A, X):
if (A * (N - A) < X):
print ( "-1" )
return
Arr = [ 0 ] * N
for i in range (A,N):
Arr[i] = 1
cur = A - 1
prev = N - 1
while (X and cur > = 0 ):
if (X > = prev - cur):
Arr[prev], Arr[cur] = Arr[cur],Arr[prev]
X - = prev - cur
prev - = 1
cur - = 1
else :
Arr[cur], Arr[cur + 1 ] = Arr[cur + 1 ], Arr[cur]
cur + = 1
X - = 1
for u in Arr:
print (u, end = " " )
if __name__ = = '__main__' :
N = 5
A = 2
X = 1
binaryArrayInversions(N, A, X)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void binaryArrayInversions( int N, int A, int X)
{
if (A * (N - A) < X) {
Console.Write( "-1" );
return ;
}
int []Arr = new int [N];
Array.Clear(Arr, 0, N);
for ( int i=A;i<N;i++)
Arr[i] = 1;
int cur = A - 1;
int prev = N - 1;
while (X!=0 && cur >= 0)
{
if (X >= prev - cur)
{
int temp = Arr[prev];
Arr[prev] = Arr[cur];
Arr[cur] = temp;
X -= prev - cur;
prev--;
cur--;
}
else {
int temp = Arr[cur];
Arr[cur] = Arr[cur + 1];
Arr[cur + 1] = temp;
cur++;
X--;
}
}
for ( int i = 0; i < Arr.Length; i++)
Console.Write(Arr[i] + " " );
}
public static void Main()
{
int N = 5;
int A = 2;
int X = 1;
binaryArrayInversions(N, A, X);
}
}
|
Javascript
<script>
function binaryArrayInversions(N, A, X) {
if (A * (N - A) < X) {
document.write( "-1" );
return ;
}
let Arr = new Array(N).fill(0);
Arr.forEach((item, i) => {
if (i >= Arr.length - (N - A)) {
Arr[i] = 1
}
})
let cur = A - 1;
let prev = N - 1;
while (X && cur >= 0) {
if (X >= prev - cur) {
let temp = Arr[prev];
Arr[prev] = Arr[cur];
Arr[cur] = temp;
X -= prev - cur;
prev--;
cur--;
}
else {
let temp = Arr[cur + 1];
Arr[cur + 1] = Arr[cur];
Arr[cur] = temp;
cur++;
X--;
}
}
document.write(Arr);
}
let N = 5;
let A = 2;
let X = 1;
binaryArrayInversions(N, A, X);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
09 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...