# Generate a Bitonic array starting with N and adjacent difference of K

Given two integers N and K, the task is to generate a bitonic array where the first element is N and every element is at the difference of K.
Examples:

Input: N = 10, K = 5
Output: 10 5 0 5 10
Input: N = 16, K = 5
Output: 16 11 6 1 -4 1 6 11 16

Approach: The idea is to use recursion to solve this problem. As stated in the problem, the first element of the bitonic array is N. Therefore, append it into the array and solve for the N. Below is the recursive function definition:

• Base Case: When the value of N is less than equal to 0, Then return 1 because now the values will increase.
• Recursive Case: If the value of the N is greater than , Then append N – K and recursively call for the N – K and finally append N.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to generate a ` `// Bitonic array where consecutive  ` `// elements are at difference of K ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Recursive function to generate a ` `// Bitonic array where consecutive  ` `// elements are at the difference of K ` `int` `decreseq(``int` `n, ``int` `k) ` `{ ` `    ``// Recursively call until N > 0 ` `    ``if` `(n > 0) { ` `         `  `        ``// Print decreasing sequence ` `        ``cout << n - k << ``" "``; ` `        ``decreseq(n - k, k); ` `    ``} ` `     `  `    ``// if N less than 0 then  ` `    ``// particular function return 1 ` `    ``if` `(n <= 0) ` `        ``return` `1; ` `         `  `    ``// Print incresing sequence ` `    ``cout << n << ``" "``; ` `    ``return` `1; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 10, k = 5; ` `    ``cout << n << ``" "``; ` `    ``decreseq(n, k); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation to generate a ` `// Bitonic array where consecutive  ` `// elements are at difference of K ` `import` `java.util.*; ` `class` `GFG{ ` `  `  `// Recursive function to generate a ` `// Bitonic array where consecutive  ` `// elements are at the difference of K ` `static` `int` `decreseq(``int` `n, ``int` `k) ` `{ ` `    ``// Recursively call until N > 0 ` `    ``if` `(n > ``0``)  ` `    ``{ ` `          `  `        ``// Print decreasing sequence ` `        ``System.out.print(n - k + ``" "``); ` `        ``decreseq(n - k, k); ` `    ``} ` `      `  `    ``// if N less than 0 then  ` `    ``// particular function return 1 ` `    ``if` `(n <= ``0``) ` `        ``return` `1``; ` `          `  `    ``// Print incresing sequence ` `    ``System.out.print(n + ``" "``); ` `    ``return` `1``; ` `} ` `  `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``10``, k = ``5``; ` `    ``System.out.print(n+ ``" "``); ` `    ``decreseq(n, k); ` `} ` `} ` ` `  `// This code is contributed by sapnasingh4991`

## Python3

 `# Python3 implementation to generate a ` `# Bitonic array where consecutive  ` `# elements are at difference of K ` ` `  `# Recursive function to generate a ` `# Bitonic array where consecutive  ` `# elements are at the difference of K ` `def` `decreseq(n, k): ` ` `  `    ``# Recursively call until N > 0 ` `    ``if` `(n > ``0``): ` `         `  `        ``# Print decreasing sequence ` `        ``print``(n ``-` `k, end ``=` `" "``); ` `        ``decreseq(n ``-` `k, k); ` `     `  `    ``# if N less than 0 then  ` `    ``# particular function return 1 ` `    ``if` `(n <``=` `0``): ` `        ``return` `1``; ` `         `  `    ``# Print incresing sequence ` `    ``print``(n, end ``=` `" "``); ` `    ``return` `1``; ` ` `  `# Driver Code ` `n ``=` `10``; k ``=` `5``; ` `print``(n, end ``=` `" "``); ` `decreseq(n, k); ` ` `  `# This code is contributed by Code_Mech  `

## C#

 `// C# implementation to generate a ` `// Bitonic array where consecutive  ` `// elements are at difference of K ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Recursive function to generate a ` `// Bitonic array where consecutive  ` `// elements are at the difference of K ` `static` `int` `decreseq(``int` `n, ``int` `k) ` `{ ` `     `  `    ``// Recursively call until N > 0 ` `    ``if` `(n > 0)  ` `    ``{ ` `         `  `        ``// Print decreasing sequence ` `        ``Console.Write(n - k + ``" "``); ` `        ``decreseq(n - k, k); ` `    ``} ` `     `  `    ``// If N less than 0 then  ` `    ``// particular function return 1 ` `    ``if` `(n <= 0) ` `        ``return` `1; ` `         `  `    ``// Print incresing sequence ` `    ``Console.Write(n + ``" "``); ` `    ``return` `1; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `n = 10, k = 5; ` `     `  `    ``Console.Write(n + ``" "``); ` `     `  `    ``decreseq(n, k); ` `} ` `} ` ` `  `// This code is contributed by gauravrajput1 `

Output:

`10 5 0 5 10`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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.

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.