Dada una array arr[] y una posición en la array, k. Escriba un nombre de función inversa (a[], k) tal que invierta el subarreglo arr[0..k-1]. El espacio adicional utilizado debe ser O(1) y la complejidad de tiempo debe ser O(k).
Ejemplo:
Input: arr[] = {1, 2, 3, 4, 5, 6} k = 4 Output: arr[] = {4, 3, 2, 1, 5, 6}
Le recomendamos encarecidamente que minimice su navegador y que pruebe esto usted mismo primero.
A continuación se muestra la implementación de la misma.
C++
// C++ program to reverse a subarray arr[0..k-1] #include <bits/stdc++.h> using namespace std; // Reverse subarray a[0..k-1] void reverse(int a[], int n, int k) { if (k > n) { cout << "Invalid k"; return; } // One by one reverse first and last elements of a[0..k-1] for (int i = 0; i < k/2; i++) swap(a[i], a[k-i-1]); } // Driver program int main() { int a[] = {1, 2, 3, 4, 5, 6}; int n = sizeof(a) / sizeof(int), k = 4; reverse(a, n, k); for (int i = 0; i < n; ++i) printf("%d ", a[i]); return 0; }
Java
// java program to reverse a // subarray arr[0..k-1] public class GFG { // Reverse subarray a[0..k-1] static void reverse(int []a, int n, int k) { if (k > n) { System.out.println( "Invalid k"); return; } // One by one reverse first // and last elements of a[0..k-1] for (int i = 0; i < k / 2; i++) { int tempswap = a[i]; a[i] = a[k - i - 1]; a[k - i - 1] = tempswap; } } // Driver code public static void main(String args[]) { int []a = {1, 2, 3, 4, 5, 6}; int n = a.length, k = 4; reverse(a, n, k); for (int i = 0; i < n; ++i) System.out.print(a[i] + " "); } } // This code is contributed by Sam007.
Python3
# python program to reverse a subarray # arr[0..k-1] from __future__ import print_function # Reverse subarray a[0..k-1] def reverse(a, n, k): if (k > n): print( "Invalid k") return # One by one reverse first and # last elements of a[0..k-1] for i in range(0, (int)(k/2)): temp = a[i] a[i] = a[k-i-1] a[k-i-1] = temp # Driver program a = [1, 2, 3, 4, 5, 6] n = len(a) k = 4 reverse(a, n, k); for i in range(0, n): print(a[i], end=" ") # This code is contributed by Sam007.
C#
// C# program to reverse a // subarray arr[0..k-1] using System; class GFG { static void SwapNum(ref int x, ref int y) { int tempswap = x; x = y; y = tempswap; } // Reverse subarray a[0..k-1] static void reverse(int []a, int n, int k) { if (k > n) { Console.Write( "Invalid k"); return; } // One by one reverse first // and last elements of a[0..k-1] for (int i = 0; i < k / 2; i++) SwapNum(ref a[i], ref a[k - i - 1]); } // Driver Code public static void Main() { int []a = {1, 2, 3, 4, 5, 6}; int n = a.Length, k = 4; reverse(a, n, k); for (int i = 0; i < n; ++i) Console.Write(a[i] + " "); } } // This code is contributed by Sam007
Javascript
<script> // Javascript program to reverse // a subarray arr[0..k-1] // Reverse subarray a[0..k-1] function reverse( a, n, k) { if (k > n) { document.write("Invalid k"); return; } // One by one reverse first // and last elements of a[0..k-1] for (let i = 0; i < Math.floor(k/2); i++) { let temp = a[i] ; a[i] = a[k-i-1] ; a[k-i-1] = temp ; } } // driver code let a = [1, 2, 3, 4, 5, 6]; let n = a.length, k = 4; reverse(a, n, k); for (let i = 0; i < n; ++i) document.write(a[i] + " "); </script>
4 3 2 1 5 6
Complejidad del tiempo: O(k)
Método 2 (usando STL):
En este método, utilizaremos una función STL de C++ incorporada denominada inversa. Esta función completa la tarea de invertir K elementos de la array en tiempo O(K) y tampoco utiliza espacio adicional.
La implementación de este método se encuentra a continuación.
C++
// C++ program to reverse the first K // elements using in-built function #include <bits/stdc++.h> using namespace std; int main() { int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; int k = 4; // STL function to reverse element // from 0 index to K-1 index. reverse(arr, arr + k); // printing the array after reversing // first K elements. for (int i = 0; i < 8; i++) { cout << arr[i] << " "; } return 0; } // this code is contributed by Machhaliya Muhammad
Python3
# Python3 program to reverse the first K # elements using in-built function arr = [ 1, 2, 3, 4, 5, 6, 7, 8 ] k = 4 # Using list slicing to reverse the array # from 0 index to K-1 index. arr[:k] = arr[:k][::-1] # printing the array after reversing # first K elements. print(*arr) # This code is contributed by phasing17
4 3 2 1 5 6 7 8
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA