Meeting Maximum Guests Problem – Java implementation

    Imagine a party. Many guests are coming. You want to calculate the maximum number of guests in the party hall at a particular time given to you.

How will you do this????

First, Get the inputs. The inputs are two array of integers given below.

‘arrive[]’ – This represents the guests who enters the party hall at that time.

‘depart[]’ – This represents the guests who leaves the party hall at that moment.

Note:

Always check both array’s length should be same in size.

Arrival entry should be entered in ‘arrive[]’ and Departure entry should be noted in ‘depart[]’.

Algorithm:

  • Get the inputs ‘arrive[]’ and ‘depart[]’;
  • Sort the arrays.
  • Let us declare two pointers ‘i’ and ‘j’. ‘i’ points arrive[] and ‘j’ points depart[].
  • Call the function finditMaxGuests()
  • This function check the condition to proceed.
  •   If arrive[i]<=depart[j] then, a guest came into the party hall. Increase the count of arrive[].
  •   Otherwise, decrease the count of depart and move on.
  • Finally, print the value of maximum guests in the party at a particular time.

Here, is the program.

import java.util.Arrays;

import java.util.Scanner;

public class MeetingMaxGuestsEg {

    public static void main(String[] args) {

         Scanner scanner1 = new Scanner(System.in);

          System.out.print("Enter number of guests arrived: ");

          int no = scanner1.nextInt();

          int[] arrive = new int[no];

          for (int i = 0; i < no; i++)

          {

           System.out.print("Enter the next one: ");

           arrive[i] = scanner1.nextInt();

          }

          Scanner scanner2 = new Scanner(System.in);

          System.out.print("Enter number of guests leaved: ");

          int no1 = scanner2.nextInt();

          int[] depart = new int[no];

          for (int i = 0; i < no1; i++)

          {

           System.out.print("Enter the next one: ");

           depart[i] = scanner2.nextInt();

          }

        System.out.println("Maximum number of guests at the party: " + finditMaxGuests(arrive, depart));

    }

     public static int finditMaxGuests(int[] arrive, int[] depart) {

        Arrays.sort(arrive);

        Arrays.sort(depart);

        int mGuests = 0;

        int cGuests = 0;

        int i = 0, j = 0;

        while (i < arrive.length && j < depart.length) {

            if (arrive[i] <= depart[j]) {

                cGuests++;

                mGuests = Math.max(mGuests, cGuests);

                i++;

            } else {

                cGuests--;

                j++;

            }

        }

        return mGuests;

    }

}

Output:

C:\raji\blog>javac MeetingMaxGuestsEg.java

C:\raji\blog>java MeetingMaxGuestsEg

Enter number of guests arrived: 10

Enter the next one: 1

Enter the next one: 3

Enter the next one: 5

Enter the next one: 7

Enter the next one: 9

Enter the next one: 11

Enter the next one: 15

Enter the next one: 2

Enter the next one: 4

Enter the next one: 6

Enter number of guests leaved: 4

Enter the next one: 6

Enter the next one: 3

Enter the next one: 45

Enter the next one: 34

Maximum number of guests at the party: 2

That’s all. The java program to find the maximum guests in the party is clearly written. Keep coding!!!

Different ways of Sorting using built in functions-Java Programming

     Sorting is a process of arranging elements in particular order. It may be ascending and descending order.

Here, two examples are given to sort the elements using built in functions.

  • ·       Arrays.sort()
  • ·       Custom comparators

Java Program to sort integers using ‘Arrays.sort()’:

              This program reads the integer array from user. It process it using ‘Arrays.sort()’ function.

Finally, it prints the sorted integers.

Program:

   import java.util.Arrays;

   import java.util.Collections;

   import java.util.Scanner;

   public class SortEg1 {

       public static void main(String[] args) {

          Scanner scanner = new Scanner(System.in);

          System.out.print("Enter number of elements: ");

          int no = scanner.nextInt();

          int[] integerArray = new int[no];

          for (int i = 0; i < no; i++)

          {

           System.out.print("Enter element: ");

           integerArray[i] = scanner.nextInt();

          }

           Arrays.sort(integerArray);

           System.out.println(Arrays.toString( integerArray));

       }

   }

Output:

C:\raji\blog>javac SortEg1.java

C:\raji\blog>java SortEg1

Enter number of elements: 5

Enter element: 45

Enter element: 56

Enter element: 9

Enter element: 23

Enter element: 34

[9, 23, 34, 45, 56]

This is a normal sorting.

If you want to sort the data in reverse manner, let us use the custom comparators.

Java Program to sort the string elements in reverse order:

              This program reads the number of String elements and input strings from the user. Using the custom comparator, it sorts in reverse order.

At last, it prints the data.

Program:

   import java.util.Arrays;

   import java.util.Comparator;

   import java.util.Scanner;

   public class CustomSortEg {

       public static void main(String[] args) {

          Scanner scanner = new Scanner(System.in);

          System.out.print("Enter number of Strings: ");

          int no = scanner.nextInt();

          String[] a = new String[no];

          for (int i = 0; i < no; i++)

          {

           System.out.print("Enter element: ");

           a[i] = scanner.next();

          }

           Arrays.sort(a, Comparator.reverseOrder());

           System.out.println("Sorting in reverse order");

           System.out.println(Arrays.toString(a));

       }

   }

Output:

C:\raji\blog>javac CustomSortEg.java

C:\raji\blog>java CustomSortEg

Enter number of Strings: 5

Enter element: car

Enter element: van

Enter element: bike

Enter element: truck

Enter element: bus

Sorting in reverse order

[van, truck, car, bus, bike]

That’s all. These are the ways to sort the elements using built in functions.