Java > Array-2 > post4 (CodingBat Solution)

Problem:

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) {
  for (int i = nums.length-1; i >= 0; i--) {
    if (nums[i] == 4) {
      int[] foo;
      foo = new int[nums.length-i-1];

      for (int j = 0; j < foo.length; j++) {
        foo[j] = nums[i+j+1];
      }
      return foo;
    }
  }
  
  int[] bar;
  bar = new int[0];
  return bar;
}


18 comments :

  1. public int[] post4(int[] nums) {
    int result_length = 0;
    int nums_length = nums.length;
    for(int count=nums_length-1; count>0; count--){
    if(nums[count]==4){
    break;
    }else{
    result_length++;
    }
    }
    int[] result = new int[result_length];
    int nums_char_at_index = nums_length - result_length;
    for(int count=0; count<result_length; count++){
    result[count] = nums[nums_char_at_index];
    nums_char_at_index++;
    }
    return result;
    }

    ReplyDelete
  2. for(int i=nums.length-1; i>=0; i--){
    if(nums[i] == 4){
    return Arrays.copyOfRange(nums, i+1, nums.length);
    }
    }

    return new int[0];

    ReplyDelete
  3. public int[] post4(int[] nums) {
    int len = nums.length -1;
    int index = 0;
    for(int i=nums.length - 1; i >= 0;i--)
    {
    if(nums[i] == 4)
    {
    index = i;
    break;
    }
    }
    int[] arr = new int[len-index];
    for(int i = index+1, j=0; i < nums.length; i++, j++)
    {
    arr[j] = nums[i];
    }
    return arr;
    }

    ReplyDelete
  4. public int[] post4(int[] nums) {

    int count = 0;
    for(int i=0; i<nums.length; i++){
    if(nums[i] == 4){
    count = i;
    }
    }
    int newCount = 0;
    int[] newArray = new int[nums.length-1-count];
    for(int j=count+1; j<nums.length; j++){
    newArray[newCount] = nums[j];
    newCount++;
    }
    return newArray;
    }

    ReplyDelete
  5. What if i wanted to only return the even numbers that come after 4?

    ReplyDelete
  6. What if I wanted to return only even numbers after 4?

    ReplyDelete
  7. public int[] post4(int[] nums) {

    int index = 0;
    boolean got4 = false;

    for (int i = nums.length - 1; i >= 0; i--) {

    if (!got4 && nums[i] == 4) {
    got4 = true;
    index = i + 1;

    }

    }

    return Arrays.copyOfRange(nums, index, nums.length);

    ReplyDelete
  8. public int[] post4(int[] nums)
    { int arr1[]={};
    for(int i=nums.length-1;i>=0;i--)
    { if(nums[i]==4)
    { int len=nums.length-1-i;
    int arr[]=new int[len];
    for(int j=0;j<arr.length;j++)
    { arr[j]=nums[j+i+1];
    }
    return arr;
    }
    }return arr1;
    }

    ReplyDelete
  9. Its a solution with 1 cycle:

    public static int[] post4(int[] nums) {
    int arr[] = new int[0];
    boolean found = false;

    int j = 0;
    for (int i = nums.length-1; i >= 0; i--) {
    if (!found && nums[i] == 4) {
    found = true;
    arr = new int[j];
    i = nums.length - 1;
    }
    if(!found)
    j++;
    if (found && j > 0) {
    j--;
    arr[j] = nums[i];
    }
    }
    return arr;
    }

    ReplyDelete
  10. int[] post4 = {};
    int index = 0;

    for (int i = nums.length - 1; i >= 0; i--) {

    if(nums[i] == 4) {
    index = i;
    post4 = new int[ (nums.length - 1) - index ];
    break;
    }
    }

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

    post4[i] = nums[index + 1];
    index++;
    }

    return post4;

    ReplyDelete
  11. public int[] post4(int[] nums) {
    int[] result;
    int count = 0;

    for (int i = nums.length - 1; nums[i] != 4 ; i--) {
    count++;
    }
    result = new int[count];

    for (int i = nums.length - count, j = 0; i < nums.length; i++, j++) {
    result[j] = nums[i];
    }
    return result;
    }

    ReplyDelete
  12. def post4(n):
    L = []
    for i in range(len(n)):
    if 4 in n[i:len(n)]:
    pass
    else:
    L.append(n[i])
    return L

    ReplyDelete
  13. public int[] post4(int[] nums) {

    int count = 0;
    int i = nums.length-1;
    int index = 0;

    while(nums[i] != 4){
    count++;
    i--;
    }

    int [] num = new int[count];

    for(int j = i + 1; j < nums.length ; j++){

    num[index] = nums[j];
    index++;
    }

    return num;

    }

    ReplyDelete
  14. public int[] post4(int[] nums) {
    int[] temp=new int[nums.length];
    int k=0;
    boolean found=false;
    for(int i=0;i<nums.length;i++){
    if(nums[i]==4){
    k=0;
    found=true;

    }
    else if(found==true&&nums[i]!=4){


    temp[k]=nums[i];
    k++;
    }
    }

    int[] temp2=new int[k];
    for(int i=0;i<k;i++)
    temp2[i]=temp[i];


    return temp2;
    }

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

    return nums;
    }

    ReplyDelete
  16. public int[] post4(int[] nums) {

    int index = 0;

    for(int i=nums.length-1 ; i>0 ; i--){
    if(nums[i]==4){
    index=i;
    break;
    }
    }

    int a[] = new int[nums.length-(index+1)];

    for(int i=0 ; i<a.length ; i++){
    a[i] = nums[index+1];
    index++;

    }


    return a;
    }

    ReplyDelete
  17. // with 1 reversed loop:

    public int[] post4(int[] nums) {

    int[] result = new int[0];
    boolean found_4 = false;
    int first_index_4 = 0; // initialize

    for (int i = nums.length - 1; i >= first_index_4; i--) {

    if (nums[i] == 4 && found_4 == false) {

    result = new int[nums.length - 1 - i];
    found_4 = true;
    first_index_4 = i;
    i = nums.length - 1;
    }

    if (found_4 && i - first_index_4 - 1 >= 0) {
    result[i - first_index_4 - 1] = nums[i];

    }

    }
    return result;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact