Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive.
Note: A naive algorithm of $O(n^2)$ is trivial. You MUST do better than that.
// to get start while (b[i] - a[start] >= wlower) { start++; }
// to get end while (b[i] - a[end] > wupper) { end++; }
sum += start - end; }
return sum; }
Count of Smaller Numbers After Self
You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].
Example:
1 2 3 4 5 6
Given nums = [5, 2, 6, 1]
To the right of 5 there are 2 smaller elements (2 and 1). To the right of 2 there is only 1 smaller element (1). To the right of 6 there is 1 smaller element (1). To the right of 1 there is 0 smaller element.
var countPrimes = function(n) { // if modified to 'var hash = []' // it will be Memory Limit Exceeded // I don't know why // I think both are the same var hash = newArray(n) , a = Math.sqrt(n);
for (var i = 2; i <= a; i++) { if (!hash[i]) for (var j = i * i; j < n; j += i) hash[j] = true; }
var ans = 0; for (var i = 2; i < n; i++) if (!hash[i]) ans ++;