Iron,Magnet and wall

(FEMA 2)

Understanding the problem statement

Chef placed some magical tokens in a row of N cells. For simplicity, the row is represented as a string S, each character of the string can be one of the following: 

  1. Iron
  2. Magnet
  3. Empty cell
  4. Sheet
  5. Wall

T

_

M

_

:

X

Representation

S(i,j) denotes  the number of cells containg sheets between index i and j.

  1. Iron
  2. Magnet
  3. Empty cell
  4. Sheet
  5. Wall

T

_

M

_

:

X

Representation

Understanding the problem statement

S(i,j) denotes  the number of cells containg sheets between index i and j.

  1. Iron
  2. Magnet
  3. Empty cell
  4. Sheet
  5. Wall

T

_

M

_

:

X

Representation

Understanding the problem statement

S(i,j) denotes  the number of cells containg sheets between index i and j.

  1. Iron
  2. Magnet
  3. Empty cell
  4. Sheet
  5. Wall

T

_

M

_

:

X

Representation

i

j

S(i,j) = 4

Understanding the problem statement

The attraction power of a magnet at position i on cell j is given by the following law:

i

j

For  K = 10

P(i,j) = 10+1 - |6 -0 | - 3

P(i,j) = K + 1 - | j - i | - S(i,j)

Understanding the problem statement

The attraction power of a magnet at position i on cell j is given by the following law:

i

j

For  K = 10

P(i,j) = 10+1 - |6 -0 | - 3

P(i,j) = 11 - 9

P(i,j) = 2

P(i,j) = K + 1 - | j - i | - S(i,j)

Understanding the problem statement

The attraction power of a magnet at position i on cell j is given by the following law:

i

j

For  K = 10

P(i,j) = 10+1 - |6 -0 | - 3

P(i,j) = 11 - 9

P(i,j) = 2

P(i,j) = K + 1 - | j - i | - S(i,j)

P(i,j) > 0

The magnet can attract iron!!

Each magnet can attract

at most one and vice cersa..!!

Understanding the problem statement

The attraction power of a magnet at position i on cell j is given by the following law:

i

j

For  K = 10

P(i,j) = 10+1 - |6 -0 | - 3

P(i,j) = 11 - 9

P(i,j) = 2

P(i,j) = K + 1 - | j - i | - S(i,j)

P(i,j) > 0

If there is a wall between

Iron and magnet than

attraction won't happen!!

Understanding the problem statement

The attraction power of a magnet at position i on cell j is given by the following law:

i

j

Chef can decide for each magnet and iron to attract.

TASK

Find the maximum number of irons that can be attracted by the magnets.

Understanding the problem statement

Understanding the sample test cases

2
4 5
I::M
9 10
MIM_XII:M

1

2

Sample Cases

Output

i

j

2
4 5
I::M
9 10
MIM_XII:M

1

2

Sample Cases

Output

Understanding the sample test cases

i

j

2
4 5
I::M
9 10
MIM_XII:M

1

2

P(i,j) = K + 1 - | j - i | - S(i,j)

Sample Cases

Output

Understanding the sample test cases

i

j

2
4 5
I::M
9 10
MIM_XII:M

1

2

P(i,j) = K + 1 - | j - i | - S(i,j)

5+1 - |3-0| - 2

= 1

0

3

2

1

Sample Cases

Output

Understanding the sample test cases

i

j

2
4 5
I::M
9 10
MIM_XII:M

1

2

P(i,j) = K + 1 - | j - i | - S(i,j)

5+1 - |3-0| - 2

= 1

0

3

2

1

P(i,j)  >  0

Yes the only Iron is attracted by magnet.

Sample Cases

Output

Understanding the sample test cases

i

j

2
4 5
I::M
9 10
MIM_XII:M

1

2

Sample Cases

Output

Understanding the sample test cases

i

j

2
4 5
I::M
9 10
MIM_XII:M

1

2

P(i,j) = K + 1 - | j - i | - S(i,j)

10 + 1 - |1-0| - 0

10

0

8

7

6

5

4

3

2

1

Sample Cases

Output

Understanding the sample test cases

i

j

2
4 5
I::M
9 10
MIM_XII:M

1

2

0

8

7

6

5

4

3

2

1

Sample Cases

Output

Understanding the sample test cases

i

j

2
4 5
I::M
9 10
MIM_XII:M

1

2

0

8

7

6

5

4

3

2

1

The magnet cannot attract Iron because wall is present between them..!!

Sample Cases

Output

Understanding the sample test cases

i

j

2
4 5
I::M
9 10
MIM_XII:M

1

2

P(i,j) = K + 1 - | j - i | - S(i,j)

