Dada la longitud de una array de números enteros N y un número entero K . La tarea es modificar la array de tal manera que la array contenga primero todos los enteros impares del 1 al N en orden ascendente, luego todos los enteros pares del 1 al N en orden ascendente y luego imprima el K -ésimo elemento en la array modificada.
Ejemplos:
Entrada: N = 8, K = 5
Salida: 2
La array será {1, 3, 5, 7, 2, 4, 6, 8}
y el quinto elemento es 2.
Entrada: N = 7, K = 2
Salida : 3
Enfoque ingenuo: un enfoque simple es almacenar primero los números impares, uno por uno hasta N , y luego almacenar los números pares uno por uno hasta N , y luego imprimir el k-ésimo elemento.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the kth element // in the modified array int getNumber(int n, int k) { int arr[n]; int i = 0; // First odd number int odd = 1; while (odd <= n) { // Insert the odd number arr[i++] = odd; // Next odd number odd += 2; } // First even number int even = 2; while (even <= n) { // Insert the even number arr[i++] = even; // Next even number even += 2; } // Return the kth element return arr[k - 1]; } // Driver code int main() { int n = 8, k = 5; cout << getNumber(n, k); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the kth element // in the modified array static int getNumber(int n, int k) { int []arr = new int[n]; int i = 0; // First odd number int odd = 1; while (odd <= n) { // Insert the odd number arr[i++] = odd; // Next odd number odd += 2; } // First even number int even = 2; while (even <= n) { // Insert the even number arr[i++] = even; // Next even number even += 2; } // Return the kth element return arr[k - 1]; } // Driver code public static void main(String[] args) { int n = 8, k = 5; System.out.println(getNumber(n, k)); } } // This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach # Function to return the kth element # in the modified array def getNumber(n, k): arr = [0] * n; i = 0; # First odd number odd = 1; while (odd <= n): # Insert the odd number arr[i] = odd; i += 1; # Next odd number odd += 2; # First even number even = 2; while (even <= n): # Insert the even number arr[i] = even; i += 1; # Next even number even += 2; # Return the kth element return arr[k - 1]; # Driver code if __name__ == '__main__': n = 8; k = 5; print(getNumber(n, k)); # This code is contributed by Rajput-Ji
C#
// C# implementation of the approach using System; class GFG { // Function to return the kth element // in the modified array static int getNumber(int n, int k) { int []arr = new int[n]; int i = 0; // First odd number int odd = 1; while (odd <= n) { // Insert the odd number arr[i++] = odd; // Next odd number odd += 2; } // First even number int even = 2; while (even <= n) { // Insert the even number arr[i++] = even; // Next even number even += 2; } // Return the kth element return arr[k - 1]; } // Driver code public static void Main(String[] args) { int n = 8, k = 5; Console.WriteLine(getNumber(n, k)); } } // This code is contributed by PrinciRaj1992
Javascript
<script> // C++ implementation of the approach // Function to return the kth element // in the modified array function getNumber(n, k) { var arr = Array(n).fill(n); var i = 0; // First odd number var odd = 1; while (odd <= n) { // Insert the odd number arr[i++] = odd; // Next odd number odd += 2; } // First even number var even = 2; while (even <= n) { // Insert the even number arr[i++] = even; // Next even number even += 2; } // Return the kth element return arr[k - 1]; } // Driver code var n = 8, k = 5; document.write(getNumber(n, k)); </script>
2
Complejidad temporal: O(n)
Espacio auxiliar: O(n), ya que se utiliza un espacio adicional de tamaño n
Enfoque eficiente: encuentre el índice donde se almacenará el primer elemento par en la array generada. Ahora, si el valor de k es menor o igual que el índice, entonces el número deseado será k * 2 – 1, de lo contrario, el número deseado será (k – índice) * 2
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the kth element // in the modified array int getNumber(int n, int k) { int pos; // Finding the index from where the // even numbers will be stored if (n % 2 == 0) { pos = n / 2; } else { pos = (n / 2) + 1; } // Return the kth element if (k <= pos) { return (k * 2 - 1); } else return ((k - pos) * 2); } // Driver code int main() { int n = 8, k = 5; cout << getNumber(n, k); return 0; }
Java
// Java implementation of the approach import java.io.*; class GFG { // Function to return the kth element // in the modified array static int getNumber(int n, int k) { int pos; // Finding the index from where the // even numbers will be stored if ((n % 2) == 0) { pos = n / 2; } else { pos = (n / 2) + 1; } // Return the kth element if (k <= pos) { return (k * 2 - 1); } else return ((k - pos) * 2); } // Driver code public static void main (String[] args) { int n = 8, k = 5; System.out.println (getNumber(n, k)); } } // This code is contributed by @tushil.
Python3
# Python3 implementation of the approach # Function to return the kth element # in the modified array def getNumber(n, k) : # Finding the index from where the # even numbers will be stored if (n % 2 == 0) : pos = n // 2; else : pos = (n // 2) + 1; # Return the kth element if (k <= pos) : return (k * 2 - 1); else : return ((k - pos) * 2); # Driver code if __name__ == "__main__" : n = 8; k = 5; print(getNumber(n, k)); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; class GFG { // Function to return the kth element // in the modified array static int getNumber(int n, int k) { int pos; // Finding the index from where the // even numbers will be stored if ((n % 2) == 0) { pos = n / 2; } else { pos = (n / 2) + 1; } // Return the kth element if (k <= pos) { return (k * 2 - 1); } else return ((k - pos) * 2); } // Driver code static public void Main () { int n = 8, k = 5; Console.Write(getNumber(n, k)); } } // This code is contributed by @ajit.
Javascript
<script> // Javascript implementation of the approach // Function to return the kth element // in the modified array function getNumber(n, k) { let pos; // Finding the index from where the // even numbers will be stored if ((n % 2) == 0) { pos = parseInt(n / 2, 10); } else { pos = parseInt(n / 2, 10) + 1; } // Return the kth element if (k <= pos) { return (k * 2 - 1); } else return ((k - pos) * 2); } let n = 8, k = 5; document.write(getNumber(n, k)); </script>
2
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por dangenmaster2 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA