Interview Question
SDE1sCountry: United States
Can be solved in linear time.
Partition the array into a negative side and a positive side. The switch point can be found in O(n) by iterating from right to left until a negative value is found (or until we fall off the left end). It could even be found in O(log(n)) with a modified binary search, but it's not worth the hassle, since the rest of the algorithm is O(n). Let idx_negative be the index of the rightmost negative integer, or -1 if there are no negatives.
Now, perform a conceptual merge sort where one array is the subarray A[idx_negative+1..n-1] and the other array is the array that you'd get if you travel right to left on the negative side, taking the absolute value of each element along the way.
In other words, the problem reduces to a merge step of mergesort if you look at your array as 2 independent sorted arrays (where the negative array is traversed right to left after getting the absolute value of each element). To avoid counting the same number twice, keep track of the previous number that would have been inserted in the output array of mergesort - if the current number is equal, don't count it, otherwise, count it.
Implementation in C:
#include <stdio.h>
#include <assert.h>
unsigned abs_distinct_merge(int arr[], size_t arr_sz, int idx_positive, int idx_negative, int last) {
unsigned res = 0;
while (idx_negative >= 0 && idx_positive < arr_sz) {
int next;
if (-arr[idx_negative] < arr[idx_positive]) {
next = -arr[idx_negative];
idx_negative--;
} else {
next = arr[idx_positive];
idx_positive++;
}
if (next != last) {
res++;
}
last = next;
}
while (idx_negative >= 0) {
if (last != -arr[idx_negative]) {
res++;
}
last = -arr[idx_negative];
idx_negative--;
}
while (idx_positive < arr_sz) {
if (last != arr[idx_positive]) {
res++;
}
last = arr[idx_positive];
idx_positive++;
}
return res;
}
unsigned abs_distinct(int arr[], int arr_sz) {
int right_neg;
for (right_neg = arr_sz-1; right_neg >= 0 && arr[right_neg] > 0; right_neg--)
; /* Intentionally left blank */
int idx_positive = right_neg+1;
int idx_negative = right_neg;
int last;
if (idx_negative >= 0 && idx_positive < arr_sz) {
if (-arr[idx_negative] < arr[idx_positive]) {
last = -arr[idx_negative];
idx_negative--;
} else {
last = arr[idx_positive];
idx_positive++;
}
} else if (idx_negative >= 0) {
last = -arr[idx_negative];
idx_negative--;
} else if (idx_positive < arr_sz) {
last = arr[idx_positive];
idx_positive++;
} else {
assert(0);
}
return abs_distinct_merge(arr, arr_sz, idx_positive, idx_negative, last)+1;
}
static int array_buf[1024];
int main(void) {
printf("Enter array size, followed by the elements. Array must be sorted.\n");
printf("> ");
size_t array_sz;
while (scanf("%zu", &array_sz) == 1) {
assert(array_sz <= sizeof(array_buf)/sizeof(array_buf[0]));
size_t i;
for (i = 0; i < array_sz; i++) {
scanf("%d", &array_buf[i]);
}
unsigned distcount = abs_distinct(array_buf, array_sz);
printf("Absolute distinct count = %u\n", distcount);
printf("> ");
}
return 0;
}
Do you really need the search at the start? I believe this simple loop will work ( same idea though):
int absCount(std::vector<int> a) {
int count = 0;
for (int i = 0, j = a.size() - 1; i <= j;) {
if (std::abs(a[i]) < std::abs(a[j])) {
count++;
j--;
} else if (std::abs(a[i]) > std::abs(a[j])) {
count++;
i++;
} else {
count++;
i++;
j--;
}
}
return count;
}
Correct me if I'm wrong. But since, as you said, the array is broken up into two subarrays already, we don't actually need to find the midpoint. The fact that it exists is enough. Still O(n) but should be about 1.5x as fast.
Can be solved in linear time.
Partition the array into a negative side and a positive side. The switch point can be found in O(n) by iterating from right to left until a negative value is found (or until we fall off the left end). It could even be found in O(log(n)) with a modified binary search, but it's not worth the hassle, since the rest of the algorithm is O(n). Let idx_negative be the index of the rightmost negative integer, or -1 if there are no negatives.
Now, perform a conceptual merge sort where one array is the subarray A[idx_negative+1..n-1] and the other array is the array that you'd get if you travel right to left on the negative side, taking the absolute value of each element along the way.
In other words, the problem reduces to a merge step of mergesort if you look at your array as 2 independent sorted arrays (where the negative array is traversed right to left after getting the absolute value of each element). To avoid counting the same number twice, keep track of the previous number that would have been inserted in the output array of mergesort - if the current number is equal, don't count it, otherwise, count it.
Implementation in C:
- Anonymous June 15, 2015