给定大小为N的数组arr [] ,任务是将以下操作恰好执行N次,
创建一个整数b []的空列表,然后在第i个操作中,
- 将arr [i]附加到b []的末尾。
- 反转b []中的元素。
最后,在所有操作结束后打印列表b []的内容。
例子:
Input: arr[] = {1, 2, 3, 4}
Output: 4 2 1 3
Input: arr[] = {1, 2, 3}
Output: 3 1 2
方法:我们需要一些观察来解决此问题。假设数组中的元素数为偶数。假设我们的数组是{4,8,6,1,7,9}。
Operation | b[] |
---|---|
1 | {1} |
2 | {2, 1} |
3 | {3, 1, 2} |
4 | {4, 2, 1, 3} |
仔细观察后,我们得出结论,对于数组中元素的偶数大小,偶数位置(基于索引1)的数字被颠倒并在开头加,奇数位置的数字保持相同的顺序并相加到底。
而对于奇数大小的数组,位于奇数位置的元素将颠倒并在开头添加,而位于偶数位置的数组中的元素将保持相同并在结尾添加。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that generates the
// array b[] when n is even
void solveEven(int n, int* arr, int* b)
{
int left = n - 1;
// Fill the first half of the final array
// with reversed sequence
for (int i = 0; i < (n / 2); ++i) {
b[i] = arr[left];
left = left - 2;
if (left < 0)
break;
}
// Fill the second half
int right = 0;
for (int i = n / 2; i <= n - 1; ++i) {
b[i] = arr[right];
right = right + 2;
if (right > n - 2)
break;
}
}
// Function that generates the
// array b[] when n is odd
void solveOdd(int n, int* arr, int* b)
{
// Fill the first half of the final array
// with reversed sequence
int left = n - 1;
for (int i = 0; i < (n / 2) + 1; ++i) {
b[i] = arr[left];
left = left - 2;
if (left < 0)
break;
}
// Fill the second half
int right = 1;
for (int i = (n / 2) + 1; i <= n - 1; ++i) {
b[i] = arr[right];
right = right + 2;
if (right > n - 2)
break;
}
}
// Function to find the final array b[]
// after n operations of given type
void solve(int n, int* arr)
{
// Create the array b
int b[n];
// If the array size is even
if (n % 2 == 0)
solveEven(n, arr, b);
else
solveOdd(n, arr, b);
// Print the final array elements
for (int i = 0; i <= n - 1; ++i) {
cout << b[i] << " ";
}
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
solve(n, arr);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
class GFG
{
// Function that generates the
// array b[] when n is even
static void solveEven(int n, int arr[], int b[])
{
int left = n - 1;
// Fill the first half of the final array
// with reversed sequence
for (int i = 0; i < (n / 2); ++i)
{
b[i] = arr[left];
left = left - 2;
if (left < 0)
break;
}
// Fill the second half
int right = 0;
for (int i = n / 2; i <= n - 1; ++i)
{
b[i] = arr[right];
right = right + 2;
if (right > n - 2)
break;
}
}
// Function that generates the
// array b[] when n is odd
static void solveOdd(int n, int arr[], int b[])
{
// Fill the first half of the final array
// with reversed sequence
int left = n - 1;
for (int i = 0; i < (n / 2) + 1; ++i)
{
b[i] = arr[left];
left = left - 2;
if (left < 0)
break;
}
// Fill the second half
int right = 1;
for (int i = (n / 2) + 1; i <= n - 1; ++i)
{
b[i] = arr[right];
right = right + 2;
if (right > n - 2)
break;
}
}
// Function to find the final array b[]
// after n operations of given type
static void solve(int n, int arr[])
{
// Create the array b
int b[] = new int[n];
// If the array size is even
if (n % 2 == 0)
solveEven(n, arr, b);
else
solveOdd(n, arr, b);
// Print the final array elements
for (int i = 0; i <= n - 1; ++i)
{
System.out.print( b[i] + " ");
}
}
// Driver code
public static void main (String[] args)
{
int []arr = { 1, 2, 3, 4 };
int n = arr.length;
solve(n, arr);
}
}
// This code is contributed by anuj_67..
Python3
# Python 3 implementation of the approach
# Function that generates the
# array b[] when n is even
def solveEven(n, arr, b):
left = n - 1
# Fill the first half of the final array
# with reversed sequence
for i in range((n // 2)):
b[i] = arr[left]
left = left - 2
if (left < 0):
break
# Fill the second half
right = 0
for i in range(n // 2, n, 1):
b[i] = arr[right]
right = right + 2
if (right > n - 2):
break
# Function that generates the
# array b[] when n is odd
def solveOdd(n, arr, b):
# Fill the first half of the final array
# with reversed sequence
left = n - 1
for i in range(n // 2 + 1):
b[i] = arr[left]
left = left - 2
if (left < 0):
break
# Fill the second half
right = 1
for i in range(n // 2 + 1, n, 1):
b[i] = arr[right]
right = right + 2
if (right > n - 2):
break
# Function to find the final array b[]
# after n operations of given type
def solve(n, arr):
# Create the array b
b = [0 for i in range(n)]
# If the array size is even
if (n % 2 == 0):
solveEven(n, arr, b)
else:
solveOdd(n, arr, b)
# Print the final array elements
for i in range(n):
print(b[i], end = " ")
# Driver code
if __name__ == '__main__':
arr = [1, 2, 3, 4]
n = len(arr)
solve(n, arr)
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function that generates the
// array b[] when n is even
static void solveEven(int n, int []arr,
int []b)
{
int left = n - 1;
// Fill the first half of the final array
// with reversed sequence
for (int i = 0; i < (n / 2); ++i)
{
b[i] = arr[left];
left = left - 2;
if (left < 0)
break;
}
// Fill the second half
int right = 0;
for (int i = n / 2; i <= n - 1; ++i)
{
b[i] = arr[right];
right = right + 2;
if (right > n - 2)
break;
}
}
// Function that generates the
// array b[] when n is odd
static void solveOdd(int n, int []arr, int []b)
{
// Fill the first half of the final array
// with reversed sequence
int left = n - 1;
for (int i = 0; i < (n / 2) + 1; ++i)
{
b[i] = arr[left];
left = left - 2;
if (left < 0)
break;
}
// Fill the second half
int right = 1;
for (int i = (n / 2) + 1; i <= n - 1; ++i)
{
b[i] = arr[right];
right = right + 2;
if (right > n - 2)
break;
}
}
// Function to find the final array b[]
// after n operations of given type
static void solve(int n, int []arr)
{
// Create the array b
int []b = new int[n];
// If the array size is even
if (n % 2 == 0)
solveEven(n, arr, b);
else
solveOdd(n, arr, b);
// Print the final array elements
for (int i = 0; i <= n - 1; ++i)
{
Console.Write( b[i] + " ");
}
}
// Driver code
public static void Main ()
{
int []arr = { 1, 2, 3, 4 };
int n = arr.Length;
solve(n, arr);
}
}
// This code is contributed by anuj_67..
CPP
// C++ implementation of the approach
#include
using namespace std;
int* solve(int arr[], int n)
{
static int b[4];
int p = 0;
for (int i = n - 1; i >= 0; i--)
{
b[p] = arr[i--];
if (i >= 0)
b[n - 1 - p] = arr[i];
p++;
}
return b;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4 };
int n = sizeof(arr)/sizeof(arr[0]);
int *b ;
b = solve(arr, n);
for(int i = 0; i < n; i++)
cout << b[i] << " ";
}
// This code is contributed by Rajput-Ji
Java
// Java implementation of the approach
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
static int[] solve(int[] arr, int n) {
int[] b = new int[n];
int p = 0;
for (int i = n - 1; i >= 0; i--) {
b[p] = arr[i--];
if (i >= 0)
b[n - 1 - p] = arr[i];
p++;
}
return b;
}
public static void main(String[] args)
{
int []arr = { 1, 2, 3, 4 };
int n = arr.length;
int[] b = solve(arr, n);
System.out.println(Arrays.toString(b));
}
}
// This code is contributed by Pramod Hosahalli
C#
// C# implementation of the approach
using System;
class GFG
{
static int[] solve(int[] arr, int n)
{
int[] b = new int[n];
int p = 0;
for (int i = n - 1; i >= 0; i--)
{
b[p] = arr[i--];
if (i >= 0)
b[n - 1 - p] = arr[i];
p++;
}
return b;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4 };
int n = arr.Length;
int[] b = solve(arr, n);
Console.WriteLine("[" + String.Join(",", b) + "]");
}
}
// This code is contributed by Princi Singh
Python3
# Python3 implementation of the approach
def solve(arr, n):
b = [0 for i in range(n)]
p = 0
i = n - 1
while i >= 0:
b[p] = arr[i]
i -= 1
if (i >= 0):
b[n - 1 - p] = arr[i]
p += 1
i -= 1
return b
# Driver Code
arr = [1, 2, 3, 4]
n = len(arr)
b = solve(arr, n)
print(b)
# This code is contributed by Mohit kumar
输出:
4 2 1 3
高效方法:
数组的最后一个元素将只反转一次。最后,但一个元素将反转两次。因此,它将到达最终结果数组中的最后位置,即b。因此,我们可以通过从末尾迭代原始数组并将元素放置在未填充的第一个索引和未填充的最后一个索引来填充b数组。相同的想法在下面实现。
下面是上述方法的实现:
CPP
// C++ implementation of the approach
#include
using namespace std;
int* solve(int arr[], int n)
{
static int b[4];
int p = 0;
for (int i = n - 1; i >= 0; i--)
{
b[p] = arr[i--];
if (i >= 0)
b[n - 1 - p] = arr[i];
p++;
}
return b;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4 };
int n = sizeof(arr)/sizeof(arr[0]);
int *b ;
b = solve(arr, n);
for(int i = 0; i < n; i++)
cout << b[i] << " ";
}
// This code is contributed by Rajput-Ji
Java
// Java implementation of the approach
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
static int[] solve(int[] arr, int n) {
int[] b = new int[n];
int p = 0;
for (int i = n - 1; i >= 0; i--) {
b[p] = arr[i--];
if (i >= 0)
b[n - 1 - p] = arr[i];
p++;
}
return b;
}
public static void main(String[] args)
{
int []arr = { 1, 2, 3, 4 };
int n = arr.length;
int[] b = solve(arr, n);
System.out.println(Arrays.toString(b));
}
}
// This code is contributed by Pramod Hosahalli
C#
// C# implementation of the approach
using System;
class GFG
{
static int[] solve(int[] arr, int n)
{
int[] b = new int[n];
int p = 0;
for (int i = n - 1; i >= 0; i--)
{
b[p] = arr[i--];
if (i >= 0)
b[n - 1 - p] = arr[i];
p++;
}
return b;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4 };
int n = arr.Length;
int[] b = solve(arr, n);
Console.WriteLine("[" + String.Join(",", b) + "]");
}
}
// This code is contributed by Princi Singh
Python3
# Python3 implementation of the approach
def solve(arr, n):
b = [0 for i in range(n)]
p = 0
i = n - 1
while i >= 0:
b[p] = arr[i]
i -= 1
if (i >= 0):
b[n - 1 - p] = arr[i]
p += 1
i -= 1
return b
# Driver Code
arr = [1, 2, 3, 4]
n = len(arr)
b = solve(arr, n)
print(b)
# This code is contributed by Mohit kumar
输出:
[4, 2, 1, 3]