给定大小为N的圆形数组arr []和索引K ,任务是从索引K开始反转圆形数组的所有元素。
例子:
Input: arr[] = {3, 5, 2, 4, 1}, K = 2
Output: 4 2 5 3 1
Explanation:
After reversing the elements of the array from index K to K – 1, the modified arr[] is {4, 1, 2, 5, 3}.
Input: arr[] = {1, 2, 3, 4, 5}, K = 4
Output: 3 2 1 5 4
Explanation:
After reversing the elements of the array from index K to K – 1, the modified arr[] is {3, 2, 1, 5, 4}.
方法:要解决给定的问题,其想法是使用两个指针方法。请按照以下步骤解决问题:
- 初始化三个变量开始为K和端作为(K – 1),来跟踪边界使用两个指针的办法,并计为N / 2。
- 迭代直到count的值为正,然后执行以下步骤:
- 交换元素arr [start%N]和arr [end%N] 。
- 增量从1开始,递减从1结束。如果end等于-1 ,则将end更新为(N – 1) 。
- 递减计数1。
- 完成上述步骤后,打印完成上述步骤后获得的更新数组。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the array arr[]
void printArray(int arr[], int N)
{
// Print the array
for (int i = 0; i < N; i++)
{
cout << arr[i] << " ";
}
}
// Function to reverse elements of given
// circular array starting from index k
void reverseCircularArray(int arr[],
int N, int K)
{
// Initialize two variables as
// start = k and end = k-1
int start = K, end = K - 1;
// Initialize count = N/2
int count = N / 2;
// Loop while count > 0
while (count--) {
// Swap the elements at index
// (start%N) and (end%N)
int temp = arr[start % N];
arr[start % N] = arr[end % N];
arr[end % N] = temp;
// Update the start and end
start++;
end--;
// If end equals to -1
// set end = N-1
if (end == -1) {
end = N - 1;
}
}
// Print the circular array
printArray(arr, N);
}
// Driver Code
int main()
{
int arr[] = { 3, 5, 2, 4, 1 };
int K = 2;
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
reverseCircularArray(arr, N, K);
return 0;
}
Java
// Java program for the above approach
class GFG {
// Function to print the array arr[]
static void printArray(int arr[], int N)
{
// Print the array
for (int i = 0; i < N; i++)
{
System.out.print(arr[i] + " ");
}
}
// Function to reverse elements of given
// circular array starting from index k
static void reverseCircularArray(int arr[],
int N, int K)
{
// Initialize two variables as
// start = k and end = k-1
int start = K, end = K - 1;
// Initialize count = N/2
int count = N / 2;
// Loop while count > 0
while (count != 0)
{
// Swap the elements at index
// (start%N) and (end%N)
int temp = arr[start % N];
arr[start % N] = arr[end % N];
arr[end % N] = temp;
// Update the start and end
start++;
end--;
// If end equals to -1
// set end = N-1
if (end == -1)
{
end = N - 1;
}
count -= 1;
}
// Print the circular array
printArray(arr, N);
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 3, 5, 2, 4, 1 };
int K = 2;
int N = arr.length;
// Function Call
reverseCircularArray(arr, N, K);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to prthe array arr[]
def printArray(arr, N):
# Print the array
for i in range(N):
print(arr[i], end = " ")
# Function to reverse elements of given
# circular array starting from index k
def reverseCircularArray(arr, N, K):
# Initialize two variables as
# start = k and end = k-1
start, end = K, K - 1
# Initialize count = N/2
count = N // 2
# Loop while count > 0
while (count):
# Swap the elements at index
# (start%N) and (end%N)
temp = arr[start % N]
arr[start % N] = arr[end % N]
arr[end % N] = temp
# Update the start and end
start += 1
end -= 1
# If end equals to -1
# set end = N-1
if (end == -1):
end = N - 1
count -= 1
# Print the circular array
printArray(arr, N)
# Driver Code
if __name__ == '__main__':
arr = [ 3, 5, 2, 4, 1 ]
K = 2
N = len(arr)
# Function Call
reverseCircularArray(arr, N, K)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG
{
// Function to print the array []arr
static void printArray(int []arr, int N)
{
// Print the array
for (int i = 0; i < N; i++)
{
Console.Write(arr[i] + " ");
}
}
// Function to reverse elements of given
// circular array starting from index k
static void reverseCircularArray(int []arr,
int N, int K)
{
// Initialize two variables as
// start = k and end = k-1
int start = K, end = K - 1;
// Initialize count = N/2
int count = N / 2;
// Loop while count > 0
while (count != 0)
{
// Swap the elements at index
// (start%N) and (end%N)
int temp = arr[start % N];
arr[start % N] = arr[end % N];
arr[end % N] = temp;
// Update the start and end
start++;
end--;
// If end equals to -1
// set end = N-1
if (end == -1)
{
end = N - 1;
}
count -= 1;
}
// Print the circular array
printArray(arr, N);
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 3, 5, 2, 4, 1 };
int K = 2;
int N = arr.Length;
// Function Call
reverseCircularArray(arr, N, K);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
4 2 5 3 1
时间复杂度: O(N)
辅助空间: O(1)