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)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximize number of groups formed with size not smaller than its largest element
- Maximum number of groups of size 3 containing two type of items
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K
- Count the number of ways to divide N in k groups incrementally
- Count all possible groups of size 2 or 3 that have sum as multiple of 3
- Reverse an array in groups of given size
- Reverse an array in groups of given size | Set 2 (Variations of Set 1 )
- Ways to split array into two groups of same XOR value
- Minimum difference between groups of size two
- Maximum difference between groups of size two
- Divide N segments into two non-empty groups such that given condition is satisfied
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Maximize the happiness of the groups on the Trip
- Minimum sum obtained from groups of four elements from the given array
- Minimize the cost of partitioning an array into K groups
- Reverse a singly Linked List in groups of given size | Set 3
- Count of different groups using Graph
- Partition the array into two odd length groups with minimized absolute difference between their median
- Reverse an array in groups of given size | Set 3 (Single traversal)
- Segregate groups of first N numbers having GCD equals to 1

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.