### Author Topic: (Challenge) Max Sum of Sub Array  (Read 300 times)

0 Members and 1 Guest are viewing this topic.

#### John Kaul (Se7en)

• Needs a day job
• Posts: 8863
##### (Challenge) Max Sum of Sub Array
« on: December 07, 2017, 09:52:34 am »
I found an old challenge that should be fun to revisit. This challenge was from a friend of mine (his post) but he is no longer a member here so I will post his words below instead of trying to find a link to his post. I found this in one of my old files with the description and my entry into the challenge (I will post my entry later).

Given the array { 1, 9, -6, 5, -9, 3, 2, -7, 4, -5, 8, -2, 6}

Write the most efficient function you can which returns the sum of the largest possible sub array value and that sub arrays start and end indexes. The array may not be re-ordered it must stay as is. This can be solved in O(n) or in regular language: a single pass over each element of the array.

This should test your algorithm writing abilities not your ability to use your favorite language.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

#### John Kaul (Se7en)

• Needs a day job
• Posts: 8863
##### Re: (Challenge) Max Sum of Sub Array
« Reply #1 on: December 07, 2017, 11:00:29 am »

EDIT: Note, this code submission is incorrect.
Below was the code I had for the answer to the challenge Cornbread posted.
Code - Auto/Visual Lisp: [Select]
`(defun MaxSumofSubArray (arr / item inx-start inx-end cntr)  (setq cntr 0        item (car arr))  (mapcar    '(lambda (x)       (if (> x item)         ;; if we have a bigger number then the one stored already,         (setq inx-start               ;; find the start index, but...               (if (= cntr 0)                 ;; if the item is the first item in the list, then                 ;; dont go lower then zero                 0                 (1- cntr) )               inx-end               ;; find the end index, but...               (if (eq cntr (1- (length arr)))                 ;; if the item is the last item in the list, then                 ;; dont go any higher then the length of the list                 (1- (length arr))                 (1+ (1+ cntr)) )               item x               cntr (1+ cntr)               )         ;; if we have a smaller number, then the one we have stored         (setq cntr (1+ cntr))         )        )    arr    ) (list item inx-start inx-end))`

EDIT: This return value is incorrect.
Code: [Select]
`(MaxSumofSubArray '(1 9 -6 5 -9 3 2 -7 4 -5 8 -2 6))(9 0 3)`
EDIT: The correct return value should be: 12 10 12

« Last Edit: December 08, 2017, 08:05:13 am by John Kaul (Se7en) »
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

#### roy_043

• Water Moccasin
• Posts: 1513
##### Re: (Challenge) Max Sum of Sub Array
« Reply #2 on: December 08, 2017, 05:53:47 am »
I may have misunderstood, but shouldn't the answer be:
Code: [Select]
`(10 0 1)`?

#### John Kaul (Se7en)

• Needs a day job
• Posts: 8863
##### Re: (Challenge) Max Sum of Sub Array
« Reply #3 on: December 08, 2017, 07:18:18 am »
I may not have had the correct answer saved in my old file but I remember us all writing several versions trying to find the right answer. It was a really fun and confusing challenge (that much I remember). This came from a professional CS, so I wouldn't expect it to be that easy.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

#### roy_043

• Water Moccasin
• Posts: 1513
##### Re: (Challenge) Max Sum of Sub Array
« Reply #4 on: December 08, 2017, 07:32:51 am »
So what should the answer be?

#### John Kaul (Se7en)

• Needs a day job
• Posts: 8863
##### Re: (Challenge) Max Sum of Sub Array
« Reply #5 on: December 08, 2017, 07:45:18 am »
I'm trying to figure that out right now. Do you want just the answer or how I got that (my "key" so to speak) as well?
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

#### John Kaul (Se7en)

• Needs a day job
• Posts: 8863
##### Re: (Challenge) Max Sum of Sub Array
« Reply #6 on: December 08, 2017, 07:46:28 am »
The correct answer/return value should be: 12 10 12
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

#### John Kaul (Se7en)

• Needs a day job
• Posts: 8863
##### Re: (Challenge) Max Sum of Sub Array
« Reply #7 on: December 08, 2017, 08:02:46 am »
Now I understand the problem again I need to work on my code and I already see that I am missing a big chunk of it. I will try and get to redoing my code throughout the day.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

#### roy_043

• Water Moccasin
• Posts: 1513
##### Re: (Challenge) Max Sum of Sub Array
« Reply #8 on: December 08, 2017, 10:05:28 am »
OK. The answer should indeed be (12 10 12).

Here is my suggestion:
Code - Auto/Visual Lisp: [Select]
`; (Roy_MaxSumofSubList '(1 9 -6 5 -9 3 2 -7 4 -5 8 -2 6))    => (12 10 12); (Roy_MaxSumofSubList '(1 9 -6 5 -9 3 2 -7 4 -5 8 -2 12 6)) => (24 10 13)(defun Roy_MaxSumofSubList (lst / idx ret sub tmp)  (setq idx 0)  (repeat (length lst)    (setq sub (reverse lst))    (repeat (1- (length sub))      (if (> (setq tmp (apply '+ sub)) (car ret))        (setq ret (list tmp idx (+ idx (length sub) -1)))      )      (setq sub (cdr sub))    )    (setq idx (1+ idx))    (setq lst (cdr lst))  )  ret)`

