Dada una array, invierta cada sub-array formada por k elementos consecutivos.
Ejemplos:
Entrada: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3.
Salida: [3, 2, 1, 6, 5, 4, 9, 8, 7 , 10]
Entrada: arr = [1, 2, 3, 4, 5, 6, 7], k = 5.
Salida: [5, 4, 3, 2, 1, 7, 6]
Acercarse:
- Usaremos la técnica de dos punteros para resolver este problema.
- Primero, inicializaremos nuestro primer puntero d con valor k-1 ( d = k-1 ) y una variable m con valor 2 ( m = 2 ).
- Ahora, iteraremos la array con nuestro segundo puntero i y verificaremos
- Si i < d , Intercambiar (arr[i], arr[d]) y disminuir d en 1. De lo contrario,
- Haz d = k * m – 1 , i = k * (m – 1) – 1 y m = m + 1.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program to reverse every sub-array // formed by consecutive k elements #include<bits/stdc++.h> using namespace std; // Function to reverse every sub-array // formed by consecutive k elements void ReverseInGroup(int arr[], int n, int k) { if(n < k) { k = n; } // Initialize variables int d = k - 1, m = 2; int i = 0; for(i = 0; i < n; i++) { if (i >= d) { // Update the variables d = k * (m); if(d >= n) { d = n; } i = k * (m - 1) - 1; m++; } else { int t = arr[i]; arr[i] = arr[d]; arr[d] = t; } d = d - 1; } return; } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; int k = 3; int n = sizeof(arr) / sizeof(arr[0]); ReverseInGroup(arr, n, k); for(int i = 0; i < n; i++) cout << arr[i] << " "; return 0; } // This code is contributed by Code_Mech
C
// C program to reverse every sub-array // formed by consecutive k elements #include<stdio.h> // Function to reverse every sub-array // formed by consecutive k elements void ReverseInGroup(int arr[], int n, int k) { if(n<k) { k=n; } // Initialize variables int d = k-1, m=2; int i = 0; for (i = 0; i < n; i++) { if (i >= d) { // Update the variables d = k * (m); if(d>=n) { d = n; } i = k * (m - 1)-1; m++; } else { int t = arr[i]; arr[i] = arr[d]; arr[d] = t; } d = d - 1; } return; } // Driver code int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7}; int k = 3; int n = sizeof(arr) / sizeof(arr[0]); ReverseInGroup(arr, n, k); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }
Java
// Java program to reverse every sub-array // formed by consecutive k elements class GFG{ // Function to reverse every sub-array // formed by consecutive k elements static void ReverseInGroup(int arr[], int n, int k) { if(n < k) { k = n; } // Initialize variables int d = k - 1, m = 2; int i = 0; for(i = 0; i < n; i++) { if (i >= d) { // Update the variables d = k * (m); if(d >= n) { d = n; } i = k * (m - 1) - 1; m++; } else { int t = arr[i]; arr[i] = arr[d]; arr[d] = t; } d = d - 1; } return; } // Driver code public static void main(String[] args) { int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; int k = 3; int n = arr.length; ReverseInGroup(arr, n, k); for(int i = 0; i < n; i++) System.out.printf("%d ", arr[i]); } } // This code is contributed by sapnasingh4991
Python3
# Python3 program to reverse # every sub-array formed by # consecutive k elements # Function to reverse every # sub-array formed by consecutive # k elements def ReverseInGroup(arr, n, k): if(n < k): k = n # Initialize variables d = k - 1 m = 2 i = 0 while i < n: if (i >= d): # Update the # variables d = k * (m) if(d >= n): d = n i = k * (m - 1) - 1 m += 1 else: arr[i], arr[d] = (arr[d], arr[i]) d = d - 1 i += 1 return # Driver code if __name__ == "__main__": arr = [1, 2, 3, 4, 5, 6, 7] k = 3 n = len(arr) ReverseInGroup(arr, n, k) for i in range(n): print(arr[i], end = " ") # This code is contributed by Chitranayal
C#
// C# program to reverse every sub-array // formed by consecutive k elements using System; class GFG{ // Function to reverse every sub-array // formed by consecutive k elements static void ReverseInGroup(int []arr, int n, int k) { if(n < k) { k = n; } // Initialize variables int d = k - 1, m = 2; int i = 0; for(i = 0; i < n; i++) { if (i >= d) { // Update the variables d = k * (m); if(d >= n) { d = n; } i = k * (m - 1) - 1; m++; } else { int t = arr[i]; arr[i] = arr[d]; arr[d] = t; } d = d - 1; } return; } // Driver code public static void Main() { int []arr = { 1, 2, 3, 4, 5, 6, 7 }; int k = 3; int n = arr.Length; ReverseInGroup(arr, n, k); for(int i = 0; i < n; i++) Console.Write(arr[i] + " "); } } // This code is contributed by Code_Mech
Javascript
<script> // Javascript program to reverse every sub-array // formed by consecutive k elements // Function to reverse every sub-array // formed by consecutive k elements function ReverseInGroup(arr, n, k) { if(n < k) { k = n; } // Initialize variables let d = k - 1, m = 2; let i = 0; for(i = 0; i < n; i++) { if (i >= d) { // Update the variables d = k * (m); if(d >= n) { d = n; } i = k * (m - 1) - 1; m++; } else { let t = arr[i]; arr[i] = arr[d]; arr[d] = t; } d = d - 1; } return; } // Driver code let arr = [ 1, 2, 3, 4, 5, 6, 7 ]; let k = 3; let n = arr.length; ReverseInGroup(arr, n, k); for(let i = 0; i < n; i++) document.write(arr[i] + " "); // This code is contributed by _saurabh_jaiswal </script>
Producción:
3 2 1 6 5 4 7
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por vaibhavgopanpalli18 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA