- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Number of groups of magnets formed from N magnets in C++

The digit **1** represent positive pole whereas **0** represents negative pole.

The magnet will have both poles as **10** or **01.** A group can be formed with the magnets that attracts each other. The magnets with different pole facing each other will be in the same group.

Here, you are given N number of magnets. You need to find out number of groups can be formed with them.

Whenever there are two different magnets side by side, there forms a new group. In that case increment the count of the groups.

Let's see an example.

**Input**

magnets = ["10", "01", "01", "01", "10", "01"]

**Output**

4

There are 4 magnets that attract each other side by side in the given array. They are "10", "01", "10", "01".

## Algorithm

- Initialise the array with magnets.
- Initialise the count to 1 as we are taking pairs.
- Write a loop that iterates from 1 index to the end of the array.
- If the current magnet is not equal to the previous one, then increment the count.
- Return the count.

## Implementation

Following is the implementation of the above algorithm in C++

#include <bits/stdc++.h> using namespace std; int getMangetGroupsCount(string magnets[], int n) { int count = 1; for (int i = 1; i < n; i++) { if (magnets[i] != magnets[i - 1]) { count++; } } return count; } int main() { string magnets[] = { "10", "01", "01", "01", "10", "01" }; int n = 6; cout << getMangetGroupsCount(magnets, n) << endl; return 0; }

## Output

If you run the above code, then you will get the following result.

4

- Related Questions & Answers
- Maximum number of 3-person teams formed from two groups in C++
- C++ program to find number of groups can be formed from set of programmers
- Find the Number of Triangles Formed from a Set of Points on Three Lines using C++\n
- Program to find minimum number of groups in communication towers in C++?\n
- Minimize the sum of squares of sum of N/2 paired formed by N numbers in C++
- Split tuple into groups of n in Python
- Print all possible strings of length k that can be formed from a set of n characters in C++
- Count total number of digits from 1 to N in C++
- Maximum number of groups of size 3 containing two type of items in C++
- Recursive sum of digits of a number formed by repeated appends in C++
- Return the difference between the maximum & minimum number formed out of the number n in JavaScript
- Number of Groups of Sizes Two Or Three Divisible By 3 in C++
- Minimum sum of two numbers formed from digits of an array in C++
- N digit numbers divisible by 5 formed from the M digits in C++
- Count of alphabets whose ASCII values can be formed with the digits of N in C++