📜  计算给定数组中的结块数

📅  最后修改于: 2021-04-26 17:43:15             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是计算给定数组中的结块数。

例子:

方法:为了解决问题,我们需要遵循以下步骤:

  1. 遍历数组,并检查两个连续索引上是否存在相同元素。
  2. 对于任何这种情况,请循环播放直到出现不同的数字。
  3. 仅在执行步骤2之后,才将计数增加1。如果尚未遍历整个数组,请对以下元素重复上述步骤。
  4. 遍历整个数组后,打印出最终的团块计数。

下面是上述方法的实现:

C++
// C++ program to calculate
// the number of clumps in
// an array
#include 
using namespace std;
 
// Function to count the number of
// clumps in the given array arr[]
int countClumps(int arr[], int N)
{
 
    // Initialise count of clumps as 0
    int clumps = 0;
 
    // Traverse the arr[]
    for (int i = 0; i < N - 1; i++) {
 
        int flag = 0;
        // Whenever a sequence of same
        // value is encountered
        while (arr[i] == arr[i + 1]) {
            flag = 1;
            i++;
        }
 
        if (flag)
            clumps++;
    }
 
    // Return the count of clumps
    return clumps;
}
// Driver Code
int main()
{
 
    // Given array
    int arr[] = { 13, 15, 66, 66, 66, 37, 37,
                  8, 8, 11, 11 };
 
    // length of the given array arr[]
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << countClumps(arr, N) << '\n';
    return 0;
}


Java
// Java program for the above approach
class Test {
 
    // Given array arr[]
    static int arr[] = { 13, 15, 66, 66, 66,
                         37, 37, 8, 8, 11, 11 };
 
    // Function to count the number of
    // clumps in the given array arr[]
    static int countClumps()
    {
        int l = arr.length;
 
        // Initialise count of clumps as 0
        int clumps = 0;
 
        // Traverse the arr[]
        for (int i = 0; i < l - 1; i++) {
 
            int flag = 0;
            // Whenever a sequence of same
            // value is encountered
            while (i < l - 1
                   && arr[i] == arr[i + 1]) {
                flag = 1;
                i++;
            }
 
            if (flag == 1)
                clumps++;
        }
 
        // Return the count of clumps
        return clumps;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Function Call
        System.out.println(countClumps());
    }
}


Python3
# Python3 program to calculate
# the number of clumps in
# an array
 
# Function to count the number of
# clumps in the given array arr[]
def countClumps(arr, N):
     
    # Initialise count of clumps as 0
    clumps = 0
 
    # Traverse the arr[]
    i = 0
    while(i < N - 1):
         
        flag = 0
         
        # Whenever a sequence of same
        # value is encountered
        while (i + 1 < N and
               arr[i] == arr[i + 1]):
            flag = 1
            i += 1
 
        if (flag):
            clumps += 1
             
        i += 1
 
    # Return the count of clumps
    return clumps
     
# Driver Code
 
# Given array
arr = [ 13, 15, 66, 66, 66,
        37, 37, 8, 8, 11, 11 ]
 
# length of the given array arr[]
N = len(arr)
 
# Function Call
print(countClumps(arr, N))
 
# This code is contributed by yatin


C#
// C# program for the above approach
using System;
class GFG{
 
// Given array arr[]
static int []arr = { 13, 15, 66, 66, 66,
                     37, 37, 8, 8, 11, 11 };
 
// Function to count the number of
// clumps in the given array arr[]
static int countClumps()
{
    int l = arr.Length;
 
    // Initialise count of clumps as 0
    int clumps = 0;
 
    // Traverse the arr[]
    for (int i = 0; i < l - 1; i++)
    {
        int flag = 0;
         
        // Whenever a sequence of same
        // value is encountered
        while (i < l - 1 && arr[i] == arr[i + 1])
        {
            flag = 1;
            i++;
        }
 
        if (flag == 1)
            clumps++;
    }
 
    // Return the count of clumps
    return clumps;
}
 
// Driver Code
public static void Main()
{
 
    // Function Call
    Console.WriteLine(countClumps());
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
4

时间复杂度: O(N) ,其中N是给定数组中元素的数量。
辅助空间: O(1)

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”