Dados dos enteros N y K , la tarea es generar una array bitónica donde el primer elemento es N y cada elemento está en la diferencia de K.
Ejemplos:
Entrada: N = 10, K = 5
Salida: 10 5 0 5 10
Entrada: N = 16, K = 5
Salida: 16 11 6 1 -4 1 6 11 16
Enfoque: La idea es utilizar la recursividad para resolver este problema. Como se indica en el problema, el primer elemento de la array bitónica es N . Por lo tanto, agréguelo a la array y resuelva la N. A continuación se muestra la definición de la función recursiva:
- Caso base: cuando el valor de N es menor que igual a 0, devuelva 1 porque ahora los valores aumentarán.
- Caso recursivo: si el valor de N es mayor que , luego agregue N – K y llame recursivamente a N – K y finalmente agregue N.
A continuación se muestra la implementación del enfoque anterior:
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>
Producción:
10 5 0 5 10
Publicación traducida automáticamente
Artículo escrito por rajasek155 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA