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++
#include <bits/stdc++.h>
using namespace std;
void reverse( int a[], int n, int k)
{
if (k > n)
{
cout << "Invalid k" ;
return ;
}
for ( int i = 0; i < k/2; i++)
swap(a[i], a[k-i-1]);
}
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
public class GFG {
static void reverse( int []a, int n, int k)
{
if (k > n)
{
System.out.println( "Invalid k" );
return ;
}
for ( int i = 0 ; i < k / 2 ; i++)
{
int tempswap = a[i];
a[i] = a[k - i - 1 ];
a[k - i - 1 ] = tempswap;
}
}
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] + " " );
}
}
|
Python3
from __future__ import print_function
def reverse(a, n, k):
if (k > n):
print ( "Invalid k" )
return
for i in range ( 0 , ( int )(k / 2 )):
temp = a[i]
a[i] = a[k - i - 1 ]
a[k - i - 1 ] = temp
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 = " " )
|
C#
using System;
class GFG {
static void SwapNum( ref int x, ref int y)
{
int tempswap = x;
x = y;
y = tempswap;
}
static void reverse( int []a, int n,
int k)
{
if (k > n)
{
Console.Write( "Invalid k" );
return ;
}
for ( int i = 0; i < k / 2; i++)
SwapNum( ref a[i], ref a[k - i - 1]);
}
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] + " " );
}
}
|
Javascript
<script>
function reverse( a, n, k)
{
if (k > n)
{
document.write( "Invalid k" );
return ;
}
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 ;
}
}
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>
|
Time complexity: O(k)
Auxiliary Space: O(1) ,since extra space is used.
Method 2 (using STL):
In this method we will use an in-built C++ STL function named reverse. This function completes the task of reversing K elements of array in O(K) time and also doesn’t use extra space.
implementation of this method is below.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int k = 4;
reverse(arr, arr + k);
for ( int i = 0; i < 8; i++) {
cout << arr[i] << " " ;
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.util.Arrays;
class GFG {
public static void main (String[] args) {
Integer[] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
int k = 4 ;
Integer[] arr1 = Arrays.copyOfRange(arr, 0 , k);
Collections.reverse(Arrays.asList(arr1));
System.arraycopy(arr1, 0 , arr, 0 , k);
for ( int i = 0 ; i < 8 ; i++) {
System.out.print(arr[i] + " " );
}
}
}
|
Python3
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]
k = 4
arr[:k] = arr[:k][:: - 1 ]
print ( * arr)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 4, 5, 6, 7, 8 };
int k = 4;
Array.Reverse(arr, 0, k);
for ( int i = 0; i < 8; i++) {
Console.Write(arr[i] + " " );
}
}
}
|
Javascript
let arr = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
let k = 4;
let arr1 = arr.slice(0, k);
arr1.reverse();
arr.splice(0, k, ...arr1);
for ( var i = 0; i < 8; i++)
process.stdout.write(arr[i] + " " );
|
Time Complexity :O(K) , as complexity of reverse() function is O(number of elements of array to be sorted).
Auxiliary Space :O(1), as no extra space used.
Last Updated :
13 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...