Find the total guests that are present at the party
Last Updated :
13 Mar, 2023
A person hosts a party and invites N guests to it. However, each guest has a condition, that each guest ‘Gi’ only stays at the party if there are at least ‘Pi’ people already at the party, otherwise leaves. The total number of guests N and the number of people each guest needs ‘Pi’ are given as input for each guest. The task is to find the total guests that are present at the party. It is also given that the guests arrive at the party in the order given in the array ‘Pi’
Examples:
Input: N = 5, Pi = {1, 0, 2, 1, 3}
Output: 2
Explanation:
Since 5 guests are invited to the party.
Total guest present initially = 0
For Guest number 1:
The 1st guest needs at least 1 person,
since he is the first to arrive,
and there is no one else, so he leaves.
Therefore, Total guest so far = 0
For Guest number 2:
The 2nd guest needs 0 people, so he stays.
Therefore, Total guest so far = 0 + 1 = 1
For Guest number 3:
The 3rd guest needs at least 2 people,
And there are still only 1 guest present,
so he leaves.
Therefore, Total guest so far = 1 + 0 = 1
For Guest number 4:
The 4th guest needs at least 1 people,
And there is 1 guest present, so he stays.
Therefore, Total guest so far = 1 + 1 = 2
For Guest number 5:
The 5th guest needs at least 3 people,
And there is only 2 guest present, so he leaves.
Therefore, Total guest so far = 2 + 0 = 2
Total guests that are present at the party = 2.
Input: N = 3, Pi = {0, 2, 1}
Output: 2
Explanation:
Since 3 guests are invited to the party.
Total guest present initially = 0
For Guest number 1:
The 1st guest needs 0 people, so he stays.
Therefore, Total guest so far = 1
For Guest number 2:
The 2nd guest needs at least 2 people,
And there are still only 1 guest present,
so he leaves.
Therefore, Total guest so far = 1 + 0 = 1
For Guest number 3:
The 3rd guest needs at least 1 people,
And there is 1 guest present, so he stays.
Therefore, Total guest so far = 1 + 1 = 2
Total guests that are present at the party = 2.
Approach:
- Get the number of guests invited in N and the requirement of each guest in array guest[].
- Initialize totalGuests to 0, as the total number of guests present.
- Iterate in the array guest[] from 0 to N-1.
- If the requirement of the guest is less than or equal to totalGuests, Increment totalGuests by 1
- When complete array guest[] has been traversed, print the total number of guests ‘totalGuests’
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findGuest( int array[], int N)
{
int count = 0;
for ( int i = 0; i < N; i++) {
if (array[i] <= count) {
count++;
}
}
return count;
}
int main()
{
int N = 5;
int guests[] = { 1, 0, 2, 1, 3 };
int totalGuests = findGuest(guests, N);
cout << totalGuests << endl;
return 0;
}
|
Java
class GFG
{
static int findGuest( int array[], int N)
{
int count = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (array[i] <= count)
{
count++;
}
}
return count;
}
public static void main(String[] args)
{
int N = 5 ;
int guests[] = { 1 , 0 , 2 , 1 , 3 };
int totalGuests = findGuest(guests, N);
System.out.println(totalGuests);
}
}
|
Python3
def findGuest(guests, N):
count = 0
for i in range (N):
if guests[i] < = count:
count + = 1
return count
N = 5
guests = [ 1 , 0 , 2 , 1 , 3 ]
totalGusets = findGuest(guests, N)
print (totalGusets)
|
C#
using System;
class GFG
{
static int findGuest( int [] array, int N)
{
int count = 0;
for ( int i = 0; i < N; i++)
{
if (array[i] <= count)
{
count++;
}
}
return count;
}
public static void Main ()
{
int N = 5;
int [] guests = { 1, 0, 2, 1, 3 };
int totalGuests = findGuest(guests, N);
Console.WriteLine(totalGuests);
}
}
|
PHP
<?php
function findGuest( $array , $N )
{
$count = 0;
for ( $i = 0; $i < $N ; $i ++)
{
if ( $array [ $i ] <= $count )
{
$count ++;
}
}
return $count ;
}
$N = 5;
$guests = array (1, 0, 2, 1, 3 );
$totalGuests = findGuest( $guests , $N );
echo $totalGuests ;
?>
|
Javascript
<script>
function findGuest(array , N) {
var count = 0;
for (i = 0; i < N; i++) {
if (array[i] <= count) {
count++;
}
}
return count;
}
var N = 5;
var guests = [ 1, 0, 2, 1, 3 ];
var totalGuests = findGuest(guests, N);
document.write(totalGuests);
</script>
|
Time complexity: O(N) where N is size of given input array
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...