Sherlock and Array (Hackerrank)
Sometimes it's better to stop and first formulate a way to solve a problem before jumping right into it. Why? Well because it gives you more time to better understand a problem and secondly, in some cases even a simpler way to solve a problem.
Remember kids! When solving algorithms, Math is your friend!
I realized that a little late into this question and when i did, it was just a 5 min snippet.
Question-
Approach - It's quite simple and straight forward.
Instead of dividing array into two halves, getting their sums, checking whether the sums are equal and trying to find out if an element which causes sum to be unequal exist or not, here's a better approach.
array - [5,6,8,11] , here we have 5+6=11 in LHS and 11 in RHS with 8 being an extra element.
Imagine LHS and RHS as same, ex - x+y+x= sum. i.e 2x= sum-y.
Here we just need to check if this equality holds, if it does we print YES and add value to x, else we print NO
Code-