给定一个由N个整数组成的数组arr [] ,任务是交换第一个和最后一个元素,然后交换第三个和最后一个元素,然后交换第五个和第五个最后一个,依此类推。在所有有效操作之后打印最终数组。
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: 6 2 4 3 5 1
Operation 1: Swap 1 and 6
Operation 2: Swap 3 and 4
Input: arr[] = {5, 54, 12, 63, 45}
Output: 45 54 12 63 5
方法:初始化指针i = 0和j = N – 1,然后在这些指针处交换元素并更新i = i + 2和j = j – 2 。当我
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Utility function to print
// the contents of an array
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
// Function to update the array
void UpdateArr(int arr[], int n)
{
// Initialize the pointers
int i = 0, j = n - 1;
// While there are elements to swap
while (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
// Update the pointers
i += 2;
j -= 2;
}
// Print the updated array
printArr(arr, n);
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
UpdateArr(arr, n);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Utility function to print
// the contents of an array
static void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
// Function to update the array
static void UpdateArr(int arr[], int n)
{
// Initialize the pointers
int i = 0, j = n - 1;
// While there are elements to swap
while (i < j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
// Update the pointers
i += 2;
j -= 2;
}
// Print the updated array
printArr(arr, n);
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = arr.length;
UpdateArr(arr, n);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
# Utility function to print
# the contents of an array
def printArr(arr, n):
for i in range(n):
print(arr[i], end = " ");
# Function to update the array
def UpdateArr(arr, n):
# Initialize the pointers
i = 0;
j = n - 1;
# While there are elements to swap
while (i < j):
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
# Update the pointers
i += 2;
j -= 2;
# Print the updated array
printArr(arr, n);
# Driver code
if __name__ == '__main__':
arr = [1, 2, 3, 4, 5, 6 ];
n = len(arr);
UpdateArr(arr, n);
# This code is contributed by PrinciRaj1992
C#
// C# implementation of the approach
using System;
class GFG
{
// Utility function to print
// the contents of an array
static void printArr(int []arr, int n)
{
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
// Function to update the array
static void UpdateArr(int []arr, int n)
{
// Initialize the pointers
int i = 0, j = n - 1;
// While there are elements to swap
while (i < j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
// Update the pointers
i += 2;
j -= 2;
}
// Print the updated array
printArr(arr, n);
}
// Driver code
public static void Main (String[] args)
{
int []arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.Length;
UpdateArr(arr, n);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
6 2 4 3 5 1