📘
Placecom Question
  • ✅ABC? (Contest)
  • ✅Activity Selection (Contest)
  • ✅Absolute value discrepancy (Contest)
  • ✅Akash's Contest (Contest)
  • ✅Alternate Matrix Addition(Contest)
  • ✅Anti clockwise(Contest)
  • ✅Arpit's toy (Contest)
  • ✅Array Frequency Paradigm (Contest)
  • ✅Array Games (Contest)
  • ✅Arranging Students (Contest)
  • ✅Array Proximity (Contest)
  • ✅ATM Machine (Contest)
  • ✅Awesome Numbers(Contest)
  • ✅Bad Dish (Contest)
  • ✅Balanced Neighborhood (Contest)
  • ✅Birthday Gift (Contest)
  • ✅Boundary Traversal of Matrix(Contest)
  • ✅Buy and Sell Stock(Contest)
  • ✅Cakewalk? (Contest)
  • ✅Can we sort? (Contest)
  • ✅Candy Love (Contest)
  • ✅Candy Shopping (Contest)
  • ✅Choose Card optimally(Contest)
  • ✅Candy Store Earnings (Contest)
  • ✅Choose points (Contest)
  • ✅Concatenate Strings (Contest)
  • ✅Common digits in two numbers (Contest)
  • ✅Count 1's in binary array(Contest)
  • ✅Count Numbers (Contest)
  • ✅Count Total Digits in a Number (Contest)
  • ✅Counting Zeroes to Ones (Contest)
  • ✅Cyclic Rotation Paradigm (Contest)
  • ✅Derrangement Exercise (Contest)
  • ✅Diagonal Sum(Contest)
  • ✅Diet planning (Contest)
  • ✅Difference Array (Contest)
  • ✅Digits Rearrangement (Contest)
  • ❌Divisible by 8 (Contest)
  • ✅Door problem (Contest)
  • ✅Duplicates at a distance k (Contest)
  • ✅Easy - Peasy (Contest)
  • ✅Eat or be Eaten(Contest)
  • ✅Factorial - Recursion(Contest)
  • ✅Find element in 2D array (Contest)
  • ✅Generate all parentheses(Contest)
  • ✅The gabba test(Contest)
  • ✅Fruit Market (Contest)
  • ✅Frequency Sort (Contest)
  • ✅Floor in a Sorted Array(Contest)
  • ✅First non-repeating character in a String(Contest)
  • ✅Find unique(Contest)
  • ✅Get the Shadow (Contest)
  • ✅Good cells (Contest)
  • ✅Good circular array (Contest)
  • ✅Grid Magic (Contest)
  • ✅Guardians of Galaxy(Contest)
  • ✅Happy Balloons (Contest)
  • ✅Help James (Contest)
  • ✅Help Samar with Chopsticks (Contest)
  • ✅Hip Hip Array (Contest)
  • ✅Increment- Decrement Philosophy (Contest)
  • ✅Infinity Stones : Form Black Order - The Army of Thanos (Contest)
  • ✅Insert Operator(Contest)
  • ✅Integer Modification (contest)
  • ✅Inversion of array(Contest)
  • ✅Iso Lexo String (Contest)
  • ✅Jumping Numbers (Contest)
  • ✅K closest points(Contest)
  • ✅Knight game(Contest)
  • ✅K-Pairs (Contest)
  • ✅Kth Row of Pascal's Triangle(Contest)
  • ✅Largest Bitonic Subarray(Contest)
  • ✅Least Subarrays(Contest)
  • ✅Lexographical Rotation (Contest)
  • ✅Lone Sum Supremacy (Contest)
  • ✅Lucky Boys(Contest)
  • ❌Majority Element(Contest)
  • ✅Matrix problem(Contest)
  • ✅Matrix ZigZag Traversal(Contest)
  • ✅Max Candies(Contest)
  • ✅Max permute (Contest)
  • ✅Max Score in Quiz (Contest)
  • ✅Max sum column (Contest)
  • ✅Max XOR(Contest)
  • ❌Maximize Strength (Contest)
  • ✅Maximizing Difference(Contest)
  • ✅Maximum Area(Contest)
  • ❌Maximizing Difference(Contest)
  • ✅Maximum Force(Contest)
  • ✅Longest subarray not having more than K distinct elements(Contest)
  • ✅Maximum value of difference of a pair of elements and their Index(Contest)
  • ✅Minimum adjacent difference in a circular array easy(Contest)
  • ✅Minimum Element in Sorted and Rotated Array(Contest)
  • ✅Minimum operation - II(Contest)
  • ✅Missing two(Contest)
  • ✅Mohit and array(Contest)
  • ✅Most occurring elements(Contest)
  • ✅Move Zeros(Contest)
  • ✅Moving right (Contest)
  • ✅Max Score in Quiz (Contest)
  • ✅N integers: Easy (Contest)
  • ✅Nth node from end of linked list(Contest)
  • ✅Number of distinct numbers(Contest)
  • ✅Odd Sum Array (Contest)
  • ✅Optimal Goodies (Contest)
  • ✅Orange or Chocolate Candy? (Contest)
  • ✅Packing Rectangles (Contest)
  • ✅Pair sum (Contest)
  • ✅Passcode (Contest)
  • ✅Permutation - 2 (Contest)
  • ✅Polynomial equation(Contest)
  • ✅Power function(Contest)
  • ✅Power of Three(Contest)
  • ✅Print Pattern (Contest)
  • ✅Reduce to 1 (Contest)
  • ✅Remove Duplicates Inplace(Contest)
  • ✅Repeating numbers (Contest)
  • ✅Replace element(Contest)
  • ✅Ropes (Contest)
  • ✅Row Index Identification (Contest)
  • ✅Row with maximum 1's(Contest)
  • ✅Sara and Monsters (Contest)
  • ✅Science Camp (contest)
  • ✅Score bar(Contest)
  • ✅Search in rotated sorted array(Contest)
  • ✅Searching an element in a sorted array(Contest)
  • ✅Separating Negative and Positive numbers(Contest)
  • ❌Sequence Formation (Contest)
  • ❌Setwet's Fish Pond(Contest)
  • ✅Shopping (Contest)
  • ✅Simple Circle (Simple Contest)
  • ✅Simple Pairs(Contest)
  • ✅Simple Transpose (Contest)
  • ✅Skit Video (Contest)
  • ✅Smaller elements (Contest)
  • ✅Sort it (Contest)
  • ✅Special digit sum (Contest)
  • ✅Spiral rotation(Contest)
  • ✅Squiggly brackets (Contest)
  • ✅Strange element (Easy-Version)
  • ✅Subarray with given sum(Contest)
  • ✅Sum of largest elements(Contest)
  • ✅Sum Up(Contest)
  • ✅Swapping Matrix (Contest)
  • ✅Sweet Bunty (Contest)
  • ✅Teacher(Contest)
  • ✅The EndGame : Concatenated Words(Contest)
  • ✅The high median paradigm (Contest)
  • ✅Tower of Hanoi(Contest)
  • ✅Transpose of a matrix(Contest)
  • ✅Triangular matrix (Contest)
  • ✅Two Sum Maximization(Contest)
  • ✅Ultron : Vibranium Quest(Contest)
  • ✅Wakandan Point in Unsorted Array(Contest)
  • ✅Yet again Partition sort problem (Contest)
  • ✅Yet Another Array Rearrangement Problem (Contest)
  • ✅Zero Padding(Contest)
  • ✅First & Last (Contest)
  • ❌Fast Search (Contest)
