Skip to content

Latest commit

 

History

History
31 lines (25 loc) · 754 Bytes

post4.md

File metadata and controls

31 lines (25 loc) · 754 Bytes

post4

Given a non-empty array of ints, return a new array containing the elements from the original array that come after the last 4 in the original array. The original array will contain at least one 4. Note that it is valid in java to create an array of length 0.

post4([2, 4, 1, 2]) → [1, 2]
post4([4, 1, 4, 2]) → [2]
post4([4, 4, 1, 2, 3]) → [1, 2, 3]

Solution:

public int[] post4(int[] nums) {
  int size = 0;
  for(int i=0; i<nums.length; i++){
    if(nums[i] == 4){
      size = nums.length-i-1;
    }
  }
  int[] arr = new int[size];
  for(int i=0; i<size; i++){
    arr[i] = nums[nums.length-size+i];
  }
  return arr;
}

codingbat

< back to readme