You can just traverse the array and solve this problem by converting words to equation.
Base Case:
Time t=0
first person has patience A_1 (using 1 based indexing to explain).
As t<A1, person 1 is served. Due to this serving, the total time spent till now is advanced by B_{1}.
t = t + B_i
Step Case
We are at time t, the last person served is A_{j}. the next person k=j+1 is waiting in line if B_k-t>=0. If that is the case, we serve this person. Otherwise we increment k by 1 and repeat the condition check in a while loop. Once we find the first next person that can be served, we serve him/her and increment time again.
Time Complexity
O(N)
Sir, I am new to competitive coding can you please make it a little easier.
Guys, I am a complete newbie here. Not getting a thing.
If you feel so please take get some time to answer my questions
To start with , right now I only know about HTML and CSS that I use for WebDev. for Codechef however I have realised that I need to learn C / C++ / Python.
- What should be the correct sequence to learn the things (programming languages, DS-Algo etc) in order to be able solve the problems here & get rated good in future ?
- Does solving practice problems & getting them wrong display a bad picture of my profile ?
- Does solving the rated questions incorrectly deduct points ?
- How much & what should I practice till I become capable to enter rated contests ?
Whatever language you suggest me, please also tell majority of the topics that I need to cover to be confident to solve questions.
YES , I am a newbie here & you guys might make fun of me for that but I donâ€™t want to be one forever. Also whatever I learn I want to learn it completely & perfectly with no doubts in head. Neither do i want to cheat. So in the end,
- Please suggest me Proper sources to learn whatever you have suggested. whether it is books, Youtube Videos or Website.
Follow these steps to become good in CP.
- If you are new to programming, first learn C Language. You need to cover the following topics.
- Input and Output (scanf and printf statements).
- Syntaxes.
- Conditional statements (if, else if, else, switch)
- Loops (for loop, while loop, do while loop)
- Control statements (break, continue)
- Functions
- Functions with arguments, with return statements.
- Recursion
- Strings (character Arrays and Strings)
- Arrays
- Pointers (referencing and de-referencing)
- Dynamic Memory allocation (malloc, calloc, free)
- And much more advanced concepts (file handling, optimisation techniques (code optimisation, time optimisation, space optimisation).
Now, as you learn above topics, try to implement basic standard problems, that include:
- Printing Mathematical table.
- Sum of Array elements.
- Primality check.
- GCD and LCM of two numbers.
- GCD and LCM of array of integers.
- Fibonacci sequence. etc
- Searching an element in array( linear search, binary search).
- Sorting array of integers (Selection sort, Bubble sort).
- Factorisation in sqrt(N) time.
- Finding N factorial using Iteration and Recursion.
While intermediate topics include:
- Sorting array of integers (Insertion sort, Merge Sort, Quick Sort, Heap Sort).
- Fast Exponentiation, Modular Exponentiation.
- Sieve of Eratosthenes.
- Eulerâ€™s Totient Function.
- Factorisation in Log(N) time.
- Fermatâ€™s little theorem.
- nCr mod p using Fermatâ€™s Principle.
- Stacks, Queues, Linked Lists.
etc etc.
And Advanced topic include:
- Segment trees.
- BIT (Binary Indexed Trees).
- Greedy Problems.
- Divide and Conquer
- Dynamic Programming.
- Back Tracking.
- Graph Problems.
- Trees.
- Sets
- Hashing.
etc etc.
After you become thorough in topics of C ( from 1 to 13), start learning CPP. CPP is quite similar to C, so it will not take much time to learn.
WHY LEARN CPP?
- CPP comes with STL (Standard Template Library), using which Data Structures can be implemented easily. Also, it is the most used language for Competitive Programming.
Remember, if you want to implement a solution to a Problem, you need a Language. The more topics you cover, the more youâ€™ll be benefited.
After you learn C and while you are learning CPP, donâ€™t forget to check out CP Algorithms..
To start with CP, you should start with basic problems in Hackerrank.
Hackerrank is the best place for beginners.
After you solve around 20 or more problems in Hackerrank, try solving Codechef Practice problems, in the order they appear.
After solving about 40 problems, then participate in Contests.
Codechef has 3 Regular Rated Contests every month. Hereâ€™s the link where you can get details.
PS: PRACTICE IS REALLY IMPORTANT. THE MORE YOU PRACTICE, THE FASTER YOUâ€™LL IMPROVE.
Happy Coding
this problem is quite easy but you canâ€™t just ask for the answer, please mention where you are stuck and what approaches youâ€™ve tried yet.
you see at line number 7 you are incrementing the value of â€śtâ€ť even before checking for the first element, the first element will always process as its already at the counter window.
look at first iteration at i=0 youâ€™ll skip a[i] here as t=3 and a[i]==1 even though a[0] is already at the counter you are counting it as removed.
let me upload the solution as well so youâ€™ll get some clarity.!
i havenâ€™t checked for the edge cases in my code. but you now have the idea
Screenshot 2021-01-24 143323|651x438
thnx bro for your help, got itâ€¦