1

What would be a divide-and-conquer algorithm to check if there’s a subarray say A[i : j] of A such that A[i] + . . . + A[j] = target value. What would be a good way to go about designing an algorithm like this that would print out the indices of the sub-array?

The elements in the array are signed, i.e. both positive and negative values are allowed.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
Imd15
  • 31
  • 6
  • 2
    Keep a running total, if the total is too small add the next right element, if the total is too big drop the most left element, if the total matches the target then done. If the elements and/or target value can be positive or negative, then need a different approach. – Eljay Sep 26 '19 at 15:28
  • @IDm What makes you think that divide-and-conquer is the way to go? – Support Ukraine Sep 26 '19 at 15:37
  • @4386427 The issue is that im trying to get it faster than O(n^2) so by doing a brute force as Eljay described would give me a O(n^3) which I am trying to avoid as it will take too long. so divide and conquer I felt would be the best approach – Imd15 Sep 26 '19 at 15:40
  • 2
    @IDm Eljay approach is linear for positive array elements – MBo Sep 26 '19 at 15:45
  • Eljay provides a solution in O(n). is it not a brute force approach. – Jarod42 Sep 26 '19 at 15:48
  • They are signed – Imd15 Sep 26 '19 at 15:54
  • @4386427 they are signed – Imd15 Sep 26 '19 at 15:54
  • @Jarod42 they are signed, so they contain negative numbers aswell – Imd15 Sep 26 '19 at 15:54
  • 2
    @IDm I edited your question to add that information. Next time: Instead of writing it in a comment, edit the question :-) – Support Ukraine Sep 26 '19 at 15:56
  • @4386427 youre a superstar thank you. please let me know if there are any other things I need to clear up – Imd15 Sep 26 '19 at 15:57
  • 2
    @IDm I don't know an algo that can make this better than O(n^2) (which is easy to achieve). Maybe by calculating and storing intermediate results, it can be done. But... how big arrays are we talking about? And how much memory are you willing to pay for improved performance? – Support Ukraine Sep 26 '19 at 16:06
  • Calculate cumulative sums for array, sort them, then look for needed difference with two-pointer algorithm described by @Eljay. Complexity is `O(nlogn)` – MBo Sep 26 '19 at 16:14
  • @4386427 These arrays can have up to 100,000. how would you go about getting it to do O(n^2)? – Imd15 Sep 26 '19 at 16:16
  • @MBo: But care with "inverted" indexes. – Jarod42 Sep 26 '19 at 16:16
  • @MBo I could try that. calclating the cumulitive sums – Imd15 Sep 26 '19 at 16:16
  • @IDm O(n^2) is simply achieved by just calculating all sub-array, i.e. [0:N], [1:N], [2:N], .... During calculation, you always check if target was hitted. That will give a maximum of N*(N-1)/2 loops, i.e. O(n^2). It's just two nested for-loops. – Support Ukraine Sep 26 '19 at 16:18
  • It's a fine question – but as no real code involved, possibly not suited for this site? Maybe rather [software engineering](https://softwareengineering.stackexchange.com/)? – Aconcagua Sep 26 '19 at 16:29
  • @Aconcagua AFAIK questions about sw algorithms are in-scope for SO as long as the problem is clearly stated. – Support Ukraine Sep 26 '19 at 16:52
  • @Prune That might be exactly what I was looking for. I really appreciate your help – Imd15 Sep 26 '19 at 22:25
  • I appreciate everyone's help. thank you :) – Imd15 Sep 26 '19 at 22:26

0 Answers0