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, 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 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

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


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Output:

3

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.