Find Minimum and Maximum distinct persons entering or leaving the room
Last Updated :
09 May, 2022
Given a binary string persons where ‘1’ and ‘0’ represent people entering and leaving a room respectively. The task is to find the Minimum and Maximum Distinct Persons entering or leaving the building.
Examples:
Input: “000”
Output: Minimum Persons: 3
Maximum Persons: 3
Explanation: 3 distinct persons left the building.
Input: “111”
Output: Minimum Persons: 3
Maximum Persons: 3
Explanation: 3 distinct persons entered the building.
Input: “110011”
Output: Minimum Persons: 2
Maximum Persons: 6
Explanation: 2 persons entered->those 2 persons left->same 2 persons entered
to account for minimum 2 persons.
All persons entered or left are distinct to account for maximum 6 persons.
Input: “10101”
Output: Minimum Persons: 1
Maximum Persons: 5
Explanation: 1 person entered- > he left -> again entered -> again left -> and again entered
to account for minimum 1 person.
All persons entered or left are distinct to account for maximum 5 persons.
Approach: The problem can be solved based on the following observation:
- Each person entering or leaving the room can be a unique person. This will give the maximum number of persons that can enter a room. This will be equal to the total number of times a leaving or entering operation is performed,
- Each time the same persons who are leaving the room are entering the room next time. So the maximum among the people leaving at a time or entering at a time is the minimum possible number of unique persons.
Follow the below illustration for a better understanding.
Illustration:
Consider persons = “10101”
For finding the maximum:
=> At first, first person (say P1) enters the room
=> Then, second person (say P2) exits the room
=> Then, third person (say P3) enters the room
=> Then, fourth person (say P4) exits the room
=> At last, fifth person (say P5) enters the room
Total 5 persons enter or leave the room at most.
For finding the minimum possible persons:
=> At first, first person (say P1) enters the room.
=> Then P1 exits the room.
=> Then P1 again enters the room.
=> Then again P1 exits the room.
=> At last P1 again enters the room.
So at least one person enters or leaves the room.
Follow the below steps to implement the above observation:
- Start traversing the whole string persons.
- If persons[i] = ‘1’ then increment entered else decrement entered.
- Store the maximum value of entered and N (size of string persons) as the first and second value of the pair result.
- In that loop, if at anytime entered becomes negative then re-initialize it to 0 and increment the first value of the pair result.
- Return result as the final pair containing minimum and maximum distinct persons as first and second value respectively.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
pair< int , int > minDistPersons(string& persons)
{
int N=persons.length(),entered=0;
pair< int , int >result={0,N};
for ( int i=0;i<N;i++)
{
if (persons[i]== '1' )
entered++;
else entered--;
result.first=max(result.first,entered);
if (entered<0)
{
entered=0;
result.first++;
}
}
return result;
}
int main()
{
string persons = "10101" ;
pair< int , int > ans = minDistPersons(persons);
cout << "Minimum Persons: " << ans.first
<< "\n" ;
cout << "Maximum Persons: " << ans.second;
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static int [] minDistPersons(String persons)
{
int N = persons.length();
int entered = 0 ;
int result[] = new int [ 2 ];
result[ 1 ] = N;
for ( int i = 0 ; i < N; i++) {
if (persons.charAt(i) == '1' )
entered++;
else entered--;
result[ 0 ] = Math.max(result[ 0 ],entered);
if (entered< 0 )
{
entered= 0 ;
result[ 0 ]++;
}
}
return result;
}
public static void main(String[] args)
{
String persons = "10101" ;
int ans[] = minDistPersons(persons);
System.out.println( "Minimum Persons: " + ans[ 0 ]);
System.out.print( "Maximum Persons: " + ans[ 1 ]);
}
}
|
Python
def minDistPersons(persons):
N = len (persons)
entered, exited = 0 , 0
result = [ 0 , N]
for i in range (N):
if persons[i] = = "1" :
entered + = 1
else :
entered - = 1
result[ 0 ] = max (result[ 0 ], entered)
if (entered< 0 ):
entered = 0
result[ 0 ] + = 1
return result
persons = "10101"
ans = minDistPersons(persons)
print ( "Minimum Persons: " + str (ans[ 0 ]))
print ( "Maximum Persons: " + str (ans[ 1 ]))
|
C#
using System;
class GFG
{
public static int [] minDistPersons( string persons)
{
int N = persons.Length;
int entered = 0;
int [] result = new int [2];
result[1] = N;
for ( int i = 0; i < N; i++) {
if (persons[i] == '1' )
entered++;
else entered--;
result[0] = Math.Max(result[0],entered);
if (entered<0)
{
entered=0;
result[0]++;
}
}
return result;
}
public static void Main()
{
string persons = "10101" ;
int [] ans = minDistPersons(persons);
Console.WriteLine( "Minimum Persons: " + ans[0]);
Console.Write( "Maximum Persons: " + ans[1]);
}
}
|
Javascript
<script>
function minDistPersons(persons) {
let N = persons.length;
let entered = 0;
let result = [0, N];
for (let i = 0; i < N; i++) {
if (persons[i] == '1' )
entered++;
else
entered--;
result[0] = Math.max(...[result[0], entered]);
if (entered<0)
{
entered=0;
result[0]++;
}
}
return result;
}
let persons = "10101" ;
let ans = minDistPersons(persons);
document.write( "Minimum Persons: " + ans[0] + '<br>' );
document.write( "Maximum Persons: " + ans[1]);
</script>
|
Output
Minimum Persons: 1
Maximum Persons: 5
Time Complexity: O(N), where N is the length of the string persons.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...