Reverse an array upto a given position
Given an array arr[] and a position in array, k. Write a function name reverse (a[], k) such that it reverses subarray arr[0..k-1]. Extra space used should be O(1) and time complexity should be O(k).
Example:
Input:
arr[] = {1, 2, 3, 4, 5, 6}
k = 4
Output:
arr[] = {4, 3, 2, 1, 5, 6}
We strongly recommend you to minimize your browser and try this yourself first.
Below is the implementation for the same.
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; } |
chevron_right
filter_none
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; } } // Drive 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. |
chevron_right
filter_none
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. |
chevron_right
filter_none
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 |
chevron_right
filter_none
Output:
4 3 2 1 5 6
Time complexity: O(k)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Maximize array elements upto given number
- Sort an array where a subarray of a sorted array is in reverse order
- Find the position of the given row in a 2-D array
- First string from the given array whose reverse is also present in the same array
- Find the position of the last removed element from the array
- Sort an array without changing position of negative numbers
- How to reverse an Array using STL in C++?
- Perl | Reverse an array
- All reverse permutations of an array using STL in C++
- Sorting array with reverse around middle
- Program to reverse an array using pointers
- C Program to Reverse Array of Strings
- Reverse an array in groups of given size
- Reverse an array in groups of given size | Set 2 (Variations of Set 1 )
- Reverse an array without affecting special characters
Improved By : Sam007



