Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 18, 2016 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/a05ab342cf882989c94197e7ca2dc9bd to your computer and use it in GitHub Desktop.
Save jianminchen/a05ab342cf882989c94197e7ca2dc9bd to your computer and use it in GitHub Desktop.
HackerRank university codesprint - advanced algorithm - trace log to understand the algorithm
w[p, sum, diffsum] is not true w[0,0,0]
For loop (A - E): for(; i<=s;i++) i=0 s=3
iterate on i =0
A: review sum, diffsum 0 0
B: newSum, newDiffSum 0 0
C: i, n, p i=0 n=3 p=0
D-set A[p]=i, A[p]=0 where p=0 and i=0
D-Normal: loop start to end: From 0 to 3 current i = 0 and arguments:0 0 1
E: Array A is [0,0,0]
F: recursive call construct - arguments: (A,0,0,1)
w[p, sum, diffsum] is not true w[1,0,0]
i=A[p-1], p=1, i=0;
For loop (A - E): for(; i<=s;i++) i=0 s=3
iterate on i =0
A: review sum, diffsum 0 0
B: newSum, newDiffSum 0 0
C: i, n, p i=0 n=3 p=1
D-set A[p]=i, A[p]=0 where p=1 and i=0
D-Normal: loop start to end: From 0 to 3 current i = 0 and arguments:0 0 2
E: Array A is [0,0,0]
F: recursive call construct - arguments: (A,0,0,2)
w[p, sum, diffsum] is not true w[2,0,0]
i=A[p-1], p=2, i=0;
For loop (A - E): for(; i<=s;i++) i=0 s=3
iterate on i =0
A: review sum, diffsum 0 0
B: newSum, newDiffSum 0 0
C: i, n, p i=0 n=3 p=2
D-set A[p]=i, A[p]=0 where p=2 and i=0
D-Normal: loop start to end: From 0 to 3 current i = 0 and arguments:0 0 3
E: Array A is [0,0,0]
F: recursive call construct - arguments: (A,0,0,3)
p==n return 0 whereas p=3 n=3
iterate on i =1
A: review sum, diffsum 0 0
B: newSum, newDiffSum 1 2
C: i, n, p i=1 n=3 p=2
D-set A[p]=i, A[p]=1 where p=2 and i=1
D-Normal: loop start to end: From 0 to 3 current i = 1 and arguments:1 2 3
E: Array A is [0,0,1]
F: recursive call construct - arguments: (A,1,2,3)
p==n return 0 whereas p=3 n=3
iterate on i =2
A: review sum, diffsum 0 0
B: newSum, newDiffSum 2 4
C: i, n, p i=2 n=3 p=2
D-set A[p]=i, A[p]=2 where p=2 and i=2
D-Normal: loop start to end: From 0 to 3 current i = 2 and arguments:2 4 3
E: Array A is [0,0,2]
F: recursive call construct - arguments: (A,2,4,3)
p==n return 0 whereas p=3 n=3
iterate on i =3
A: review sum, diffsum 0 0
B: newSum, newDiffSum 3 6
C: i, n, p i=3 n=3 p=2
D-Exception: newSum, newDiffSum are bigger than s, k 3 6 exit for loop.
return 0
iterate on i =1
A: review sum, diffsum 0 0
B: newSum, newDiffSum 2 2
C: i, n, p i=1 n=3 p=1
D-set A[p]=i, A[p]=1 where p=1 and i=1
D-Normal: loop start to end: From 0 to 3 current i = 1 and arguments:1 1 2
E: Array A is [0,1,2]
F: recursive call construct - arguments: (A,1,1,2)
w[p, sum, diffsum] is not true w[2,1,1]
i=A[p-1], p=2, i=1;
For loop (A - E): for(; i<=s;i++) i=1 s=3
iterate on i =1
A: review sum, diffsum 1 1
B: newSum, newDiffSum 2 2
C: i, n, p i=1 n=3 p=2
D-set A[p]=i, A[p]=1 where p=2 and i=1
D-Normal: loop start to end: From 1 to 3 current i = 1 and arguments:2 2 3
E: Array A is [0,1,1]
F: recursive call construct - arguments: (A,2,2,3)
p==n return 0 whereas p=3 n=3
iterate on i =2
A: review sum, diffsum 1 1
B: newSum, newDiffSum 3 4
C: i, n, p i=2 n=3 p=2
D-set A[p]=i, A[p]=2 where p=2 and i=2
D-Normal: loop start to end: From 1 to 3 current i = 2 and arguments:3 4 3
E: Array A is [0,1,2]
F: recursive call construct - arguments: (A,3,4,3)
sum == s && diffsum == k - find the solution!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment