Norway


Given three positive integer n, s and k. The task is to all possible sequence of length s, with n and the absolute between element is less than k.

Examples:

Input : n = 5, s = 3, k = 2
Output :
5 5 5 
5 5 6 
5 5 4 
5 6 6 
5 6 7 
5 6 5 
5 4 4 
5 4 5 
5 4 3 

Input : n = 3, s = 2, k = 1
Output :
3 3 

Observe, to get the absolute difference between consecutive element less than k, we can increase from 0 to k – 1. Similarly, we can decrease the next element from 1 to k – 1.
Now, to form the required sequence, we will first push ‘n’ to the vector. And then try to fill the other element of the sequence by making recursive call for each element in the sequence. At each recursive call we run a loop from 0 to k – 1 and add (n + i) to the sequence. Once we make the sequence of size ‘s’, we will print the whole sequence and return back to the recursively calling function and remove (n + i).
Similarly, we can run loop from 1 to k – 1 and insert (n – i) to next element position.
To check the number of remaining element required we will pass size – 1 to recursive call and when size become 0, we will print the whole sequence.

Below is the C++ implementation of this approach:

// CPP Program all sequence of length s 
// starting with n such that difference 
// between consecutive element is less than k.
#include <bits/stdc++.h>
using namespace std;

// Recursive function to print all sequence 
// of length s starting with n such that 
// difference between consecutive element 
// is less than k.
void printSequence(vector<int>& v, int n,
                               int s, int k)
{
    // If size become 0, print the sequence.
    if (s == 0) {
        for (int i = 0; i < v.size(); i++)
            cout << v[i] << " ";
        cout << endl;
        return;
    }

    // Increment the next element and make 
    // recursive call after inserting the 
    // (n + i) to the sequence.
    for (int i = 0; i < k; i++) {
        v.push_back(n + i);
        printSequence(v, n + i, s - 1, k);
        v.pop_back();
    }

    // Decrementing the next element and'
    // make recursive call after inserting 
    // the (n - i) to the sequence.
    for (int i = 1; i < k; i++) {
        v.push_back(n - i);
        printSequence(v, n - i, s - 1, k);
        v.pop_back();
    }
}

// Wrapper Function
void wrapper(int n, int s, int k)
{
    vector<int> v;
    v.push_back(n);
    printSequence(v, n, s - 1, k);
}

// Driven Program
int main()
{
    int n = 5, s = 3, k = 2;
    wrapper(n, s, k);
    return 0;
}

Output

5 5 5 
5 5 6 
5 5 4 
5 6 6 
5 6 7 
5 6 5 
5 4 4 
5 4 5 
5 4 3


- avatar - Print all sequences starting with n and consecutive difference limited to k

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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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







Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here