Hi guys.

I just did day1 of 2024 (I know, I’m late) this might be a dumb question but I reallydont understand why we should sort the two lists before substracting? I created some test cases with 10-20 items and calculated them manually, if you don’t sort the list and just add all list1 items and substract from the sum of list2, you get the same answer. But it doesn’t work with the large input file.

Let’s say list1 has elements (a,b,c,d) and list2 has (e,f,g,h).

(a-e)+(b-f)+(c-g)+(d-h) = a-e+b-f+c-g+d-h = (a+b+c+d)-(e+f+g+h)

So the sorting shouldn’t matter. Right? But it does. Am I too bad at basic math?

  • somegeekOP
    link
    fedilink
    arrow-up
    2
    ·
    20 hours ago

    You are right. Although the way I was talking about is |(2+10)-(1+20)|= 9 Seems like I coincidentally created some example lists that all worked with this method!

    And the algebraic reason would be the presence of parenthesis. |(a+b)-(c+d)| != |(a-c)|+|(b-d)|