# Number of groups of magnets formed from N magnets

Given N magnets kept in a row one after another, either with a negative pole on the left and positive pole on the right (01) or positive pole on the left and negative pole on the right (10). Considering the fact that if 2 consecutive magnets have different poles facing each other, they form a group and attract to each other, find the total number of groups possible.

**Examples**:

Input: N = 6 magnets = {10, 10, 10, 01, 10, 10}Output: 3 The groups are formed by the following magnets: 1, 2, 3 4 5, 6Input: N = 5 magnets = {10, 10, 10, 10, 10, 01}Output: 1

Let us consider every pair of consecutive magnets, there are 2 possible cases:

- Both of them have the same configuration: In this case, the connecting ends will have different poles and hence they would belong to the same group.
- Both of them have different Configuration: In this case, the connecting ends will have the same pole and hence they would repel each other to form different groups.

So a new group will only be formed in the case when two consecutive magnets have different configuration. So traverse the array of magnets and find the number of consecutive pairs with the different configuration.

Below is the implementation of the above approach:

## C++

`// C++ program to find number of groups ` `// of magnets formed from N magnets ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count number of groups of ` `// magnets formed from N magnets ` `int` `countGroups(` `int` `n, string m[]) ` `{ ` ` ` `// Intinially only a single group ` ` ` `// for the first magnet ` ` ` `int` `count = 1; ` ` ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` ` ` `// Different configuration increases ` ` ` `// number of groups by 1 ` ` ` `if` `(m[i] != m[i - 1]) ` ` ` `count++; ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 6; ` ` ` ` ` `string m[n] = { ` `"10"` `, ` `"10"` `, ` `"10"` `, ` `"01"` `, ` `"10"` `, ` `"10"` `}; ` ` ` ` ` `cout << countGroups(n, m); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the maximum number ` `// of elements that can be added to a set ` `// such that it is the absolute difference // of magnets formed from N magnets ` ` ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` `class` `GFG{ ` ` ` `// Function to count number of groups of ` `// magnets formed from N magnets ` `static` `int` `countGroups(` `int` `n, String m[]) ` `{ ` ` ` `// Intinially only a single group ` ` ` `// for the first magnet ` ` ` `int` `count = ` `1` `; ` ` ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) ` ` ` ` ` `// Different configuration increases ` ` ` `// number of groups by 1 ` ` ` `if` `(m[i] != m[i - ` `1` `]) ` ` ` `count++; ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `n = ` `6` `; ` ` ` ` ` `String []m = { ` `"10"` `, ` `"10"` `, ` `"10"` `, ` `"01"` `, ` `"10"` `, ` `"10"` `}; ` ` ` ` ` `System.out.println( countGroups(n, m)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find number ` `# of groups of magnets formed ` `# from N magnets ` ` ` `# Function to count number of ` `# groups of magnets formed ` `# from N magnets ` `def` `countGroups(n, m): ` ` ` ` ` `# Intinially only a single ` ` ` `# group for the first magnet ` ` ` `count ` `=` `1` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, n): ` ` ` ` ` `# Different configuration increases ` ` ` `# number of groups by 1 ` ` ` `if` `(m[i] !` `=` `m[i ` `-` `1` `]): ` ` ` `count ` `+` `=` `1` ` ` ` ` `return` `count ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `6` ` ` ` ` `m ` `=` `[ ` `"10"` `, ` `"10"` `, ` `"10"` `, ` ` ` `"01"` `, ` `"10"` `, ` `"10"` `] ` ` ` ` ` `print` `(countGroups(n, m)) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find number of groups ` `// of magnets formed from N magnets ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to count number of groups of ` ` ` `// magnets formed from N magnets ` ` ` `static` `int` `countGroups(` `int` `n, String []m) ` ` ` `{ ` ` ` ` ` `// Intinially only a single group ` ` ` `// for the first magnet ` ` ` `int` `count = 1; ` ` ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` ` ` `// Different configuration increases ` ` ` `// number of groups by 1 ` ` ` `if` `(m[i] != m[i - 1]) ` ` ` `count++; ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 6; ` ` ` `String [] m = {` `"10"` `, ` `"10"` `, ` `"10"` `, ` ` ` `"01"` `, ` `"10"` `, ` `"10"` `}; ` ` ` ` ` `Console.WriteLine(countGroups(n, m)); ` `} ` `} ` ` ` `// This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find number of groups ` `// of magnets formed from N magnets ` ` ` `// Function to count number of groups ` `// of magnets formed from N magnets ` `function` `countGroups(` `$n` `, ` `$m` `) ` `{ ` ` ` `// Intinially only a single group ` ` ` `// for the first magnet ` ` ` `$count` `= 1; ` ` ` ` ` `for` `(` `$i` `= 1; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` ` ` `// Different configuration increases ` ` ` `// number of groups by 1 ` ` ` `if` `(` `$m` `[` `$i` `] != ` `$m` `[` `$i` `- 1]) ` ` ` `$count` `++; ` ` ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver Code ` `$n` `= 6; ` ` ` `$m` `= ` `array` `( ` `"10"` `, ` `"10"` `, ` `"10"` `, ` ` ` `"01"` `, ` `"10"` `, ` `"10"` `); ` ` ` `echo` `(countGroups(` `$n` `, ` `$m` `)); ` ` ` `// This code is contributed ` `// by Shivi_Aggarwal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(N)

## Recommended Posts:

- Maximum number of groups of size 3 containing two type of items
- Maximum number formed from array with K number of adjacent swaps allowed
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K
- Check if a number is formed by Concatenation of 1, 14 or 144 only
- Find the largest number that can be formed with the given digits
- Length of the smallest number which is divisible by K and formed by using 1's only
- Check if the large number formed is divisible by 41 or not
- Number of balanced bracket expressions that can be formed from a string
- Total number of triangles formed when there are H horizontal and V vertical lines
- Print the nearest prime number formed by adding prime numbers to N
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Count of different groups using Graph
- Reverse an array in groups of given size
- Maximum difference between groups of size two

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.