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 <bits/stdc++.h>using namespace std;// Recursive function to generate a// Bitonic array where consecutive// elements are at the difference of Kint 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 increasing sequence cout << n << " "; return 1;}// Driver Codeint 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 Kimport java.util.*;class GFG{ // Recursive function to generate a// Bitonic array where consecutive// elements are at the difference of Kstatic 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 increasing sequence System.out.print(n + " "); return 1;} // Driver Codepublic 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 Kdef 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 increasing sequence print(n, end = " "); return 1;# Driver Coden = 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 Kusing System;class GFG{// Recursive function to generate a// Bitonic array where consecutive// elements are at the difference of Kstatic 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 increasing sequence Console.Write(n + " "); return 1;}// Driver Codepublic static void Main(String[] args){ int n = 10, k = 5; Console.Write(n + " "); decreseq(n, k);}}// This code is contributed by gauravrajput1 |
Javascript
<script>// Javascript 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 Kfunction decreseq(n, k){ // Recursively call until N > 0 if (n > 0) { // Print decreasing sequence document.write( n - k + " "); decreseq(n - k, k); } // if N less than 0 then // particular function return 1 if (n <= 0) return 1; // Print increasing sequence document.write( n + " "); return 1;}// Driver Codevar n = 10, k = 5;document.write( n + " ");decreseq(n, k);// This code is contributed by itsok.</script> |
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.



