New answers tagged arrays
0
Assume elements start at index 0. N is odd.
Let mid be an even number close to the middle of the array. Compare a[mid-1] and a[mid]. There is an odd number of elements before mid-1 and an even number after mid.
If both numbers are equal then the unique one is in the left half < mid-1. If they are not equal then the unique one is in the right half ...
1
This is a classical problem when the array isn't sorted, which has a surprising, almost magical linear solution.
But here, with a sorted array, your idea is great.
The unique element will have an index $u$, and every element $a[i]$ will be exactly the same as either $a[i-1]$ or $a[i+1]$ (which are different).
Assuming that arrays start at $0$, the shape of ...
0
here, you are multiplying the depth of the "parent" array, instead of the nested array.
sum+=(depth)* productSum(array[i],depth+1)
try this
sum+=(depth+1)* productSum(array[i],depth+1)
multiply it by depth+1
0
The data structure itself is unambiguously called a contiguous array. Depending on the context and language I would call it "initialized" or "having only non-null elements" or similar to describe the contents.
Top 50 recent answers are included
Related Tags
arrays × 458algorithms × 260
data-structures × 81
sorting × 58
time-complexity × 52
algorithm-analysis × 34
search-algorithms × 26
runtime-analysis × 23
optimization × 22
dynamic-programming × 19
lower-bounds × 12
amortized-analysis × 12
maximum-subarray × 12
heaps × 11
complexity-theory × 10
efficiency × 10
matrices × 10
terminology × 9
binary-search × 9
subsequences × 9
linked-lists × 9
randomized-algorithms × 8
searching × 8
asymptotics × 7
sets × 6