HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Checking whether if start and end has equal number of non-zero integers and contains 3 or more 0 in the center

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numberthenonequalcheckingstartmoreintegershascenter

Problem

An array is said to be hollow if it contains 3 or more zeros in the
middle that are preceded and followed by the same number of non-zero
elements. Write a function named isHollow that accepts an integer
array and returns 1 if it is a hollow array, otherwise it returns 0.
The function signature is int isHollow(int[ ] a).




Note: Please do not use any String functions. No sorting allowed. No additional arrays or data structures allowed.




Test Cases:



  • isHollow({1,2,4,0,0,0,3,4,5}) returns true



  • isHollow({1,2,4,0,0,0,1,0,3,4,5}) returns false



  • isHollow({1,2,0,0,0,3,4,5}) returns false



  • isHollow({1,2,4,9, 0,0,0,3,4,5}) returns false



  • isHollow({1,2, 0,0, 3,4}) returns false




private static int isHollow(int[] array) {
int length = array.length;
int startCount = 0;
int endCount = 0;
int zeroCount = 0;
int nonZeroCount = 0;

if (array[0] == 0 || array[length - 1] == 0) return 0;

for (int i = 0; i = 0; i--) {
if (array[i] != 0) {
endCount++;
} else {
break;
}
}

if (startCount == endCount && (startCount + endCount) == nonZeroCount && zeroCount >= 3) {
return 1;
}
return 0;
}

Solution

You can determine the correct answer in a single pass, by working your way from the middle of the array outward, or from the ends of the array inward. Let's get started.

Although the problem description states to return 1 if the array is hollow and 0 if it is not, such signature would make no sense in Java. It's only natural, and strongly recommended to use proper boolean true and false, respectively.

To work outward from the center, let's track left and right indexes.
If the array has odd number of elements,
left and right can both point to the middle element.
Otherwise the array has two central elements,
let's make left and right point at those.

int mid = nums.length / 2;
int left, right;
if (nums.length % 2 != 0) {
    left = right = mid;
} else {
    left = mid - 1;
    right = mid;
}


Now, let's start moving the left and right indexes outward, as long as both nums[left] and nums[right] are 0, and left > 0.

while (left > 0 && nums[left] == 0 && nums[right] == 0) {
    left--;
    right++;
}


The loop ends either when left reaches the second position of the array,
or when nums[left] or nums[right] (or both) are not zero.
If left and right were not moved enough, that is,
we haven't moved over enough zeros, then the array is not hollow and we can return.

if (right - left < 4) {
    return false;
}


Finally, we need to verify that the rest of the elements are all non-zero.
We can do that by continuing to move outward, and aborting if any of the values is 0.

while (left >= 0) {
    if (nums[left] == 0 || nums[right] == 0) {
        return false;
    }
    left--;
    right++;
}


When the loop completes without aborting, we know the array is hollow.

The complete solution, with some more test cases to verify it:

boolean isHollow(int... nums) {
    int mid = nums.length / 2;
    int left, right;
    if (nums.length % 2 == 0) {
        left = mid - 1;
        right = mid;
    } else {
        left = right = mid;
    }

    while (left > 0 && nums[left] == 0 && nums[right] == 0) {
        left--;
        right++;
    }

    if (right - left = 0) {
        if (nums[left] == 0 || nums[right] == 0) {
            return false;
        }
        left--;
        right++;
    }

    return true;
}

@Test
public void verify_isHollow() {
    assertThat(isHollow(1, 2, 4, 0, 0, 0, 3, 4, 5)).isTrue();
    assertThat(isHollow(1, 2, 4, 0, 0, 0, 1, 0, 3, 4, 5)).isFalse();
    assertThat(isHollow(1, 2, 0, 0, 0, 3, 4, 5)).isFalse();
    assertThat(isHollow(1, 2, 4, 9, 0, 0, 0, 3, 4, 5)).isFalse();
    assertThat(isHollow(1, 2, 0, 0, 3, 4)).isFalse();
    assertThat(isHollow(1, 2, 0, 0, 0, 0, 3, 4)).isTrue();
    assertThat(isHollow(1, 0, 0, 0, 3)).isTrue();
    assertThat(isHollow(1, 2, 0, 0, 0, 0, 3, 4, 5)).isFalse();
    assertThat(isHollow(1, 2, 1, 0, 0, 0, 0, 3, 4)).isFalse();
    assertThat(isHollow(0, 0, 0)).isFalse();
    assertThat(isHollow(0, 0, 0, 0, 0)).isFalse();
    assertThat(isHollow(0, 0, 0, 0, 0, 1)).isFalse();
    assertThat(isHollow(1, 0, 0, 0, 0, 0)).isFalse();
    assertThat(isHollow(1, 0, 0, 0, 0, 0, 2)).isTrue();
}

Code Snippets

int mid = nums.length / 2;
int left, right;
if (nums.length % 2 != 0) {
    left = right = mid;
} else {
    left = mid - 1;
    right = mid;
}
while (left > 0 && nums[left] == 0 && nums[right] == 0) {
    left--;
    right++;
}
if (right - left < 4) {
    return false;
}
while (left >= 0) {
    if (nums[left] == 0 || nums[right] == 0) {
        return false;
    }
    left--;
    right++;
}
boolean isHollow(int... nums) {
    int mid = nums.length / 2;
    int left, right;
    if (nums.length % 2 == 0) {
        left = mid - 1;
        right = mid;
    } else {
        left = right = mid;
    }

    while (left > 0 && nums[left] == 0 && nums[right] == 0) {
        left--;
        right++;
    }

    if (right - left < 4) {
        return false;
    }

    while (left >= 0) {
        if (nums[left] == 0 || nums[right] == 0) {
            return false;
        }
        left--;
        right++;
    }

    return true;
}

@Test
public void verify_isHollow() {
    assertThat(isHollow(1, 2, 4, 0, 0, 0, 3, 4, 5)).isTrue();
    assertThat(isHollow(1, 2, 4, 0, 0, 0, 1, 0, 3, 4, 5)).isFalse();
    assertThat(isHollow(1, 2, 0, 0, 0, 3, 4, 5)).isFalse();
    assertThat(isHollow(1, 2, 4, 9, 0, 0, 0, 3, 4, 5)).isFalse();
    assertThat(isHollow(1, 2, 0, 0, 3, 4)).isFalse();
    assertThat(isHollow(1, 2, 0, 0, 0, 0, 3, 4)).isTrue();
    assertThat(isHollow(1, 0, 0, 0, 3)).isTrue();
    assertThat(isHollow(1, 2, 0, 0, 0, 0, 3, 4, 5)).isFalse();
    assertThat(isHollow(1, 2, 1, 0, 0, 0, 0, 3, 4)).isFalse();
    assertThat(isHollow(0, 0, 0)).isFalse();
    assertThat(isHollow(0, 0, 0, 0, 0)).isFalse();
    assertThat(isHollow(0, 0, 0, 0, 0, 1)).isFalse();
    assertThat(isHollow(1, 0, 0, 0, 0, 0)).isFalse();
    assertThat(isHollow(1, 0, 0, 0, 0, 0, 2)).isTrue();
}

Context

StackExchange Code Review Q#153501, answer score: 6

Revisions (0)

No revisions yet.