Can Anyone help me with this problem(Code in Java)

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)

1 Like

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 :pray: :pray:

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.
    1. Input and Output (scanf and printf statements).
    2. Syntaxes.
    3. Conditional statements (if, else if, else, switch)
    4. Loops (for loop, while loop, do while loop)
    5. Control statements (break, continue)
    6. Functions
    7. Functions with arguments, with return statements.
    8. Recursion
    9. Strings (character Arrays and Strings)
    10. Arrays
    11. Pointers (referencing and de-referencing)
    12. Dynamic Memory allocation (malloc, calloc, free)
    13. 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:
  1. Printing Mathematical table.
  2. Sum of Array elements.
  3. Primality check.
  4. GCD and LCM of two numbers.
  5. GCD and LCM of array of integers.
  6. Fibonacci sequence. etc
  7. Searching an element in array( linear search, binary search).
  8. Sorting array of integers (Selection sort, Bubble sort).
  9. Factorisation in sqrt(N) time.
  10. Finding N factorial using Iteration and Recursion.

While intermediate topics include:

  1. Sorting array of integers (Insertion sort, Merge Sort, Quick Sort, Heap Sort).
  2. Fast Exponentiation, Modular Exponentiation.
  3. Sieve of Eratosthenes.
  4. Euler’s Totient Function.
  5. Factorisation in Log(N) time.
  6. Fermat’s little theorem.
  7. nCr mod p using Fermat’s Principle.
  8. Stacks, Queues, Linked Lists.
    etc etc.

And Advanced topic include:

  1. Segment trees.
  2. BIT (Binary Indexed Trees).
  3. Greedy Problems.
  4. Divide and Conquer
  5. Dynamic Programming.
  6. Back Tracking.
  7. Graph Problems.
  8. Trees.
  9. Sets
  10. 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 :grinning:

3 Likes

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.


My solutionScreenshot 2021-01-24 13.38.31

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…