给定两个整数M和K以及一个由N 个正整数组成的数组arr[] ,任务是检查是否可以将数组拆分为K个长度为M 的连续非重叠子数组,以便每个子数组都包含一个不同的元素。如果发现是真的,则打印“是” 。否则,打印“否” 。
例子:
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,则打印“Yes”并跳出循环。
- 否则,如果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
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live