Print all sequences starting with n and consecutive difference limited to k
Given three positive integer n, s and k. The task is to print all possible sequence of length s, starting with n and the absolute difference between consecutive 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 implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printSequence(vector< int >& v, int n,
int s, int k)
{
if (s == 0) {
for ( int i = 0; i < v.size(); i++)
cout << v[i] << " " ;
cout << endl;
return ;
}
for ( int i = 0; i < k; i++) {
v.push_back(n + i);
printSequence(v, n + i, s - 1, k);
v.pop_back();
}
for ( int i = 1; i < k; i++) {
v.push_back(n - i);
printSequence(v, n - i, s - 1, k);
v.pop_back();
}
}
void wrapper( int n, int s, int k)
{
vector< int > v;
v.push_back(n);
printSequence(v, n, s - 1, k);
}
int main()
{
int n = 5, s = 3, k = 2;
wrapper(n, s, k);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static List<Integer> v = new ArrayList<Integer>();
static void printSequence( int n,
int s, int k)
{
if (s == 0 ) {
for ( int i = 0 ; i < v.size(); i++)
System.out.print(v.get(i) + " " );
System.out.println();
return ;
}
for ( int i = 0 ; i < k; i++) {
v.add(n + i);
printSequence(n + i, s - 1 , k);
v.remove(v.size() - 1 );
}
for ( int i = 1 ; i < k; i++) {
v.add(n - i);
printSequence(n - i, s - 1 , k);
v.remove(v.size() - 1 );
}
}
static void wrapper( int n, int s, int k)
{
v.add(n);
printSequence(n, s - 1 , k);
}
public static void main(String args[])
{
int n = 5 , s = 3 , k = 2 ;
wrapper(n, s, k);
}
}
|
Python3
def printSequence(v, n, s, k):
if (s = = 0 ) :
for i in range ( 0 , len (v)):
print ( "{} " . format (v[i]), end = "")
print ("")
return ;
for i in range ( 0 ,k):
v.append(n + i)
printSequence(v, n + i, s - 1 , k)
v.pop()
for i in range ( 1 ,k):
v.append(n - i)
printSequence(v, n - i, s - 1 , k)
v.pop()
def wrapper(n, s, k):
v = []
v.append(n)
printSequence(v, n, s - 1 , k)
n = 5 ; s = 3 ; k = 2 ;
wrapper(n, s, k);
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
class GFG {
static void printSequence( ref List< int > v, int n,
int s, int k)
{
if (s == 0) {
for ( int i = 0; i < v.Count; i++)
Console.Write(v[i] + " " );
Console.WriteLine();
return ;
}
for ( int i = 0; i < k; i++) {
v.Add(n + i);
printSequence( ref v, n + i, s - 1, k);
v.RemoveAt(v.Count - 1);
}
for ( int i = 1; i < k; i++) {
v.Add(n - i);
printSequence( ref v, n - i, s - 1, k);
v.RemoveAt(v.Count - 1);
}
}
static void wrapper( int n, int s, int k)
{
List< int > v = new List< int >();
v.Add(n);
printSequence( ref v, n, s - 1, k);
}
public static void Main()
{
int n = 5, s = 3, k = 2;
wrapper(n, s, k);
}
}
|
PHP
<?php
function printSequence( $v , $n ,
$s , $k )
{
if ( $s == 0)
{
for ( $i = 0; $i < count ( $v ); $i ++)
echo ( $v [ $i ]. " " );
echo ( "\n" );
return ;
}
for ( $i = 0; $i < $k ; $i ++)
{
array_push ( $v , $n + $i );
printSequence( $v , $n + $i ,
$s - 1, $k );
array_pop ( $v );
}
for ( $i = 1; $i < $k ; $i ++)
{
array_push ( $v , $n - $i );
printSequence( $v , $n - $i ,
$s - 1, $k );
array_pop ( $v );
}
}
function wrapper( $n , $s , $k )
{
$v = array ();;
array_push ( $v , $n );
printSequence( $v , $n , $s - 1, $k );
}
$n = 5;
$s = 3;
$k = 2;
wrapper( $n , $s , $k );
?>
|
Javascript
<script>
v = []
function printSequence(n, s, k)
{
if (s == 0) {
for ( var i = 0; i < v.length; i++)
document.write( v[i] + " " );
document.write( "<br>" );
return ;
}
for ( var i = 0; i < k; i++) {
v.push(n + i);
printSequence(n + i, s - 1, k);
v.pop();
}
for ( var i = 1; i < k; i++) {
v.push(n - i);
printSequence(n - i, s - 1, k);
v.pop();
}
}
function wrapper(n, s, k)
{
v.push(n);
printSequence( n, s - 1, k);
}
var n = 5, s = 3, k = 2;
wrapper(n, s, k);
</script>
|
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
Last Updated :
05 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...