Powered by GitBook
On this page

Door problem (Contest)

Door problem hard Time Limit: 2 sec Memory Limit: 128000 kB

PreviousDivisible by 8 (Contest)NextDuplicates at a distance k (Contest)

Last updated 2 years ago

Problem Statement :

There is a door at Newton School that can be used only by one person at a time i. e either a person can enter from the door or exit but no two people can do it simultaneously. If two people going in the opposite direction arrived at the door at the same time then these 3 cases should be considered:-

  1. If the door was not used before or it was not used in the previous second then the person who wants to exit goes first.

  2. If the door has been used in the previous second for entering, then the person who wants to enter goes first.

  3. If the door has been used in the previous second for exiting, then the person who wants to exist goes first.

If two people arrive at the same time and go in the same direction then the person whose name in the given list comes first will go first.

Note:- To cross the door, it will take exactly one second for each person. Input The first line of input contains a single integer N containing the number of people The second line of input contains N space-separated integers depicting the arrival time of the ith person. The last line of input containing N space-separated integers which are either 0 or 1. 0 indicates that the person wants to enter and 1 indicates he wants to exit.

Constraints:- 1 ≤ N ≤ 50000 0 ≤ Arrival[i] ≤ Arrival [i+1] ≤ 109 Output Print N space-separated integers denoting the time at which the ith person will cross the door. Example Sample Input 1:- 4 0 0 1 5 0 1 1 0

