Created
August 29, 2024 14:44
-
-
Save AashishNandakumar/f67fb6ad7b72b4008302f770f09e37c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int go(int[] a, int l, int r) { | |
| if (l == r) | |
| return a[l]; | |
| int mid = (l + r) / 2; | |
| return go(a, l, mid) + go(a, mid + 1, r); | |
| } | |
| int[] a = {...}; | |
| int N = a.length; | |
| int sum = go(a, 0, N - 1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static int go(int[] a, int l, int r) { | |
| if (l == r) | |
| return a[l]; | |
| int mid = (l + r) / 2; | |
| int sum = go(a, l, mid) + go(a, mid + 1, r); | |
| for (int i = l; i <= r; i++) | |
| sum += a[i]; | |
| return sum; | |
| } | |
| int[] a = {5, 7, 9}; | |
| int N = a.length; | |
| int result = go(a, 0, N - 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment