给定两个整数M和K,以及一个由N个正整数组成的数组arr [] ,任务是检查该数组是否可以拆分为长度为M的K个连续的不重叠子数组,以使每个子数组都由单个不同的元素组成。如果发现是真的,则打印“是” 。否则,打印“否” 。
例子:
Input: arr[] = {6, 1, 3, 3, 3, 3}, M = 1, K = 3
Output: Yes
Explanation:
The K consecutive non-overlapping subarrays are {6}, {1}, {3, 3, 3, 3}.
Input: arr[] = {3, 5, 3, 5, 3, 1}, M = 2, K = 3
Output: No
方法:可以通过使用简单的数组遍历并检查当前索引i上的元素和索引(i + M)上的元素是否相同来解决给定的问题。请按照以下步骤解决问题:
- 分别用1和0初始化两个变量count和t,以分别存储匹配模式的总数和当前匹配模式的长度。
- 使用变量i遍历[0,N – M – 1]范围内的给定数组,并执行以下操作:
- 如果arr [i]和arr [i + M]的值相同,则将t加1 ;如果t与m相同,则将t更新为0并增加count 。如果count的值为K,则打印“是”并退出循环。
- 否则,如果t为M,则将计数增加1 。
- 完成上述步骤后,如果count的值与K不同,则打印“ No” ,因为不存在任何这种模式。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if array can be split
// into K consecutive and non-overlapping
// subarrays of length M consisting of a
// single distinct element
string checkPattern(int arr[], int m,
int k, int n)
{
int count = 1, t = 0;
// Traverse over the range [0, N - M - 1]
for (int i = 0; i < n - m; i++) {
// Check if arr[i] is the
// same as arr[i + m]
if (arr[i] == arr[i + m]) {
// Increment current length
// t of pattern matched by 1
t++;
// Check if t is equal to m,
// increment count of total
// repeated pattern
if (t == m) {
t = 0;
count++;
// Return true if length of
// total repeated pattern is k
if (count == k) {
return "Yes";
}
}
}
else {
// Update length of the
// current pattern
t = 0;
// Update count to 1
count = 1;
}
}
// Finally return false if
// no pattern found
return "No";
}
// Driver Code
int main()
{
int arr[] = { 6, 1, 3, 3, 3, 3 };
int M = 1, K = 3;
int N = sizeof(arr) / sizeof(arr[0]);
cout << checkPattern(arr, M, K, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to check if array can be split
// into K consecutive and non-overlapping
// subarrays of length M consisting of a
// single distinct element
static String checkPattern(int arr[], int m,
int k, int n)
{
int count = 1, t = 0;
// Traverse over the range [0, N - M - 1]
for (int i = 0; i < n - m; i++)
{
// Check if arr[i] is the
// same as arr[i + m]
if (arr[i] == arr[i + m])
{
// Increment current length
// t of pattern matched by 1
t++;
// Check if t is equal to m,
// increment count of total
// repeated pattern
if (t == m)
{
t = 0;
count++;
// Return true if length of
// total repeated pattern is k
if (count == k)
{
return "Yes";
}
}
}
else
{
// Update length of the
// current pattern
t = 0;
// Update count to 1
count = 1;
}
}
// Finally return false if
// no pattern found
return "No";
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 6, 1, 3, 3, 3, 3 };
int M = 1, K = 3;
int N = arr.length;
System.out.print(checkPattern(arr, M, K, N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 program for the above approach
# Function to check if array can be split
# into K consecutive and non-overlapping
# subarrays of length M consisting of a
# single distinct element
def checkPattern(arr, m, k, n):
count = 1
t = 0
# Traverse over the range [0, N - M - 1]
for i in range(n - m):
# Check if arr[i] is the
# same as arr[i + m]
if (arr[i] == arr[i + m]):
# Increment current length
# t of pattern matched by 1
t += 1
# Check if t is equal to m,
# increment count of total
# repeated pattern
if (t == m):
t = 0
count += 1
# Return true if length of
# total repeated pattern is k
if (count == k):
return "Yes"
else:
# Update length of the
# current pattern
t = 0
# Update count to 1
count = 1
# Finally return false if
# no pattern found
return "No"
# Driver Code
if __name__ == '__main__':
arr = [6, 1, 3, 3, 3, 3]
M = 1
K = 3
N = len(arr)
print(checkPattern(arr, M, K, N))
# This code is contributed by bgangwar59.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to check if array can be split
// into K consecutive and non-overlapping
// subarrays of length M consisting of a
// single distinct element
static String checkPattern(int []arr, int m,
int k, int n)
{
int count = 1, t = 0;
// Traverse over the range [0, N - M - 1]
for (int i = 0; i < n - m; i++)
{
// Check if arr[i] is the
// same as arr[i + m]
if (arr[i] == arr[i + m])
{
// Increment current length
// t of pattern matched by 1
t++;
// Check if t is equal to m,
// increment count of total
// repeated pattern
if (t == m)
{
t = 0;
count++;
// Return true if length of
// total repeated pattern is k
if (count == k)
{
return "Yes";
}
}
}
else
{
// Update length of the
// current pattern
t = 0;
// Update count to 1
count = 1;
}
}
// Finally return false if
// no pattern found
return "No";
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 6, 1, 3, 3, 3, 3 };
int M = 1, K = 3;
int N = arr.Length;
Console.Write(checkPattern(arr, M, K, N));
}
}
// This code is contributed by Rajput-Ji
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)