在给定大小的组中反转数组 |第 2 组(第 1 组的变体)
给定一个数组,反转每个满足给定约束的子数组。
我们已经讨论了一个解决方案,其中我们反转由 Set 1 中连续 k 个元素形成的每个子数组。在这个集合中,我们将讨论这个问题的各种有趣的变体。变体 1(反向交替组):
反转由连续 k 个元素形成的每个交替子数组。
例子:
Input:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
k = 3
Output:
[3, 2, 1, 4, 5, 6, 9, 8, 7]
Input:
arr = [1, 2, 3, 4, 5, 6, 7, 8]
k = 2
Output:
[2, 1, 3, 4, 6, 5, 7, 8]
下面是实现——
C++
// C++ program to reverse every alternate sub-array
// formed by consecutive k elements
#include
using namespace std;
// Function to reverse every alternate sub-array
// formed by consecutive k elements
void reverse(int arr[], int n, int k)
{
// increment i in multiples of 2*k
for (int i = 0; i < n; i += 2*k)
{
int left = i;
// to handle case when 2*k is not multiple of n
int right = min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
swap(arr[left++], arr[right--]);
}
}
// Driver code
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
int k = 3;
int n = sizeof(arr) / sizeof(arr[0]);
reverse(arr, n, k);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Java
// Java program to reverse
// every alternate sub-array
// formed by consecutive k elements
class GFG
{
// Function to reverse every
// alternate sub-array formed
// by consecutive k elements
static void reverse(int arr[], int n, int k)
{
// increment i in multiples of 2*k
for (int i = 0; i < n; i += 2 * k)
{
int left = i;
// to handle case when 2*k is not multiple of n
int right = Math.min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right) {
swap(arr, left++, right--);
}
}
}
static int[] swap(int[] array, int i, int j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
return array;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14};
int k = 3;
int n = arr.length;
reverse(arr, n, k);
for (int i = 0; i < n; i++)
{
System.out.print(arr[i] + " ");
}
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python3 program to reverse every alternate sub-array
# formed by consecutive k elements
# Function to reverse every alternate sub-array
# formed by consecutive k elements
def reverse(arr, n, k):
# increment i in multiples of 2*k
for i in range(0,n,2*k):
left = i
# to handle case when 2*k is not multiple of n
right = min(i + k - 1, n - 1)
# reverse the sub-array [left, right]
while (left < right):
temp = arr[left]
arr[left] = arr[right]
arr[right] = temp
left += 1
right -= 1
# Driver code
if __name__ == '__main__':
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
k = 3
n = len(arr)
reverse(arr, n, k)
for i in range(0,n,1):
print(arr[i],end =" ")
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to reverse every alternate
// sub-array formed by consecutive k elements
using System;
class GFG
{
// Function to reverse every
// alternate sub-array formed
// by consecutive k elements
static void reverse(int []arr,
int n, int k)
{
// increment i in multiples of 2*k
for (int i = 0; i < n; i += 2 * k)
{
int left = i;
// to handle case when 2*k is
// not multiple of n
int right = Math.Min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
{
swap(arr, left++, right--);
}
}
}
static int[] swap(int[] array, int i, int j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
return array;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14};
int k = 3;
int n = arr.Length;
reverse(arr, n, k);
for (int i = 0; i < n; i++)
{
Console.Write(arr[i] + " ");
}
}
}
// This code is contributed by PrinciRaj1992
Javascript
C++
// C++ program to reverse every sub-array formed by
// consecutive k elements at given distance apart
#include
using namespace std;
// Function to reverse every sub-array formed by
// consecutive k elements at m distance apart
void reverse(int arr[], int n, int k, int m)
{
// increment i in multiples of k + m
for (int i = 0; i < n; i += k + m)
{
int left = i;
// to handle case when k + m is not multiple of n
int right = min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
swap(arr[left++], arr[right--]);
}
}
// Driver code
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
int k = 3;
int m = 2;
int n = sizeof(arr) / sizeof(arr[0]);
reverse(arr, n, k, m);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Java
// java program to reverse every sub-array formed by
// consecutive k elements at given distance apart
class GFG
{
// Function to reverse every sub-array formed by
// consecutive k elements at m distance apart
static void reverse(int[] arr, int n, int k, int m)
{
// increment i in multiples of k + m
for (int i = 0; i < n; i += k + m)
{
int left = i;
// to handle case when k + m is not multiple of n
int right = Math.min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
swap(arr,left++, right--);
}
}
static int[] swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14};
int k = 3;
int m = 2;
int n = arr.length;
reverse(arr, n, k, m );
for (int i = 0; i < n; i++)
{
System.out.print(arr[i] + " ");
}
}
}
// This code has been contributed by Rajput-Ji
Python3
# Python3 program to reverse every
# sub-array formed by consecutive
# k elements at given distance apart
# Function to reverse every
# sub-array formed by consecutive
# k elements at m distance apart
def reverse(arr, n, k, m):
# increment i in multiples of k + m
for i in range(0, n, k + m):
left = i;
# to handle case when k + m
# is not multiple of n
right = min(i + k - 1, n - 1);
# reverse the sub-array [left, right]
while (left < right):
arr = swap(arr,left, right);
left += 1;
right -= 1;
return arr;
def swap(arr, i, j):
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
# Driver code
arr = [1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14];
k = 3;
m = 2;
n = len(arr);
arr = reverse(arr, n, k, m );
for i in range(0, n):
print(arr[i], end = " ");
# This code is contributed by Rajput-Ji
C#
// C# program to reverse every sub-array
// formed by consecutive k elements at
// given distance apart
using System;
class GFG
{
// Function to reverse every sub-array
// formed by consecutive k elements
// at m distance apart
static void reverse(int[] arr, int n,
int k, int m)
{
// increment i in multiples of k + m
for (int i = 0; i < n; i += k + m)
{
int left = i;
// to handle case when k + m is
// not multiple of n
int right = Math.Min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
swap(arr, left++, right--);
}
}
static int[] swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14};
int k = 3;
int m = 2;
int n = arr.Length;
reverse(arr, n, k, m );
for (int i = 0; i < n; i++)
{
Console.Write(arr[i] + " ");
}
}
}
// This code is contributed by PrinciRaj1992
Javascript
C++
// C++ program to reverse every sub-array formed by
// consecutive k elements where k doubles itself with
// every sub-array.
#include
using namespace std;
// Function to reverse every sub-array formed by
// consecutive k elements where k doubles itself
// with every sub-array.
void reverse(int arr[], int n, int k)
{
// increment i in multiples of k where value
// of k is doubled with each iteration
for (int i = 0; i < n; i += k/2)
{
int left = i;
// to handle case when number of elements in
// last group is less than k
int right = min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
swap(arr[left++], arr[right--]);
// double value of k with each iteration
k = k*2;
}
}
// Driver code
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
int k = 1;
int n = sizeof(arr) / sizeof(arr[0]);
reverse(arr, n, k);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Java
// Java program to reverse every sub-array
// formed by consecutive k elements where
// k doubles itself with every sub-array.
import java.util.*;
class GFG
{
// Function to reverse every sub-array formed by
// consecutive k elements where k doubles itself
// with every sub-array.
static void reverse(int arr[], int n, int k)
{
// increment i in multiples of k where value
// of k is doubled with each iteration
for (int i = 0; i < n; i += k / 2)
{
int left = i;
// to handle case when number of elements in
// last group is less than k
int right = Math.min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
swap(arr, left++, right--);
// double value of k with each iteration
k = k * 2;
}
}
static int[] swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16};
int k = 1;
int n = arr.length;
reverse(arr, n, k);
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to reverse every
# sub-array formed by consecutive
# k elements where k doubles itself
# with every sub-array
# Function to reverse every sub-array
# formed by consecutive k elements
# where k doubles itself with every
# sub-array
def reverse(arr, n, k):
i = 0
# Increment i in multiples of k where
# value of k is doubled with each
# iteration
while (i < n):
left = i
# To handle case when number of elements
# in last group is less than k
right = min(i + k - 1, n - 1)
# Reverse the sub-array [left, right]
while (left < right):
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
# Double value of k with each iteration
k = k * 2
i += int(k / 2)
# Driver code
arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16]
k = 1
n = len(arr)
reverse(arr, n, k)
print(*arr, sep = ' ')
# This code is contributed by avanitrachhadiya2155
C#
// C# program to reverse every sub-array
// formed by consecutive k elements where
// k doubles itself with every sub-array.
using System;
class GFG
{
// Function to reverse every sub-array formed by
// consecutive k elements where k doubles itself
// with every sub-array.
static void reverse(int []arr, int n, int k)
{
// increment i in multiples of k where value
// of k is doubled with each iteration
for (int i = 0; i < n; i += k / 2)
{
int left = i;
// to handle case when number of elements in
// last group is less than k
int right = Math.Min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
swap(arr, left++, right--);
// double value of k with each iteration
k = k * 2;
}
}
static int[] swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16};
int k = 1;
int n = arr.Length;
reverse(arr, n, k);
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
3 2 1 4 5 6 9 8 7 10 11 12 14 13
变体 2(在给定距离倒车):
反转由相隔给定距离的连续 k 个元素形成的每个子阵列。
例子:
Input:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
k = 3
m = 2
Output:
[3, 2, 1, 4, 5, 8, 7, 6, 9, 10]
Input:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
k = 3
m = 1
Output:
[3, 2, 1, 4, 7, 6, 5, 8, 10, 9]
Input:
arr = [1, 2, 3, 4, 5, 6, 7, 8]
k = 2
m = 0
Output:
[2, 1, 4, 3, 6, 5, 8, 7]
下面是它的实现——
C++
// C++ program to reverse every sub-array formed by
// consecutive k elements at given distance apart
#include
using namespace std;
// Function to reverse every sub-array formed by
// consecutive k elements at m distance apart
void reverse(int arr[], int n, int k, int m)
{
// increment i in multiples of k + m
for (int i = 0; i < n; i += k + m)
{
int left = i;
// to handle case when k + m is not multiple of n
int right = min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
swap(arr[left++], arr[right--]);
}
}
// Driver code
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
int k = 3;
int m = 2;
int n = sizeof(arr) / sizeof(arr[0]);
reverse(arr, n, k, m);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Java
// java program to reverse every sub-array formed by
// consecutive k elements at given distance apart
class GFG
{
// Function to reverse every sub-array formed by
// consecutive k elements at m distance apart
static void reverse(int[] arr, int n, int k, int m)
{
// increment i in multiples of k + m
for (int i = 0; i < n; i += k + m)
{
int left = i;
// to handle case when k + m is not multiple of n
int right = Math.min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
swap(arr,left++, right--);
}
}
static int[] swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14};
int k = 3;
int m = 2;
int n = arr.length;
reverse(arr, n, k, m );
for (int i = 0; i < n; i++)
{
System.out.print(arr[i] + " ");
}
}
}
// This code has been contributed by Rajput-Ji
Python3
# Python3 program to reverse every
# sub-array formed by consecutive
# k elements at given distance apart
# Function to reverse every
# sub-array formed by consecutive
# k elements at m distance apart
def reverse(arr, n, k, m):
# increment i in multiples of k + m
for i in range(0, n, k + m):
left = i;
# to handle case when k + m
# is not multiple of n
right = min(i + k - 1, n - 1);
# reverse the sub-array [left, right]
while (left < right):
arr = swap(arr,left, right);
left += 1;
right -= 1;
return arr;
def swap(arr, i, j):
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
# Driver code
arr = [1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14];
k = 3;
m = 2;
n = len(arr);
arr = reverse(arr, n, k, m );
for i in range(0, n):
print(arr[i], end = " ");
# This code is contributed by Rajput-Ji
C#
// C# program to reverse every sub-array
// formed by consecutive k elements at
// given distance apart
using System;
class GFG
{
// Function to reverse every sub-array
// formed by consecutive k elements
// at m distance apart
static void reverse(int[] arr, int n,
int k, int m)
{
// increment i in multiples of k + m
for (int i = 0; i < n; i += k + m)
{
int left = i;
// to handle case when k + m is
// not multiple of n
int right = Math.Min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
swap(arr, left++, right--);
}
}
static int[] swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14};
int k = 3;
int m = 2;
int n = arr.Length;
reverse(arr, n, k, m );
for (int i = 0; i < n; i++)
{
Console.Write(arr[i] + " ");
}
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
3 2 1 4 5 8 7 6 9 10 13 12 11 14
变体 3(通过将组大小加倍来反转):
反转由连续 k 个元素形成的每个子数组,其中 k 与每个子数组一起翻倍。
例子:
Input:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
k = 1
Output:
[1], [3, 2], [7, 6, 5, 4], [15, 14, 13, 12, 11, 10, 9, 8]
下面是它的实现——
C++
// C++ program to reverse every sub-array formed by
// consecutive k elements where k doubles itself with
// every sub-array.
#include
using namespace std;
// Function to reverse every sub-array formed by
// consecutive k elements where k doubles itself
// with every sub-array.
void reverse(int arr[], int n, int k)
{
// increment i in multiples of k where value
// of k is doubled with each iteration
for (int i = 0; i < n; i += k/2)
{
int left = i;
// to handle case when number of elements in
// last group is less than k
int right = min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
swap(arr[left++], arr[right--]);
// double value of k with each iteration
k = k*2;
}
}
// Driver code
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
int k = 1;
int n = sizeof(arr) / sizeof(arr[0]);
reverse(arr, n, k);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Java
// Java program to reverse every sub-array
// formed by consecutive k elements where
// k doubles itself with every sub-array.
import java.util.*;
class GFG
{
// Function to reverse every sub-array formed by
// consecutive k elements where k doubles itself
// with every sub-array.
static void reverse(int arr[], int n, int k)
{
// increment i in multiples of k where value
// of k is doubled with each iteration
for (int i = 0; i < n; i += k / 2)
{
int left = i;
// to handle case when number of elements in
// last group is less than k
int right = Math.min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
swap(arr, left++, right--);
// double value of k with each iteration
k = k * 2;
}
}
static int[] swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16};
int k = 1;
int n = arr.length;
reverse(arr, n, k);
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to reverse every
# sub-array formed by consecutive
# k elements where k doubles itself
# with every sub-array
# Function to reverse every sub-array
# formed by consecutive k elements
# where k doubles itself with every
# sub-array
def reverse(arr, n, k):
i = 0
# Increment i in multiples of k where
# value of k is doubled with each
# iteration
while (i < n):
left = i
# To handle case when number of elements
# in last group is less than k
right = min(i + k - 1, n - 1)
# Reverse the sub-array [left, right]
while (left < right):
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
# Double value of k with each iteration
k = k * 2
i += int(k / 2)
# Driver code
arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16]
k = 1
n = len(arr)
reverse(arr, n, k)
print(*arr, sep = ' ')
# This code is contributed by avanitrachhadiya2155
C#
// C# program to reverse every sub-array
// formed by consecutive k elements where
// k doubles itself with every sub-array.
using System;
class GFG
{
// Function to reverse every sub-array formed by
// consecutive k elements where k doubles itself
// with every sub-array.
static void reverse(int []arr, int n, int k)
{
// increment i in multiples of k where value
// of k is doubled with each iteration
for (int i = 0; i < n; i += k / 2)
{
int left = i;
// to handle case when number of elements in
// last group is less than k
int right = Math.Min(i + k - 1, n - 1);
// reverse the sub-array [left, right]
while (left < right)
swap(arr, left++, right--);
// double value of k with each iteration
k = k * 2;
}
}
static int[] swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16};
int k = 1;
int n = arr.Length;
reverse(arr, n, k);
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
1 3 2 7 6 5 4 15 14 13 12 11 10 9 8 16
上面讨论的所有解决方案的时间复杂度都是 O(n)。
程序使用的辅助空间为 O(1)。