Sample Output 1:- 2 0 1 5

Sample Input 2:- 5 0 1 1 3 3 0 1 0 0 1

Sample Output 2:- 0 2 1 4 3

Explanation:- At t = 0:- the first and the second person wants to enter and exit. As per the case, 1 2nd person will go first. At t=1, the first and the 3rd person wants to enter and exit. As per case 3, the 3rd person goes first. At t= 2, only 1st is the only person standing so he goes. At t = 3, and 4 no one wants to cross At t = 5, the 4th person is the only one who wants to cross.

link:

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
        // Your code here
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] time = new int[n];
        int[] direction = new qint[n];
        List<Map<Integer, Integer>> entryList = new ArrayList();
        List<Map<Integer, Integer>> exitList = new ArrayList();
        for(int i=0;i<n;i++){
            time[i] = sc.nextInt();
        }
        for(int i=0;i<n;i++)
        {
            direction[i]=sc.nextInt();
            Map<Integer, Integer> map =new HashMap();
            map.put(time[i],i);
            if(direction[i]==0){
                entryList.add(map);
            }
            else
            {
                exitList.add(map);
            }
        }
        int[] res = new int[n];
        Arrays.fill(res, -1);
        int i=0, j=0, t=0, entryTime=0, exitTime=0;
        boolean prevExit = true;
        while(entryList.size()!= i || exitList.size()!= j)
        {
            int s = 0, e= 0;
            if(entryList.size() == i)
            {
                entryTime = Integer.MAX_VALUE;
            }
            else
            {
                for(Map.Entry<Integer, Integer> entry : entryList.get(i).entrySet()){
                    s = entry.getKey();
                }
                entryTime = s;

            }
            if(exitList.size() == j)
            {
                exitTime = Integer.MAX_VALUE;
            }
            else{
                for(Map.Entry<Integer, Integer> entry : exitList.get(j).entrySet()){
                    e = entry.getKey();
                }
                exitTime = e;
            }
            if(entryTime < t) entryTime = t;
            if(exitTime < t ) exitTime = t;
            if(entryTime == exitTime){
                if(entryTime == t){
                if(prevExit){
                    res[exitList.get(j).get(e)]= exitTime;
                    j++;
                    t= exitTime + 1;
                }
                else{
                    res[entryList.get(i).get(s)] = entryTime;
                    i++;
                    t = entryTime + 1;
                }
            }
            else{
                res[exitList.get(j).get(e)] = exitTime;
                j++;
                t=exitTime +1;
                prevExit = true;
            }
        }
        else{
            if(entryTime < exitTime){
                res[entryList.get(i).get(s)] = entryTime;
                i++;
                t = entryTime + 1;
                prevExit = false;
            }
            else{
                res[exitList.get(j).get(e)] = exitTime;
                j++;
                t = exitTime + 1;
                prevExit = true;
            }
        }
    
        }
    for(int value : res)
        System.out.print(value + " ");
    }
}
✅
https://my.newtonschool.co/playground/code/cub4n2n67ar0