Hasan Hüseyin Yücel - 1 year ago 50

Java Question

**There is a array and datas inserted randomly. We must find the largest range of null indexes, i have no idea. How can i solve ?
Thx....**

When you add the license numbers given below to the array, determine the largest range of indices that no license number is added. For example, if no license number is added to the indices in the range of 1255-1264 and this is the largest such range, you should determine this range and print it.

`public void maxNull() {`

int start, end, result;

for (int i = 0; i < hasharr.length; i++) {

if (hasharr[i] == null) {

while (hasharr[i] == null) {

start = i;

i++;

}

}

}

}

Answer

This code should help you:

```
//We initialize both start and end with -1, which means there was no such range
//found yet. If at the end of the search their value is unchanged, then there
//are no null values in the set
int finalStart = -1;
int finalEnd = -1;
//rangeLength represents the length of the final range, which is 0 initially
int rangeLength = 0;
int start = -1;
for (int i = 0; i < hasharr.length; i++) {
if (hasharr[i] == null) {
if (start >= 0) {
if (rangeLength < i - start + 1) {
finalStart = start;
finalEnd = i;
rangeLength = start + i;
}
start = -1;
}
} else if (start == -1) {
start = i;
}
}
if ((start >= 0) && (rangeLength < hasharr.length - start)) {
finalStart = start;
finalEnd = hasharr.length - 1;
}
```