📌  相关文章
📜  检查数组是否由每个不同元素的连续重复的子数组组成

📅  最后修改于: 2021-05-17 21:09:56             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是检查整个数组是否仅由子数组组成,以使每个子数组都由单个元素的连续重复组成,并且数组中的每个不同元素都属于该数组的一部分。子数组。

例子:

方法:
请按照以下步骤解决问题:

  • 初始化变量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)