Dado un entero positivo N , la tarea es construir una array que consta de los primeros N números naturales de modo que cada par de elementos adyacentes en la array sea un coprimo . Si existen varias soluciones, imprima cualquiera de ellas.
Ejemplos:
Entrada: N = 4
Salida: 4 1 3 2
Explicación:
Todos los pares adyacentes posibles del arreglo son { (4, 1), (1, 3), (3, 2) }
Dado que todos los pares adyacentes del arreglo son co- prima, la salida requerida es 4 1 3 2.Entrada: N = 7
Salida: 3 4 5 7 6 1 2
Enfoque: La idea es iterar sobre el rango [1, N] usando la variable i e imprimir el valor de i . Las siguientes son las observaciones:
MCD(i, i + 1) = 1
Por lo tanto, para todos los valores posibles de i el par (i, i + 1) son números coprimos.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement the above approach #include <bits/stdc++.h> using namespace std; // Function to construct an array with adjacent elements as // co-prime numbers void ConstArrayAdjacentCoprime(int N) { // Iterate over the range [1, N] for (int i = 1; i <= N; i++) cout << i << " "; } // Driver Code int main() { int N = 6; ConstArrayAdjacentCoprime(N); return 0; } // This code is contributed by Sania Kumari Gupta
C
// C program to implement the above approach #include <stdio.h> // Function to construct an array with adjacent elements as // co-prime numbers void ConstArrayAdjacentCoprime(int N) { // Iterate over the range [1, N] for (int i = 1; i <= N; i++) printf("%d ", i); } // Driver Code int main() { int N = 6; ConstArrayAdjacentCoprime(N); return 0; } // This code is contributed by Sania Kumari Gupta
Java
// Java program to implement // the above approach class GFG { // Function to construct an array with // adjacent elements as co-prime numbers static void ConstArrayAdjacentCoprime(int N) { // Iterate over the range [1, N] for (int i = 1; i <= N; i++) { // Print i System.out.print(i + " "); } } // Driver Code public static void main (String[] args) { int N = 6; ConstArrayAdjacentCoprime(N); } } // This code is contributed by AnkitRai01
Python3
# Python3 program to implement # the above approach # Function to construct an array with # adjacent elements as co-prime numbers def ConstArrayAdjacentCoprime(N): # Iterate over the range [1, N] for i in range(1, N + 1): # Print i print(i, end = " ") # Driver Code if __name__ == "__main__" : N = 6 ConstArrayAdjacentCoprime(N) # This code is contributed by AnkitRai01
C#
// C# program to implement // the above approach using System; class GFG{ // Function to construct an array with // adjacent elements as co-prime numbers static void ConstArrayAdjacentCoprime(int N) { // Iterate over the range [1, N] for(int i = 1; i <= N; i++) { // Print i Console.Write(i + " "); } } // Driver Code public static void Main () { int N = 6; ConstArrayAdjacentCoprime(N); } } // This code is contributed by susmitakundugoaldanga
Javascript
<script> // javascript program to implement // the above approach // Function to construct an array with // adjacent elements as co-prime numbers function ConstArrayAdjacentCoprime(N) { // Iterate over the range [1, N] for (let i = 1; i <= N; i++) { // Print i document.write(i + " "); } } // Driver code let N = 6; ConstArrayAdjacentCoprime(N); // This code is contributed by target_2. </script>
1 2 3 4 5 6
Complejidad temporal: O(N)
Espacio auxiliar: O(1)