通过分成两半并在交替位置反向插入后半部分来重新排列给定的数组
给定一个偶数长度N的数组arr[] ,任务是对给定数组执行以下操作:
- 将给定的数组分成两半。
- 从一开始在交替位置以相反的顺序插入下半部分。
例子:
Input: N = 6, arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 6 2 5 3 4
Explanation: The first element is 1.
Then print the last element 6
Then the 2nd element 2 followed by the 2nd last element (i.e. the 5th element) 5.
Now print the 3rd element which is 3 and the 3rd last (i.e. the 4th element) 4.
Input: N = 4, arr[] = {12, 34, 11, 20}
Output: 12 20 34 11
方法:这个问题是通过在两个指针i从0开始和j从(N-1)开始的帮助下将数组分成两部分来解决的。现在交替打印i 和 j处的元素,并在新数组中插入第 i / j位置的元素时递增 i / 递减 j 。
下面是上述方法的实现。
C++
// C++ Program to implement the approach
#include
using namespace std;
// Function to arrange array in alternate order
vector printAlternate(int arr[], int N)
{
int i = 0, j = N - 1;
vector ans;
// Run loop while i <= j
while (i <= j) {
// Push the element from starting
ans.push_back(arr[i]);
// Push the element from back
ans.push_back(arr[j]);
// Increment i by 1
i = i + 1;
// Decrement j by 1
j = j - 1;
}
return ans;
}
// Driver code
int main()
{
int N = 6;
int arr[N] = { 1, 2, 3, 4, 5, 6 };
// Function call
vector ans = printAlternate(arr, N);
for (int x : ans)
cout << x << " ";
return 0;
}
Java
// Java Program to implement the approach
import java.util.*;
class GFG {
// Function to arrange array in alternate order
public static ArrayList
printAlternate(int[] arr, int N)
{
int i = 0, j = N - 1;
ArrayList ans = new ArrayList<>();
// Run loop while i <= j
while (i <= j) {
// Push the element from starting
ans.add(arr[i]);
// Push the element from back
ans.add(arr[j]);
// Increment i by 1
i = i + 1;
// Decrement j by 1
j = j - 1;
}
return ans;
}
public static void main(String[] args)
{
int N = 6;
int arr[] = { 1, 2, 3, 4, 5, 6 };
// Function call
ArrayList ans = printAlternate(arr, N);
for (int x : ans)
System.out.print(x + " ");
}
}
// This code is contributed by ninja_hattori.
Python3
# Python Program to implement the approach
# Function to arrange array in alternate order
def printAlternate(arr, N):
i = 0
j = N - 1
ans = []
# Run loop while i <= j
while (i <= j):
# Push the element from starting
ans.append(arr[i])
# Push the element from back
ans.append(arr[j])
# Increment i by 1
i = i + 1
# Decrement j by 1
j = j - 1
return ans
# Driver code
N = 6
arr = [1, 2, 3, 4, 5, 6]
# Function call
ans = printAlternate(arr, N)
for x in ans:
print(x, end=' ')
# This code is contributed by Palak Gupta
C#
// C# Program to implement the approach
using System;
using System.Collections;
class GFG {
// Function to arrange array in alternate order
static ArrayList printAlternate(int[] arr, int N)
{
int i = 0, j = N - 1;
ArrayList ans = new ArrayList();
// Run loop while i <= j
while (i <= j) {
// Push the element from starting
ans.Add(arr[i]);
// Push the element from back
ans.Add(arr[j]);
// Increment i by 1
i = i + 1;
// Decrement j by 1
j = j - 1;
}
return ans;
}
// Driver code
public static void Main()
{
int N = 6;
int[] arr = { 1, 2, 3, 4, 5, 6 };
// Function call
ArrayList ans = printAlternate(arr, N);
foreach(int x in ans) Console.Write(x + " ");
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
1 6 2 5 3 4
时间复杂度: O(N)
辅助空间: O(N)