打印通过在每个索引后反转给定 Array 形成的 Array
给定一个数组arr[],任务是打印通过在打印每个元素后翻转整个数组从第一个索引到最后一个索引遍历给定数组形成的数组。
例子:
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
方法:给定的问题可以通过使用两指针技术来解决。这个想法是从第一个索引开始从左到右迭代数组,从倒数第二个索引开始从右到左迭代数组。
可以按照以下步骤解决问题:
- 使用指针一从左到右遍历数组,使用指针二从右到左遍历数组
- 同时打印两个指针指向的元素,并将指针递增 2 并将指针递减 2
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Function to print array elements at
// every index by flipping whole array
// after printing every element
void printFlip(vector 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 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
0 3 2 1 4
时间复杂度: O(N)
辅助空间: O(1)