Dada una array arr[] de N enteros, la tarea es contar el número de grupos en la array dada.
Agrupación se define como una serie de 2 o más elementos adyacentes del mismo valor.
Ejemplos:
Entrada: arr[] = { 13, 15, 66, 66, 37, 8, 8, 11, 52 };
Salida: 2
Explicación:
Hay dos grupos en la array dada {66, 66} y {8, 8}.
Entrada: arr[] = {1, 2, 1, 4, 3, 2}
Salida: 0
Explicación:
No hay grupos en la array dada.
Enfoque: Para resolver el problema, debemos seguir los siguientes pasos:
- Recorra la array y compruebe si aparece el mismo elemento en dos índices consecutivos.
- Para cualquier ocurrencia de este tipo, repite hasta que ocurra un número diferente.
- Aumente el conteo de grupos en 1 solo después de la ejecución del paso 2. Si aún no se recorre toda la array, repita los pasos anteriores para los siguientes elementos.
- Imprima el conteo final de grupos después de recorrer toda la array.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to calculate // the number of clumps in // an array #include <bits/stdc++.h> 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
<script> // Javascript program to calculate // the number of clumps in // an array // Function to count the number of // clumps in the given array arr[] function countClumps(arr, N) { // Initialise count of clumps as 0 let clumps = 0; // Traverse the arr[] for (let i = 0; i < N - 1; i++) { let 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; } // Given array let arr = [ 13, 15, 66, 66, 66, 37, 37, 8, 8, 11, 11 ]; // length of the given array arr[] let N = arr.length; // Function Call document.write(countClumps(arr, N)); </script>
4
Complejidad de tiempo: O(N) , donde N es el número de elementos en la array dada.
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Harshit_Singh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA