Encuentra el total de invitados que están presentes en la fiesta.

Una persona organiza una fiesta e invita a N personas a ella. Sin embargo, cada invitado tiene una condición, que cada invitado ‘Gi’ solo se quede en la fiesta si ya hay al menos personas ‘Pi’ en la fiesta, de lo contrario se va. El número total de invitados N y el número de personas que cada invitado necesita ‘Pi’ se dan como entrada para cada invitado. La tarea es encontrar el total de invitados que están presentes en la fiesta. También se da que los invitados lleguen a la fiesta en el orden dado en el arreglo ‘Pi’
Ejemplos: 
 

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.

Acercarse: 
 

  • Obtenga el número de invitados en N y el requisito de cada invitado en el arreglo guest[].
  • Inicialice totalGuests a 0, como el número total de invitados presentes.
  • Iterar en la array guest[] de 0 a N-1.
  • Si el requerimiento del invitado es menor o igual a totalGuests, Incremente totalGuests por 1
  • Cuando se haya recorrido la array completa guest[], imprima el número total de invitados ‘totalGuests’

Implementación: 
 

C++

// C++ program to get the
// total number of guests at the party
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the totalGuests
int findGuest(int array[], int N)
{
    // Total guest before the party are 0
    int count = 0;
 
    // Checking requirements for each guest
    for (int i = 0; i < N; i++) {
 
        // If requirements are met
        if (array[i] <= count) {
 
            // The Gi guest decides to stay
            // So increment total guest by 1
            count++;
        }
    }
 
    // Return the totalnumber of guest
    return count;
}
 
// Driver code
int main()
{
 
    // Get the number of guests invited
    int N = 5;
 
    // Guests array stores
    // the requirement by each guest
    int guests[] = { 1, 0, 2, 1, 3 };
 
    // Get the total number of guests present
    int totalGuests = findGuest(guests, N);
 
    cout << totalGuests << endl;
 
    return 0;
}

Java

// Java program to get the
// total number of guests at the party
class GFG
{
     
// Function to find the totalGuests
static int findGuest(int array[], int N)
{
    // Total guest before the party are 0
    int count = 0;
 
    // Checking requirements for each guest
    for (int i = 0; i < N; i++)
    {
 
        // If requirements are met
        if (array[i] <= count)
        {
 
            // The Gi guest decides to stay
            // So increment total guest by 1
            count++;
        }
    }
 
    // Return the totalnumber of guest
    return count;
}
 
// Driver code
public static void main(String[] args)
{
 
    // Get the number of guests invited
    int N = 5;
 
    // Guests array stores
    // the requirement by each guest
    int guests[] = { 1, 0, 2, 1, 3 };
 
    // Get the total number of guests present
    int totalGuests = findGuest(guests, N);
 
    System.out.println(totalGuests);
}
}
 
// This code is contributed by Code_Mech

Python3

# Python3 program to get the
# total number of guests at the party
 
# Function to find the totalGuests
def findGuest(guests, N):
    count = 0
 
    # Checking requirements for each guest
    for i in range(N):
 
        # If requirements are met
        if guests[i] <= count:
 
            # The Gi guest decides to stay
            # So increment total guest by 1
            count += 1
             
    # Return the totalnumber of gues
    return count
 
# Driver code
N = 5
guests = [1, 0, 2, 1, 3]
totalGusets = findGuest(guests, N)
print(totalGusets)
 
# This code is contributed by Shrikant13

C#

// C# program to get the
// total number of guests at the party
using System;
 
class GFG
{
         
    // Function to find the totalGuests
    static int findGuest(int [] array, int N)
    {
        // Total guest before the party are 0
        int count = 0;
     
        // Checking requirements for each guest
        for (int i = 0; i < N; i++)
        {
     
            // If requirements are met
            if (array[i] <= count)
            {
     
                // The Gi guest decides to stay
                // So increment total guest by 1
                count++;
            }
        }
     
        // Return the totalnumber of guest
        return count;
    }
     
    // Driver code
    public static void Main ()
    {
     
        // Get the number of guests invited
        int N = 5;
     
        // Guests array stores
        // the requirement by each guest
        int [] guests = { 1, 0, 2, 1, 3 };
     
        // Get the total number of guests present
        int totalGuests = findGuest(guests, N);
     
        Console.WriteLine(totalGuests);
    }
}
 
// This code is contributed by ihritik

PHP

<?php
// PHP program to get the
// total number of guests at the party
 
     
// Function to find the totalGuests
function findGuest($array, $N)
{
    // Total guest before the party are 0
    $count = 0;
 
    // Checking requirements for each guest
    for ($i = 0; $i < $N; $i++)
    {
 
        // If requirements are met
        if ($array[$i] <= $count)
        {
 
            // The Gi guest decides to stay
            // So increment total guest by 1
            $count++;
        }
    }
 
    // Return the totalnumber of guest
    return $count;
}
 
 
// Driver code
 
// Get the number of guests invited
$N = 5;
 
// Guests array stores
// the requirement by each Guest
$guests = array(1, 0, 2, 1, 3 );
 
// Get the total number of guests present
$totalGuests = findGuest($guests, $N);
 
echo $totalGuests;
 
// This code is contributed by ihritik
 
?>

Javascript

<script>
 
// javascript program to get the
// total number of guests at the party   
 
// Function to find the totalGuests
    function findGuest(array , N) {
        // Total guest before the party are 0
        var count = 0;
 
        // Checking requirements for each guest
        for (i = 0; i < N; i++) {
 
            // If requirements are met
            if (array[i] <= count) {
 
                // The Gi guest decides to stay
                // So increment total guest by 1
                count++;
            }
        }
 
        // Return the totalnumber of guest
        return count;
    }
 
    // Driver code
     
 
        // Get the number of guests invited
        var N = 5;
 
        // Guests array stores
        // the requirement by each guest
        var guests = [ 1, 0, 2, 1, 3 ];
 
        // Get the total number of guests present
        var totalGuests = findGuest(guests, N);
 
        document.write(totalGuests);
 
// This code contributed by gauravrajput1
 
</script>
Producción: 

2

 

Complejidad de tiempo : O (N) donde N es el tamaño de la array de entrada dada

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por RuchaDeodhar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *