Construct array having X subsequences with maximum difference smaller than d
Last Updated :
17 Aug, 2022
Given two numbers X and d where X represents the number of non empty subsequences and d represents the difference. Output an array of size N having non empty subsequences equal to X where difference between maximum element and minimum element of each subsequence should be less than d.
Constraints:
1 ? N ? 500
1 ? X ? 109
Examples:
Input : X = 10, d = 5
Output : 5 50 7 15 6 100
"10" desired subsequences are: [5], [50], [7],
[15], [6], [100], [5, 7], [5, 6], [7, 6],
[5, 7, 6].
Input : X = 4, d = 10
Output : 10 100 1000 10000
"4" desired subsequences are: [10], [100],
[1000], [10000].
Approach: It can be clearly seen that if d = 5, then array [1, 7] will have only 2 desired subsequences ([1] and [7] and not [1, 7]). So, similarly, if we add many 1s and many 7s in that array then also there will be no subsequence accepted which have 1 and 7 together. So, elements having difference more than d will always form disjoint sets. For e.g. in [1, 5, 9, 9, 12, 13], if d = 2, then there are 4 disjoint sets [1], [5], [9, 9], [12, 13].
The total number of non-empty subsequences in an array of size N equals to -1. Hence, we just need to find the length of disjoint set whose total number of subsequences is just smaller than or equal to X and output the elements of that disjoint set. If it is not equal to X then the same step has to be done on reduced X to find the next disjoint set until X becomes 0. For making all sets disjoint, one way is to take first array element as 1 and then add 1s in 1st disjoint set equal to the length of this disjoint set and then add “d” to the elements in previous disjoint set to make another disjoint set and so on.
For e.g: X = 25, d = 100
1st disjoint set will have 4 elements as it has 15 subsequences. So 1st disjoint set will be { 1, 1, 1, 1 }.
Now X becomes 10 and thus second disjoint set will contain only 3 elements. So 2nd. disjoint will be { 101, 101, 101 } (by adding 100 in 1 to make it disjoint from 1st set). Now X becomes 3 and thus final disjoint set will contain two elements and 3rd disjoint set will be { 201, 201 } (by again adding 100 in 101 to make it disjoint from both the previous sets).
Final Output will be [1, 1, 1, 1, 101, 101, 101, 201, 201].
Below is the implementation of above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void printArray( int X, int d, int first_ele)
{
while (X > 0) {
int count_ele = 0, pow_of_two = 2;
while (X - pow_of_two + 1 >= 0) {
count_ele++;
pow_of_two *= 2;
}
X = X - (pow_of_two / 2) + 1;
for ( int j = 0; j < count_ele; j++)
cout << first_ele << " " ;
first_ele += d;
}
}
int main()
{
int d = 100, X = 25;
printArray(X, d, 1);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void printArray( int X, int d,
int first_ele)
{
while (X > 0 )
{
int count_ele = 0 ,
pow_of_two = 2 ;
while (X - pow_of_two + 1 >= 0 )
{
count_ele++;
pow_of_two *= 2 ;
}
X = X - (pow_of_two / 2 ) + 1 ;
for ( int j = 0 ;
j < count_ele; j++)
System.out.print(first_ele + " " );
first_ele += d;
}
}
public static void main (String[] args)
{
int d = 100 , X = 25 ;
printArray(X, d, 1 );
}
}
|
Python3
def printArray(X, d, first_ele):
while (X > 0 ):
count_ele, pow_of_two = 0 , 2
while (X - pow_of_two + 1 > = 0 ):
count_ele + = 1
pow_of_two * = 2
X = X - (pow_of_two / 2 ) + 1
for j in range (count_ele):
print (first_ele, end = " " )
first_ele + = d
if __name__ = = '__main__' :
d, X = 100 , 25
printArray(X, d, 1 )
|
C#
using System;
class GFG
{
static void printArray( int X, int d,
int first_ele)
{
while (X > 0)
{
int count_ele = 0,
pow_of_two = 2;
while (X - pow_of_two + 1 >= 0)
{
count_ele++;
pow_of_two *= 2;
}
X = X - (pow_of_two / 2) + 1;
for ( int j = 0;
j < count_ele; j++)
Console.Write(first_ele + " " );
first_ele += d;
}
}
public static void Main ()
{
int d = 100, X = 25;
printArray(X, d, 1);
}
}
|
PHP
<?php
function printArray( $X , $d , $first_ele )
{
while ( $X > 0)
{
$count_ele = 0;
$pow_of_two = 2;
while ( $X - $pow_of_two + 1 >= 0)
{
$count_ele ++;
$pow_of_two *= 2;
}
$X = $X - ( $pow_of_two / 2) + 1;
for ( $j = 0; $j < $count_ele ; $j ++)
echo $first_ele , " " ;
$first_ele += $d ;
}
}
$d = 100;
$X = 25;
printArray( $X , $d , 1);
?>
|
Javascript
<script>
function printArray(X, d, first_ele)
{
while (X > 0)
{
let count_ele = 0,
pow_of_two = 2;
while (X - pow_of_two + 1 >= 0)
{
count_ele++;
pow_of_two *= 2;
}
X = X - parseInt(pow_of_two / 2, 10) + 1;
for (let j = 0; j < count_ele; j++)
document.write(first_ele + " " );
first_ele += d;
}
}
let d = 100, X = 25;
printArray(X, d, 1);
</script>
|
Output1 1 1 1 101 101 101 201 201
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...