Dado un número entero N , la tarea es generar una secuencia de N números enteros positivos tal que:
- Cada elemento en la posición par debe ser mayor que el elemento que le sigue y el elemento que le precede, es decir, arr[i – 1] < arr[i] > arr[i + 1]
- La suma de los elementos debe ser par y la mínima posible (entre todas las sucesiones posibles).
Ejemplos:
Entrada: N = 4
Salida: 1 2 1 2Entrada: N = 5
Salida: 1 3 1 2 1
Planteamiento: Para obtener la sucesión con la mínima suma posible, la sucesión debe ser de la forma 1, 2, 1, 2, 1, 2, 1… y para los casos en que la suma de la sucesión no sea par, cualquier 2 de la secuencia se puede cambiar a 3 para que la suma de la secuencia sea pareja.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to print the required sequence void make_sequence(int N) { // arr[] will hold the sequence // sum variable will store the sum // of the sequence int arr[N + 1], sum = 0; for (int i = 1; i <= N; i++) { if (i % 2 == 1) arr[i] = 1; else arr[i] = 2; sum += arr[i]; } // If sum of the sequence is odd if (sum % 2 == 1) arr[2] = 3; // Print the sequence for (int i = 1; i <= N; i++) cout << arr[i] << " "; } // Driver Code int main() { int N = 9; make_sequence(N); return 0; }
Java
// Java implementation of above approach class GFG { // Function to print the required sequence static void make_sequence(int N) { // arr[] will hold the sequence // sum variable will store the sum // of the sequence int[] arr = new int[N + 1]; int sum = 0; for (int i = 1; i <= N; i++) { if (i % 2 == 1) arr[i] = 1; else arr[i] = 2; sum += arr[i]; } // If sum of the sequence is odd if (sum % 2 == 1) arr[2] = 3; // Print the sequence for (int i = 1; i <= N; i++) System.out.print(arr[i] + " "); } // Driver Code public static void main(String[] args) { int N = 9; make_sequence(N); } } // This code is contributed by iAyushRaj.
Python 3
# Python 3 implementation of above approach # Function to print the required sequence def make_sequence( N): # arr[] will hold the sequence # sum variable will store the sum # of the sequence arr = [0] * (N + 1) sum = 0 for i in range(1, N + 1): if (i % 2 == 1): arr[i] = 1 else: arr[i] = 2 sum += arr[i] # If sum of the sequence is odd if (sum % 2 == 1): arr[2] = 3 # Print the sequence for i in range(1, N + 1): print(arr[i], end = " ") # Driver Code if __name__ == "__main__": N = 9 make_sequence(N) # This code is contributed by ita_c
C#
// C# implementation of above approach using System; class GFG { // Function to print the required sequence public static void make_sequence(int N) { // arr will hold the sequence // sum variable will store the sum // of the sequence int[] arr = new int[N + 1]; int sum = 0; for (int i = 1; i <= N; i++) { if (i % 2 == 1) arr[i] = 1; else arr[i] = 2; sum += arr[i]; } // If sum of the sequence is odd if (sum % 2 == 1) arr[2] = 3; // Print the sequence for (int i = 1; i <= N; i++) Console.Write(arr[i] + " "); } // Driver Code public static void Main() { int N = 9; make_sequence(N); } } // This code is contributed by iAyushRaj.
PHP
<?php // PHP implementation of above approach // Function to print the required sequence function make_sequence($N) { // arr[] will hold the sequence // sum variable will store the sum // of the sequence $arr = array(); $sum = 0; for ($i = 1; $i <= $N; $i++) { if ($i % 2 == 1) $arr[$i] = 1; else $arr[$i] = 2; $sum += $arr[$i]; } // If sum of the sequence is odd if ($sum % 2 == 1) $arr[2] = 3; // Print the sequence for ($i = 1; $i <= $N; $i++) echo $arr[$i], " "; } // Driver Code $N = 9; make_sequence($N); // This code is contributed by Ryuga ?>
Javascript
<script> // Javascript implementation of above approach // Function to print the required sequence function make_sequence(N) { // arr[] will hold the sequence // sum variable will store the sum // of the sequence var arr = Array(N+1), sum = 0; for (var i = 1; i <= N; i++) { if (i % 2 == 1) arr[i] = 1; else arr[i] = 2; sum += arr[i]; } // If sum of the sequence is odd if (sum % 2 == 1) arr[2] = 3; // Print the sequence for (var i = 1; i <= N; i++) document.write( arr[i] + " "); } // Driver Code var N = 9; make_sequence(N); </script>
Producción:
1 3 1 2 1 2 1 2 1
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(N)