10 + 1 - |8-5| - 1

7

0

8

7

6

5

4

3

2

1

Sample Cases

Output

Understanding the sample test cases

i

j

2
4 5
I::M
9 10
MIM_XII:M

1

2

P(i,j) = K + 1 - | j - i | - S(i,j)

10 + 1 - |8-5| - 1

7

0

8

7

6

5

4

3

2

1

P(i,j) = K + 1 - | j - i | - S(i,j)

10 + 1 - |8-6| - 1

8

Sample Cases

Output

Understanding the sample test cases

i

j

2
4 5
I::M
9 10
MIM_XII:M

1

2

0

8

7

6

5

4

3

2

1

Two irons can be attracted at max!!

Sample Cases

Output

Understanding the sample test cases

Approach Formation

Observation 1 :

Each magnet has its attraction over each cell.

Approach Formation

Observation 1 :

Each magnet has its attraction over each cell.

 A magnet at position ii has attraction  of KK at positions i-1i1 and i+1i+1, and after that it starts decreasing by one with distance, i.e the attraction power is K-1K1 at positions i-2i2 and i+2i+2, and K-2K2 at positions i-3i3 and i+3i+3 and so on.

Not taking sheets into consideration

Approach Formation

Observation 1 :

 A magnet at position ii has an  of KK at positions i-1i1 and i+1i+1, and after that it starts decreasing by one with distance, i.e the attraction power is K-1K1 at positions i-2i2 and i+2i+2, and K-2K2 at positions i-3i3 and i+3i+3 and so on.

P(i,j) = K + 1 - | j - i | - S(i,j)

Approach Formation

Observation 1 :

 A magnet at position ii has an  of KK at positions i-1i1 and i+1i+1, and after that it starts decreasing by one with distance, i.e the attraction power is K-1K1 at positions i-2i2 and i+2i+2, and K-2K2 at positions i-3i3 and i+3i+3 and so on.

P(i,j) = K + 1 - | j - i | - S(i,j)

K + 1 - |i+1-i|- 0

 = K

For (i+1)

i

i+1

i-1

Approach Formation

Observation 1 :

 A magnet at position ii has an  of KK at positions i-1i1 and i+1i+1, and after that it starts decreasing by one with distance, i.e the attraction power is K-1K1 at positions i-2i2 and i+2i+2, and K-2K2 at positions i-3i3 and i+3i+3 and so on.

P(i,j) = K + 1 - | j - i | - S(i,j)

K + 1 - |i-1-i|- 0

 = K

For (i-1)

i

i+1

i-1

Approach Formation

Observation 1 :

 A magnet at position ii has an  of KK at positions i-1i1 and i+1i+1, and after that it starts decreasing by one with distance, i.e the attraction power is K-1K1 at positions i-2i2 and i+2i+2, and K-2K2 at positions i-3i3 and i+3i+3 and so on.

P(i,j) = K + 1 - | j - i | - S(i,j)

K + 1 - |i+2-i|- 0

 = K-1

For (i+2)

i

i+1

i-1

i+2

i-2

Approach Formation

Observation 1 :

 A magnet at position ii has an  of KK at positions i-1i1 and i+1i+1, and after that it starts decreasing by one with distance, i.e the attraction power is K-1K1 at positions i-2i2 and i+2i+2, and K-2K2 at positions i-3i3 and i+3i+3 and so on.

P(i,j) = K + 1 - | j - i | - S(i,j)

K + 1 - |i-2-i|- 0

 = K-1

For (i-2)

i

i+1

i-1

i+2

i-2

Approach Formation

Observation 1 :

P(i,j) = K + 1 - | j - i | - S(i,j)

K + 1 - |i+2-i|- 1

 = K-2

For (i+2)

i

i+1

i-1

i+2

i-2

But what will happen when the sheets comes in between ?

i+3

Approach Formation

Observation 1 :

P(i,j) = K + 1 - | j - i | - S(i,j)

K + 1 - |i+2-i|- 1

 = K-2

For (i+2)

i

i+1

i-1

i+2

i-2

But what will happen when the sheets comes in between ?

i+3

Approach Formation

Observation 1 :

P(i,j) = K + 1 - | j - i | - S(i,j)

K + 1 - |i+3-i|- 2

 = K-4

For (i+3)

i

i+1

i-1

i+2

i-2

But what will happen when the sheets comes in between ?

i+3

Approach Formation

Observation 1 :

P(i,j) = K + 1 - | j - i | - S(i,j)

K + 1 - |i+4-i|- 2

 = K-5

For (i+4)

i

i+1

i-1

i+2

i-2

