Can someone please help in edge case to problem — 1765N

Revision en2, by prakharmishra, 2025-02-17 15:27:29

Hello everyone, I was trying to solve this problem, 1765N - Number Reduction. I wrote a code and its logic is same as the editorial but rather than finding from digit '0' to '9' which is occurs first in the next 'k+1' elements in the number from left to right, I just searched the minimum among the 'k+1'.

My code works on almost all testcases except when the input is increasing, like n=1337 and k=2. Answer should be 13 but mine gives garbage value.

Here is my code,

My Code

I saw authors code solutions using stack to keep the smallest elements. Can someone provide me some corrections in my code, or will i need to implement stack solution?

Edit
Solved the edge case by removing elements from end when k!=0 after traversing through the number. But got TLE as the increasing sequence will always check for minimum from the next 'k+1' elements of each index, resulting in worst case O(n^2) solution. Maintaining stack is the most optimal way to keep the smallest numbers.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English prakharmishra 2025-02-17 15:27:29 346
en1 English prakharmishra 2025-02-17 13:27:31 2782 Initial revision (published)