Method 1) Basic Method
Let
array1={10,40}
array2={40,50}
array3={10,50}
first second the 0th element from all the 3 arrays and calculate the span and assume that to be minimum span…
for the above case current three values are 10,40,10 from 1st, 2nd and 3rd array respectively and the span is 30.
now increase the index of the array which contains the minimum value and now since we have a tie between 1st array and 3rd array in the given scenario that is both have their current value as 10 we can increase either of them….
Let us assume we increased the index for 1st array and now again calculate the span…
So now the 3 current values are 40, 40, 10 and the span is again 30.
Continue increasing the current minimum value holder array’s index and recalculate the span…
so now since 3rd array contain the minimum value that is 10 increase its index…
so the next 3 values are 40,40,50 and the span is 10. Since this span is less than the previously stored span value replace the previously store span value with the current one.
Now continue doing this unless any of the index is out of bound…such that index is getting increased beyond the last value.
Method 2) Optimized
O(n*m)
n == number of rows
m == number of columns (if number of elements in rows are not same , take the max value)
Merge all rows into a single array, say A (keep track of original row numbers for each of the elements)
may be something like
struct {
int element;
int rowno;
}A[n*m];
3, 7, 16, 48
5, 9, 21, 27, 49, 58
1, 4, 8, 17, 47, 50
{ {1,3} {3,1} {4,3} {5,2} {7,1} {8,3} {9,2} {16,1} {17,3} {21,2} {27,2} {47,3} {48,1} {49,2}, {50,3} {58,2}}
start from A[0], i.e; index=firstP=0; A[index]
Increment Index till u get to c one element from each row.once u get to c all elements from each row , save a pointer to the array at that point , say secondP;
may be something lime
bool Seen[n]={0};
while ( All elements in Seen[n] != 1 )
{
index++;
}
secondP=Index;
This is our first span . (to print the span you can make one more pass over the array from firstP to secondP , and can print elements from different rows)
Find the Span of this Snippet say BestSpan=A[SecondP]-A[firstP];
*Now move firstP to next number , firstP++; , check if the elements between A[firstP] to A[SecondP] contains elements from all rows . if so find the span and update it with the max span (if span < BestSpan)
*If it doesn’t contain all elements from all rows Increment secondP++;
check if the elements between A[firstP] to A[SecondP] contains elements from all rows. If so find the span and update it with the max span (if span < BestSpan)
*Repeat the same till you are done with scanning all elements from the array
While ( Scan all elements != Done )
{
firstP++
if ( contains elements from all rows )
{
if ( span < BestSpan)
{
update BestSpan
}
continue;
}
secondP++
if ( contains elements from all rows)
{
if ( span < BestSpan)
{
update BestSpan
}
}
}
{{1,3} {3,1} {4,3} {5,2} {7,1} {8,3} {9,2} {16,1} {17,3} {21,2} {27,2} {47,3} {48,1} {49,2}, {50,3} {58,2}}
FirstP = 1
SecondP = 4
Span = 4
BestSpan = 4
{{1,3} {3,1} {4,3} {5,2} {7,1} {8,3} {9,2} {16,1} {17,3} {21,2} {27,2} {47,3} {48,1} {49,2}, {50,3} {58,2}}
FirstP = 2
SecondP = 4
Span = 2
BestSpan = 2
{{1,3} {3,1} {4,3} {5,2} {7,1} {8,3} {9,2} {16,1} {17,3} {21,2} {27,2} {47,3} {48,1} {49,2}, {50,3} {58,2}}
{{1,3} {3,1} {4,3} {5,2} {7,1} {8,3} {9,2} {16,1} {17,3} {21,2} {27,2} {47,3} {48,1} {49,2}, {50,3} {58,2}}
FirstP = 3
SecondP = 5
Span = 3
BestSpan = 2
{{1,3} {3,1} {4,3} {5,2} {7,1} {8,3} {9,2} {16,1} {17,3} {21,2} {27,2} {47,3} {48,1} {49,2}, {50,3} {58,2}}
{{1,3} {3,1} {4,3} {5,2} {7,1} {8,3} {9,2} {16,1} {17,3} {21,2} {27,2} {47,3} {48,1} {49,2}, {50,3} {58,2}}
FirstP = 4
SecondP = 6
Span = 3
BestSpan = 2

Leave a Reply
You must be logged in to post a comment.