#### roy_043

• Water Moccasin
• Posts: 1513
##### Re: (Challenge) Max Sum of Sub Array
« Reply #9 on: December 08, 2017, 02:55:06 pm »
Another:
Code - Auto/Visual Lisp: [Select]
`(defun Roy_MaxSumofSubList_2 (lst / idx ret tmp)  (setq tmp (setq ret (list (car lst) 0 0)))  (setq idx 0)  (foreach itm (cdr lst)    (setq idx (1+ idx))    (cond      ((not tmp)        (setq tmp (list itm idx idx))      )      ((>= 0 (+ itm (car tmp)))        (setq tmp nil)      )      (T        (if (and (minusp itm) (> (car tmp) (car ret))) (setq ret tmp))        (setq tmp (list (+ itm (car tmp)) (cadr tmp) idx))      )    )  )  (if (> (car tmp) (car ret)) tmp ret))`

#### John Kaul (Se7en)

• Needs a day job
• Posts: 8863
##### Re: (Challenge) Max Sum of Sub Array
« Reply #10 on: December 08, 2017, 03:59:29 pm »
Nice ones!

I haven't been able to do much today (nose deep in specifications all day) after I initially tried to modify my code; I broke it several times just trying to add simple changes (boy, am I rusty!).
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

#### Lee Mac

• Seagull
• Posts: 11854
• AutoCAD 2015 Windows 7 London, England
##### Re: (Challenge) Max Sum of Sub Array
« Reply #11 on: December 08, 2017, 04:23:40 pm »
Another:
Code - Auto/Visual Lisp: [Select]
`(defun Roy_MaxSumofSubList_2 (lst / idx ret tmp)    < ... >)`

Very good Roy

#### VovKa

• Swamp Rat
• Posts: 873
• Ukraine
##### Re: (Challenge) Max Sum of Sub Array
« Reply #12 on: December 08, 2017, 05:38:09 pm »
Code: [Select]
`(Roy_MaxSumofSubList_2 '(2 -2 -2 3))`

#### Lee Mac

• Seagull
• Posts: 11854
• AutoCAD 2015 Windows 7 London, England
##### Re: (Challenge) Max Sum of Sub Array
« Reply #13 on: December 08, 2017, 06:36:14 pm »
Quick fix could be:
Code - Auto/Visual Lisp: [Select]
`(if (< 0 itm) (setq tmp (list itm idx idx)))`

Well spotted VovKa.

#### roy_043

• Water Moccasin
• Posts: 1513
##### Re: (Challenge) Max Sum of Sub Array
« Reply #14 on: December 09, 2017, 05:15:36 am »

Improved versions of my functions (the first one would also fail with VovKa's test case):
Code - Auto/Visual Lisp: [Select]
`; (Roy_MaxSumOfSubList_1B '(1 9 -6 5 -9 3 2 -7 4 -5 8 -2 6))    => (12 10 12); (Roy_MaxSumOfSubList_1B '(1 9 -6 5 -9 3 2 -7 4 -5 8 -2 12 6)) => (24 10 13); (Roy_MaxSumOfSubList_1B '(2 -2 -2 3))                         => (3 3 3)(defun Roy_MaxSumOfSubList_1B (lst / idx ret sub tmp)  (setq idx 0)  (repeat (length lst)    (setq sub (reverse lst))    (repeat (length sub)      (if (> (setq tmp (apply '+ sub)) (car ret))        (setq ret (list tmp idx (+ idx (length sub) -1)))      )      (setq sub (cdr sub))    )    (setq idx (1+ idx))    (setq lst (cdr lst))  )  ret) ; (Roy_MaxSumOfSubList_2B '(1 9 -6 5 -9 3 2 -7 4 -5 8 -2 6))    => (12 10 12); (Roy_MaxSumOfSubList_2B '(1 9 -6 5 -9 3 2 -7 4 -5 8 -2 12 6)) => (24 10 13); (Roy_MaxSumOfSubList_2B '(2 -2 -2 3))                         => (3 3 3)(defun Roy_MaxSumOfSubList_2B (lst / idx ret tmp)  (setq tmp (setq ret (list (car lst) 0 0)))  (setq idx 0)  (foreach itm (cdr lst)    (setq idx (1+ idx))    (cond      ((not tmp)        (setq tmp (list itm idx idx))      )      ((>= 0 (+ itm (car tmp)))        (setq tmp nil)      )      ((and (>= 0 (car tmp)) (> itm (car tmp)))        (setq tmp (list itm idx idx))      )      (T        (if (and (>= 0 itm) (> (car tmp) (car ret))) (setq ret tmp))        (setq tmp (list (+ itm (car tmp)) (cadr tmp) idx))      )    )  )  (if (> (car tmp) (car ret)) tmp ret))`