Print the Array formed by reversing the given Array after each index
Last Updated :
30 Nov, 2021
Given an array arr[], the task is to print the array formed by traversing given array from first to the last index by flipping the whole array after printing every element.
Example:
Input: arr = {0, 1, 2, 3, 4, 5}
Output: 0 4 2 2 4 0
Explanation: On 1st iteration element on index 0 -> 0 is printed then the whole array is flipped: {0, 1, 2, 3, 4, 5} -> {5, 4, 3, 2, 1, 0}
On 2nd iteration element on index 1 -> 4 is printed then the whole array is flipped: : {5, 4, 3, 2, 1, 0} -> {0, 1, 2, 3, 4, 5}
On 3rd iteration element on index 2 -> 2 is printed then the whole array is flipped: {0, 1, 2, 3, 4, 5} -> {5, 4, 3, 2, 1, 0}
On 2nd iteration element on index 3 -> 2 is printed then the whole array is flipped: : {5, 4, 3, 2, 1, 0} -> {0, 1, 2, 3, 4, 5}
On 2nd iteration element on index 4 -> 4 is printed then the whole array is flipped: {0, 1, 2, 3, 4, 5} -> {5, 4, 3, 2, 1, 0}
On 2nd iteration element on index 5 -> 0 is printed then the whole array is flipped: : {5, 4, 3, 2, 1, 0} -> {0, 1, 2, 3, 4, 5}
Input: arr = {0, 1, 2, 3, 4}
Output: 0 3 2 1 4
Approach: The given problem can be solved by using the two-pointer technique. The idea is to iterate the array from left to right starting from the first index, and from right to left starting from the second last index.
Below steps can be followed to solve the problem:
- Use pointer one to iterate the array from left to right, and use pointer two to traverse the array from right to left
- Print the elements pointed by both the pointers simultaneously and increment pointer one by 2 and decrement pointer two by 2
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printFlip(vector< int > arr)
{
int N = arr.size();
int p1 = 0, p2 = N - 2;
while (p1 < N || p2 >= 0) {
cout << arr[p1] << " " ;
if (p2 > 0)
cout << arr[p2] << " " ;
p1 += 2;
p2 -= 2;
}
}
int main()
{
vector< int > arr = { 0, 1, 2, 3, 4 };
printFlip(arr);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void printFlip( int [] arr)
{
int N = arr.length;
int p1 = 0 , p2 = N - 2 ;
while (p1 < N || p2 >= 0 ) {
System.out.print(arr[p1] + " " );
if (p2 > 0 )
System.out.print(arr[p2] + " " );
p1 += 2 ;
p2 -= 2 ;
}
}
public static void main(String[] args)
{
int [] arr = { 0 , 1 , 2 , 3 , 4 };
printFlip(arr);
}
}
|
Python3
def printFlip(arr):
N = len (arr);
p1 = 0
p2 = N - 2 ;
while (p1 < N or p2 > = 0 ):
print (arr[p1], end = " " );
if (p2 > 0 ):
print (arr[p2], end = " " );
p1 + = 2 ;
p2 - = 2 ;
arr = [ 0 , 1 , 2 , 3 , 4 ];
printFlip(arr);
|
C#
using System;
class GFG {
static void printFlip( int []arr)
{
int N = arr.Length;
int p1 = 0, p2 = N - 2;
while (p1 < N || p2 >= 0) {
Console.Write(arr[p1] + " " );
if (p2 > 0)
Console.Write(arr[p2] + " " );
p1 += 2;
p2 -= 2;
}
}
public static void Main()
{
int []arr = { 0, 1, 2, 3, 4 };
printFlip(arr);
}
}
|
Javascript
<script>
function printFlip(arr)
{
let N = arr.length;
let p1 = 0, p2 = N - 2;
while (p1 < N || p2 >= 0) {
document.write(arr[p1] + " " );
if (p2 > 0)
document.write(arr[p2] + " " );
p1 += 2;
p2 -= 2;
}
}
let arr = [ 0, 1, 2, 3, 4 ];
printFlip(arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...