But what will happen when the sheets comes in between ?

i+3

i+4

Approach Formation

Observation 1 :

P(i,j) = K + 1 - | j - i | - S(i,j)

K + 1 - |i+4-i|- 0

 = K-3

For (i+4)

i

i+1

i-1

i+2

i-2

But what will happen when the sheets comes in between ?

i+3

i+4

Approach Formation

Observation 1 :

P(i,j) = K + 1 - | j - i | - S(i,j)

K + 1 - |i+6-i|- 0

 = K-5

For (i+6)

i

i+1

i-1

i+2

i-2

But what will happen when the sheets comes in between ?

i+3

i+4

i+5

i+6

Approach Formation

Observation 1 :

P(i,j) = K + 1 - | j - i | - S(i,j)

K + 1 - |i+6-i|- 0

 = K-5

For (i+6)

What we did?

Whenever we encounter sheet we will just increase the extra space..!! And remove S(i,j) from the formula!!

i

i+1

i-1

i+2

i-2

i+3

i+4

i+5

i+6

Approach Formation

i

i+1

i-1

i+2

i-2

i+3

i+4

i+5

i+6

Observation 2:

 

Whenever we encounter a wall we cannot take a magnet and the iron from the two different sides.

Approach Formation

i

i+1

i-1

i+2

i-2

i+3

i+4

i+5

i+6

Observation 2:

 

Whenever we encounter a wall we cannot take a magnet and the iron from the two different sides.

Approach Formation

i

i+1

i-1

i+2

i-2

i+3

i+4

i+5

i+6

Observation 2:

 

Whenever we encounter a wall we cannot take a magnet and the iron from the two different sides.

Approach Formation

i

i+1

i-1

i+2

i-2

i+3

i+4

i+5

i+6

Observation 2:

 

Whenever we encounter a wall we cannot take a magnet and the iron from the two different sides.

Approach Formation

i

i+1

i-1

i+2

i-2

i+3

i+4

i+5

i+6

Observation 2:

 

Whenever we encounter a wall we cannot take a magnet and the iron from the two different sides.

Approach Formation

i

i+1

i-1

i-2

i+3

i+4

i+5

i+6

Observation 2:

 

Solve the two parts seperately and find how many irons are attracted by magnets in the seprate groups..!!

Approach Formation

Observation 3:

 

See how many iron magnet pair we can form affectively..!!!

Approach Formation

Observation 3:

 

See how many iron magnet pair we can form affectively..!!!

K

K-1

K-2

0

1

K - elemnets

i

........

Approach Formation

Observation 3:

 

See how many iron magnet pair we can form affectively..!!!

j-i < K

K

K-1

K-2

0

1

K - elemnets

i

........

Approach Formation

Observation 3:

 

Iron at position k-1 is attracted by magnet at position k.

j-i < K

K

K-1

K-2

0

1

K - elemnets

i

........

Approach Formation

Observation 3:

 

Hence we will form Iron-magnet pair.

j-i < K

K

K-1

K-2

0

1

K - elemnets

i

........

Approach Formation

Observation 3:

 

We will select the magnet which is at maximum distance in the range.

j-i < K

K

K-1

K-2

0

1

K - elemnets

i

........

Approach Formation

Observation 3:

 

We need to take the postion of magnet and see how many irons lie in the attraction range of the magnet.

0

1

8

7

6

5

4

3

2

Approach Formation

Observation 3:

 

We need to take the postion of magnet and see how many irons lie in the attraction range of the magnet.

0

1

8

7

6

5

4

3

2

Approach Formation

Observation 3:

 

We need to take the postion of magnet and see how many irons lie in the attraction range of the magnet.

         0         2
         4         

0

1

8

7

6

5

4

3

2

Approach Formation

Observation 3:

 

We need to take the postion of magnet and see how many irons lie in the attraction range of the magnet.

         0         2
         4         

0

1

8

7

6

5

4

3

2

Approach Formation

Observation 3:

 

In the attraction range we will select the magnet which is at maximum distance to form the pair. 

                  2
         4         

0

1

8

7

6

5

4

3

2

Approach Formation

Observation 3:

 

In the attraction range we will select the magnet which is at maximum distance to form the pair.

                  2
                  

0

1

8

7

6

5

4

3

2

Approach Formation

Observation 3:

 

A replica situation can be done for Iron as well..!!

        2          3
         0         

0

1

3

2

Check if iron can be attracted to a magenet or not!!

Approach Formation

Observation 3:

 

A replica situation can be done for Iron as well..!!

                 3
                  

0

1

3

2

Check if iron can be attracted to a magenet or not!!

FEMA 2

By Chirayu Jain