user8858291
user8858291

Reputation:

How do I shift elements in an array to the left by 1 if there is a zero?

Ok so I have to go through an array and if there is a zero in the array then I have to shift the elements right of the zero to the left by 1.

For example, given:

[3,0,1,2]

after a call to the method I should get:

[3,1,2,0]

This is what I have so far;

public int[] shiftArray(int[] arr) {
    for (int i = 0; i < arr.length - 1; i++) {
        if (arr[i] == 0) {
            arr[i] = arr[i + 1];
        }
        arr[arr.length - 1] = 0;
    }
    return null;
}

I'm stuck on how to shift all the elements right of zero to the left by 1 and not just the one right next to the zero.

Upvotes: 0

Views: 1157

Answers (4)

user14838237
user14838237

Reputation:

You can use Arrays.stream method to iterate over the elements of this array, filter out unnecessary elements and reassemble the array of the same length as follows:

int[] arr = {3, 0, 1, 0, 2};

int length = arr.length;

System.out.println(Arrays.toString(arr)); // [3, 0, 1, 0, 2]

arr = Arrays.stream(
        Arrays.stream(arr)
                .filter(i -> i != 0)
                .boxed()
                .toArray(q -> new Integer[length]))
        .mapToInt(i -> i == null ? 0 : i)
        .toArray();

System.out.println(Arrays.toString(arr)); // [3, 1, 2, 0, 0]

This approach is best used for an array of objects rather than an array of primitives.
See: How can you reduce the indices of elements in a string array after deleting an element from it?

Upvotes: 0

AliHA
AliHA

Reputation: 181

Here are two ways to do it, one with loops and the other with streams

public static int[] ZeroRight(int[] arr) {
    int[] temp = new int[arr.length];
    int leftIndex = 0;
    int rightIndex = arr.length - 1;
    for (int i : arr) {
        if (i == 0) {
            temp[rightIndex] = i;
            rightIndex--;
        } else {
            temp[leftIndex] = i;
            leftIndex++;
        }
    }
    return temp;
}
public static int[] ZeroRightStream(int[] arr) {
    return Arrays.stream(arr)
            .boxed()
            .sorted((a, b) -> b == 0 ? -1 : 0)
            .mapToInt(i -> i)
            .toArray();
}

Upvotes: 1

Erik Elmgren
Erik Elmgren

Reputation: 780

public int[] shiftArray(int[] arr)
{
    int shift = 0;

    for (int i = 0; i < arr.length; i++) {
        if (arr[i]==0) {
            shift++; // if you only want to handle the 1st zero, use shift = 1
        } else if (shift>0) {
            arr[i-shift] = arr[i];
        }
    }
    for (int i = arr.length - shift; i < arr.length; i++) {
        arr[i] = 0;
    }
    return null;
}

Upvotes: 2

Taslim Oseni
Taslim Oseni

Reputation: 6263

The main thing your algorithm/procedure should focus on is this:

If the value in an index is zero and it isn't the last index, perform two operations:

  1. Move the value of the index to the right
  2. Move the number previously at the right to the left.

The steps above are very important. I see that you've neglected step 2 in your question.

Here's how I'd handle it:

    public int[] shiftArray(int[] arr){ 

      for(int i = 0; i < arr.length - 1; i ++) {
              if((arr[i] == 0) && (i != (arr.length - 1))){
                  int prev = arr[i];
                  int next = arr[i + 1];
                  arr[i] = next;
                  arr[i + 1] = prev;
              }
      }
    return arr;
  }

I hope this helps.. Merry coding!

Upvotes: 1

Related Questions