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 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 increasing 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 increasing 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 increasing 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 increasing 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 |
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 K function 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 Code var n = 10, k = 5; document.write( n + " " ); decreseq(n, k); // This code is contributed by itsok. </script> |
Output:
10 5 0 5 10
Please Login to comment...