I have two arrays A
and B
. And I have to find the number of elements required by array C
to fit into a specific length.
Length = B[i]-A[i]
For example:
A[0] = 1 B[0] = 4
A[1] = 4 B[1] = 5
A[2] = 5 B[2] = 9
A[3] = 8 B[3] = 10
and
C[0] = 4
C[1] = 6
C[2] = 7
C[3] = 10
C[4] = 2
Answer would be 4
.
Explanation:
C[0] is falling in the range of (A[0],B[0]) and (A[1],B[1])
C[1] is falling in the range of (A[2],B[2])
C[2] is of no use
C[3] is falling in the range of (A[3],B[3])
so the total is 4
My approaches:
Traditional approach: Simple looping every element of C
in array A
and if find in the range set the value of A[i]
and B[i]
to minus infinity or remove them(for Arraylist).
HashTable: Storing the value in hash table as A[i]
as key and B[i]
as value. Then finding the element for a given C[i]
and removing the key. I think it is not a good approach
Please provide me a efficient algorithm for this.
If the input ranges are already ordered from smallest to largest (B[i] <= A[i+1]):
int bottomRangeIndex = 0;
int topRangeIndex = numberOfABRangesYouHave - 1;
int answerCounter = 0;
C.sort(); // Sort C so that it goes from smallest to largest number.
for number in C: {
if (number < A[bottomRangeIndex]) {
continue;
} else if (number > B[topRangeIndex]) {
return answerCounter;
} else {
while ((number > B[bottomRangeIndex++]) && (bottomRangeIndex <= topRangeIndex)) {
// Just keeps incrementing bottomRangeIndex.
}
if (bottomRangeIndex > topRangeIndex) {
return answerCounter;
} else {
answerCounter++;
bottomRangeIndex++;
}
}
}
This might have some bug that I am not thinking of, but basically what this does is:
Try this and see if it works. If this is homework and you really can't sort C, then you can modify this to give you the right answer with a small amount of tweaking, but I won't say how...
Ok so based on your answer, we have no choice but to check each element of C against A and B. But since A and B are in ascending order, performance should be reasonable:
int len = A.length;
boolean[] eliminated = new boolean[len]; // initially all elements are false
int totalEliminated = 0;
for (int i = 0; i < C.length; i++) {
int c = C[i];
for (int j = 0; j < len && A[j] <= c; j++) {
if (B[j] >= c && !eliminated[j]) {
System.out.println(c + " eliminates " + A[j] + " - " + B[j]);
eliminated[j] = true;
if (++totalEliminated == len) {
return i+1;
}
}
}
}
return 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With