It's just the basics!!


I was surely underestimating the basics of DSA. It was me who thought that let me just start from stack itself, I already know ‘basics‘, but boy, was I wrong!
It so happened that after finally gathering all my courage and remaining dignity that I ultimately started DSA by basics….This time I had it all managed- the resources, the tools, which language to work on, how to proceed and everything (thanks to the free resources available in gfg by nation skillup). All I had to do was start and thank goodness, I did start! Otherwise, who knew when I would have come out of studying slump!
The course started with mathematics, which had easy concepts like prime, odd or even, gcd,lcm and so on…TBH, I was enjoying and already in the flow and I even started with solving the problems….Felt easy and powered that Yeah, I was right, I don’t need to do focus that much on basics- I know it all….
Alas!! I’m Not Hemione.
I came across this next problem statement regarding the floor and ceil of an integer which I admit, was difficult as I couldn’t think of all possible test cases but lucky for me they had the solution in their article which I had read the day before. But, did I read it thoroughly enough to remember its logic? Nope never! Eventually after understanding the algorithm, I did understand its logic and worked through to get all test cases correct.
class Solution {
public ArrayList<Integer> divFloorCeil(int a, int b) {
// code here
int res=a/b;
int ceil=res,floor=res;
if((a^b)>0 && a%b!=0)
ceil=res+1;
if((a^b)<0 && a%b!=0)
floor=res-1;
ArrayList <Integer> arr=new ArrayList<Integer>();
arr.add(floor);
arr.add(ceil);
return arr;
}
}
I was like okay! no biggie let’s jump on the next problem. I did 2 after I was again hit by an iceberg which was definitely deep inside the water! The question was to find the closes number of the division.
this was the question
I started and tried my best to figure out its logic but sadly it was failing through the various test cases. So, I looked up the google - Wait, Hey? We are in the era of ai- ask the ai girl!!?
So, I asked copilot my bestie in terms of coding questions and did helped me build the logic and worked around it to make sure it is test case proof and after 50 minutes of writing and testing, I passed all the test cases. But, I am not an easy person who would just accept her win like this. I was still not statsfied because my code was looking bulky, so want on to try an optimized solution. With the help of their inbuilt bot, I realised that I could have worked using modulo operator. So, I did. The basic structure of the logic was ready but again, it was not durable to handle all test cases. I tried a lot to analyse the code along with multiples of test cases and managed to finally get it correct after spending another 50 minutes :>
The code it turned out in the end was actually so good with time complexity of O(1) and was so small considering the previous one.
To conclude, I did learn my lesson - NEVER UNDERESTIMATE THE BASICS! Because if they are not clear how do you expect to go ahead and deal with concepts like queue or tree!? This was my work for this weekend, now will try to get done with the rest of chapter in the weekdays(Hehe.. leasson learnt 😅).
// My optimized code for the question
class Solution {
static int closestNumber(int n, int m) {
int rem=n%m;
int op1=n-rem;
int op2=((m^n)<0)?op1-m:op1+m;
int d1=Math.abs(op1-n);
int d2=Math.abs(op2-n);
if(d1==d2)
{
return (Math.abs(op1)>Math.abs(op2))?op1:op2;
}
return (d1>d2)?op2:op1;
}
}
Subscribe to my newsletter
Read articles from reverieInLoop directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

reverieInLoop
reverieInLoop
I'm reverieinLoop, a mind captivated by the craft of coding. On this anonymous journey, I'm charting my path through the world of code, guided by the belief that logic and creativity are inseparable. Follow along as I unravel technical challenges, share new discoveries, and stitch together thoughts on building with intention.