给定一个由N 个整数组成的数组arr[] ,任务是检查整个数组是否仅由子数组组成,使得每个子数组都由单个元素的连续重复组成,并且数组中的每个不同元素都是这样的一部分子阵列。
例子:
Input: N = 10, arr[] = {1, 1, 1, 1, 2, 2, 3, 3, 3, 3}
Output: Yes
Explanation:
The given array consists of 3 distinct elements {1, 2, 3} and subarrays {1, 1, 1, 1}, {2, 2}, {3, 3, 3, 3}.
Therefore, the given array satisfies the conditions.
Input: N = 10, arr[] = {1, 1, 1, 2, 2, 2, 2, 1, 3, 3}
Output: No
Explanation:
The given array consists of 3 distinct elements {1, 2, 3} and subarrays {1, 1, 1}, {2, 2, 2, 2}, {1}, {3, 3}.
Since the subarray {1} does not contain any repetition, the given array does not satisfy the conditions.
方法:
请按照以下步骤解决问题:
- 初始化一个变量curr = 0来存储遇到的单个重复元素的每个子数组的大小。
- 如果在arr[i] ≠ arr[i – 1]处找到任何此类索引,请检查curr是否大于1 。如果是这样,请将curr重置为0并继续。否则,打印“否”作为单个元素的子数组存在而不重复。
- 否则,增加curr 。
- 遍历数组后,检查curr是否大于1 。如果curr等于1 ,这将确保最后一个元素与倒数第二个元素不同。因此,打印“否” 。
- 否则,打印“是” 。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above problem
#include
using namespace std;
// Function to check if the
// array is made up of
// subarrays of repetitions
bool ContinuousElements(int a[],
int n)
{
// Base Case
if (n == 1)
return false;
// Stores the size of
// current subarray
int curr = 1;
for (int i = 1; i < n; i++) {
// If a different element
// is encountered
if (a[i] != a[i - 1]) {
// If the previous subarray
// was a single element
if (curr == 1)
return false;
// Reset to new subarray
else
curr = 0;
}
// Increase size of subarray
curr++;
}
// If last element differed from
// the second last element
if (curr == 1)
return false;
return true;
}
// Driver code
int main()
{
int a[] = { 1, 1, 2, 2, 1, 3, 3 };
int n = sizeof(a)
/ sizeof(a[0]);
if (ContinuousElements(a, n))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
Java
// Java Program to implement
// the above approach
class GFG{
// Function to check if the
// array is made up of
// subarrays of repetitions
static boolean ContinuousElements(int a[],
int n)
{
// Base Case
if (n == 1)
return false;
// Stores the size of
// current subarray
int curr = 1;
for (int i = 1; i < n; i++)
{
// If a different element
// is encountered
if (a[i] != a[i - 1])
{
// If the previous subarray
// was a single element
if (curr == 1)
return false;
// Reset to new subarray
else
curr = 0;
}
// Increase size of subarray
curr++;
}
// If last element differed from
// the second last element
if (curr == 1)
return false;
return true;
}
// Driver code
public static void main(String[] args)
{
int a[] = { 1, 1, 2, 2, 1, 3, 3 };
int n = a.length;
if (ContinuousElements(a, n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by rock_cool
Python3
# Python3 program to implement
# the above problem
# Function to check if the
# array is made up of
# subarrays of repetitions
def ContinuousElements(a, n):
# Base Case
if (n == 1):
return False
# Stores the size of
# current subarray
curr = 1
for i in range (1, n):
# If a different element
# is encountered
if (a[i] != a[i - 1]):
# If the previous subarray
# was a single element
if (curr == 1):
return False
# Reset to new subarray
else:
curr = 0
# Increase size of subarray
curr += 1
# If last element differed from
# the second last element
if (curr == 1):
return False
return True
# Driver code
if __name__ == "__main__":
a = [1, 1, 2, 2, 1, 3, 3]
n = len(a)
if (ContinuousElements(a, n)):
print ("Yes")
else:
print ("No")
# This code is contributed by Chitranayal
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to check if the
// array is made up of
// subarrays of repetitions
static Boolean ContinuousElements(int []a,
int n)
{
// Base Case
if (n == 1)
return false;
// Stores the size of
// current subarray
int curr = 1;
for(int i = 1; i < n; i++)
{
// If a different element
// is encountered
if (a[i] != a[i - 1])
{
// If the previous subarray
// was a single element
if (curr == 1)
return false;
// Reset to new subarray
else
curr = 0;
}
// Increase size of subarray
curr++;
}
// If last element differed from
// the second last element
if (curr == 1)
return false;
return true;
}
// Driver code
public static void Main(String[] args)
{
int []a = { 1, 1, 2, 2, 1, 3, 3 };
int n = a.Length;
if (ContinuousElements(a, n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by shivanisinghss2110
Javascript
No
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live