Open In App

Print the Array formed by reversing the given Array after each index

Last Updated : 30 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print array elements at
// every index by flipping whole array
// after printing every element
void printFlip(vector<int> arr)
{
 
    // Initialize length of the array
    int N = arr.size();
 
    // Initialize both the pointers
    int p1 = 0, p2 = N - 2;
 
    // Iterate until both pointers
    // are not out of bounds
    while (p1 < N || p2 >= 0) {
 
        // Print the elements
        cout << arr[p1] << " ";
        if (p2 > 0)
            cout << arr[p2] << " ";
 
        // Increment p1 by 2
        p1 += 2;
 
        // Decrement p2 by 2
        p2 -= 2;
    }
}
 
// Driver code
int main()
{
 
    // Initialize the array
    vector<int> arr = { 0, 1, 2, 3, 4 };
 
    // Call the function
    // and print the array
    printFlip(arr);
    return 0;
}
 
// This code is contributed by Potta Lokesh.


Java




// Java implementation for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to print array elements at
    // every index by flipping whole array
    // after printing every element
    public static void printFlip(int[] arr)
    {
 
        // Initialize length of the array
        int N = arr.length;
 
        // Initialize both the pointers
        int p1 = 0, p2 = N - 2;
 
        // Iterate until both pointers
        // are not out of bounds
        while (p1 < N || p2 >= 0) {
 
            // Print the elements
            System.out.print(arr[p1] + " ");
            if (p2 > 0)
                System.out.print(arr[p2] + " ");
 
            // Increment p1 by 2
            p1 += 2;
 
            // Decrement p2 by 2
            p2 -= 2;
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Initialize the array
        int[] arr = { 0, 1, 2, 3, 4 };
 
        // Call the function
        // and print the array
        printFlip(arr);
    }
}


Python3




# Python code for the above approach
 
# Function to print array elements at
# every index by flipping whole array
# after printing every element
def printFlip(arr):
 
    # Initialize length of the array
    N = len(arr);
 
    # Initialize both the pointers
    p1 = 0
    p2 = N - 2;
 
    # Iterate until both pointers
    # are not out of bounds
    while (p1 < N or p2 >= 0):
 
        # Print the elements
        print(arr[p1], end=" ");
        if (p2 > 0):
            print(arr[p2], end=" ");
 
        # Increment p1 by 2
        p1 += 2;
 
        # Decrement p2 by 2
        p2 -= 2;
     
# Driver Code
# Initialize the array
arr = [ 0, 1, 2, 3, 4 ];
 
# Call the function
# and print the array
printFlip(arr);
 
# This code is contributed by gfgking.


C#




// C# implementation for the above approach
using System;
 
class GFG {
 
    // Function to print array elements at
    // every index by flipping whole array
    // after printing every element
    static void printFlip(int []arr)
    {
 
        // Initialize length of the array
        int N = arr.Length;
 
        // Initialize both the pointers
        int p1 = 0, p2 = N - 2;
 
        // Iterate until both pointers
        // are not out of bounds
        while (p1 < N || p2 >= 0) {
 
            // Print the elements
            Console.Write(arr[p1] + " ");
            if (p2 > 0)
                Console.Write(arr[p2] + " ");
 
            // Increment p1 by 2
            p1 += 2;
 
            // Decrement p2 by 2
            p2 -= 2;
        }
    }
 
    // Driver code
    public static void Main()
    {
 
        // Initialize the array
        int []arr = { 0, 1, 2, 3, 4 };
 
        // Call the function
        // and print the array
        printFlip(arr);
    }
}
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
// Javascript code for the above approach
 
// Function to print array elements at
// every index by flipping whole array
// after printing every element
function printFlip(arr)
{
 
    // Initialize length of the array
    let N = arr.length;
 
    // Initialize both the pointers
    let p1 = 0, p2 = N - 2;
 
    // Iterate until both pointers
    // are not out of bounds
    while (p1 < N || p2 >= 0) {
 
        // Print the elements
        document.write(arr[p1] + " ");
        if (p2 > 0)
            document.write(arr[p2] + " ");
 
        // Increment p1 by 2
        p1 += 2;
 
        // Decrement p2 by 2
        p2 -= 2;
    }
}
 
// Driver Code
// Initialize the array
let arr = [ 0, 1, 2, 3, 4 ];
 
// Call the function
// and print the array
printFlip(arr);
 
// This code is contributed by Samim Hossain Mondal.
</script>


 
 

Output

0 3 2 1 4 

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads