Construct array having X subsequences with maximum difference smaller than d

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 subsequence ([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 2^n-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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to output an array having exactly X
// subsequences where difference between maximum
// and minimum element of each subsequence is less
// than d
#include <bits/stdc++.h>
using namespace std;
  
// This function outputs the desired array.
void printArray(int X, int d, int first_ele)
{
    // Iterate till all the disjoint sets are found.
    while (X > 0) {
  
        // count_ele the elements in one disjoint
        // set.  pow_of_two will keep all the 
        // powers of twos.
        int count_ele = 0, pow_of_two = 2;
  
        // Iterate to know the maximum length of 
        // disjoint set by checking whether X is
        // greater than the total number of 
        // possible not empty sequences of that 
        // disjoint set.
        while (X - pow_of_two + 1 >= 0) {
            count_ele++;
            pow_of_two *= 2;
        }
  
        // now deleting the total subsequences of
        // the maximum length disjoint set from X.
        X = X - (pow_of_two / 2) + 1;
  
        // outputing the disjoint set having equal
        // elements.
        for (int j = 0; j < count_ele; j++)
            cout << first_ele << " ";
  
        // by adding d, it makes another disjoint
        // set of equal elements.
        first_ele += d;
    }
}
  
// Driver Code
int main()
{
    int d = 100, X = 25;
    printArray(X, d, 1);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to output 
// an array having exactly
// X subsequences where 
// difference between maximum
// and minimum element of 
// each subsequence is less
// than d
import java.io.*;
  
class GFG 
{
      
// This function outputs 
// the desired array.
static void printArray(int X, int d, 
                       int first_ele)
{
    // Iterate till all the 
    // disjoint sets are found.
    while (X > 0
    {
  
        // count_ele the elements 
        // in one disjoint set. 
        // pow_of_two will keep 
        // all the powers of twos.
        int count_ele = 0
            pow_of_two = 2;
  
        // Iterate to know the 
        // maximum length of 
        // disjoint set by checking
        // whether X is greater than
        // the total number of possible
        // not empty sequences of that 
        // disjoint set.
        while (X - pow_of_two + 1 >= 0)
        {
            count_ele++;
            pow_of_two *= 2;
        }
  
        // now deleting the total 
        // subsequences of the maximum
        // length disjoint set from X.
        X = X - (pow_of_two / 2) + 1;
  
        // outputing the disjoint 
        // set having equal elements.
        for (int j = 0
                 j < count_ele; j++)
            System.out.print(first_ele + " ");
  
        // by adding d, it makes
        // another disjoint set 
        // of equal elements.
        first_ele += d;
    }
}
  
// Driver Code
public static void main (String[] args) 
{
    int d = 100, X = 25;
    printArray(X, d, 1);
}
}
  
// This code is contributed
// by anuj_67.

chevron_right


Python3

# Python 3 Program to output an array having
# exactly X subsequences where difference
# between maximum and minimum element of each
# subsequence is less than d

# This function outputs the desired array.
def printArray(X, d, first_ele):

# Iterate till all the disjoint
# sets are found.
while (X > 0):

# count_ele the elements in one
# disjoint set. pow_of_two will
# keep all the powers of twos.
count_ele, pow_of_two = 0, 2

# Iterate to know the maximum length of
# disjoint set by checking whether X is
# greater than the total number of
# possible not empty sequences of that
# disjoint set.
while (X – pow_of_two + 1 >= 0):
count_ele += 1
pow_of_two *= 2

# now deleting the total subsequences of
# the maximum length disjoint set from X.
X = X – (pow_of_two / 2) + 1

# outputing the disjoint set having
# equal elements.
for j in range(count_ele):
print(first_ele, end = ” “)

# by adding d, it makes another
# disjoint set of equal elements.
first_ele += d

# Driver Code
if __name__ == ‘__main__’:
d, X = 100, 25
printArray(X, d, 1)

# This code is contributed by PrinciRaj19992

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to output 
// an array having exactly
// X subsequences where 
// difference between maximum
// and minimum element of 
// each subsequence is less
// than d
using System;
  
class GFG 
{
      
// This function outputs 
// the desired array.
static void printArray(int X, int d, 
                       int first_ele)
{
    // Iterate till all the 
    // disjoint sets are found.
    while (X > 0) 
    {
  
        // count_ele the elements 
        // in one disjoint set. 
        // pow_of_two will keep 
        // all the powers of twos.
        int count_ele = 0, 
            pow_of_two = 2;
  
        // Iterate to know the 
        // maximum length of 
        // disjoint set by checking
        // whether X is greater than
        // the total number of possible
        // not empty sequences of that 
        // disjoint set.
        while (X - pow_of_two + 1 >= 0)
        {
            count_ele++;
            pow_of_two *= 2;
        }
  
        // now deleting the total 
        // subsequences of the maximum
        // length disjoint set from X.
        X = X - (pow_of_two / 2) + 1;
  
        // outputing the disjoint 
        // set having equal elements.
        for (int j = 0; 
                 j < count_ele; j++)
            Console.Write(first_ele + " ");
  
        // by adding d, it makes
        // another disjoint set 
        // of equal elements.
        first_ele += d;
    }
}
  
// Driver Code
public static void Main () 
{
    int d = 100, X = 25;
    printArray(X, d, 1);
}
}
  
// This code is contributed
// by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to output an array 
// having exactly X subsequences 
// where difference between maximum 
// and minimum element of each  
// subsequence is less than d 
  
// This function outputs the
// desired array. 
function printArray($X, $d,$first_ele
    // Iterate till all the disjoint
    // sets are found. 
    while ($X > 0) 
    
  
        // count_ele the elements in 
        // one disjoint set. pow_of_two 
        // will keep all the powers of twos. 
        $count_ele = 0;
        $pow_of_two = 2; 
  
        // Iterate to know the maximum 
        // length of disjoint set by 
        // checking whether X is greater 
        // than the total number of possible
        // not empty sequences of that 
        // disjoint set. 
        while ($X - $pow_of_two + 1 >= 0) 
        
            $count_ele++; 
            $pow_of_two *= 2; 
        
  
        // now deleting the total subsequences of 
        // the maximum length disjoint set from X. 
        $X = $X - ($pow_of_two / 2) + 1; 
  
        // outputing the disjoint set 
        // having equal elements. 
        for ($j = 0; $j < $count_ele; $j++) 
                echo $first_ele , " "
  
        // by adding d, it makes another 
        // disjoint set of equal elements. 
        $first_ele += $d
    
  
// Driver Code 
$d = 100;
$X = 25; 
printArray($X, $d, 1); 
  
// This code is contributed by ajit 
?>

chevron_right


Output:

1 1 1 1 101 101 101 201 201


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m, jit_t, princiraj1992



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.