Skip to content
Related Articles

Related Articles

Improve Article
Number of groups of magnets formed from N magnets
  • Difficulty Level : Basic
  • Last Updated : 21 May, 2021

Given N magnets kept in a row one after another, either with a negative pole on the left and a positive pole on the right (01) or a positive pole on the left and a 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 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, 6

Input : 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 configurations. 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 configurations. To traverse the array of magnets and find the number of consecutive pairs with the different configurations.

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;
}

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));
   
}
}

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

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

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
?>

Javascript




<script>
// Javascript 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
     
// 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
    let count = 1;
     
    for (let i = 1; i < n; i++)
     
        // Different configuration increases
        // number of groups by 1
        if (m[i] != m[i - 1])
            count++;
     
    return count;
}
     
    // Driver Code
    let n = 6;
    let m=[ "10", "10", "10", "01", "10", "10"];
    document.write( countGroups(n, m));
     
 
// This code is contributed by rag2127
</script>
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes




My Personal Notes arrow_drop_up
Recommended Articles
Page :