Java > Array-2 > fizzArray3 (CodingBat Solution)

Problem:

Given start and end numbers, return a new array containing the sequence of integers from start up to but not including end, so start=5 and end=10 yields {5, 6, 7, 8, 9}. The end number will be greater or equal to the start number. Note that a length-0 array is valid. (See also: FizzBuzz Code)

fizzArray3(5, 10) → {5, 6, 7, 8, 9}
fizzArray3(11, 18) → {11, 12, 13, 14, 15, 16, 17}
fizzArray3(1, 3) → {1, 2}


Solution:

public int[] fizzArray3(int start, int end) {
    int n = end - start;
    int[] result = new int[n];
 
    for (int i = 0; i < n; i++)
        result[i] = start++;
    return result;
}


4 comments :


  1. int dif = end-start;
    int[] erg = new int[dif];

    for(int i=0; i < dif; i++){
    erg[i] = start+i;
    }
    return erg;

    ReplyDelete
  2. public int[] fizzArray3(int start, int end) {
    int[] newArray = new int[end-start];
    int count = 0;
    for(int i=start; i<end; i++){
    newArray[count] = i;
    count++;
    }
    return newArray;
    }

    ReplyDelete
  3. public int[] fizzArray3(int start, int end) {
    int[] arr = new int[end-start];
    for (int i=start; i<end; i++){
    //if (i==start)
    arr[i-start]=i;
    }
    return arr;
    }

    ReplyDelete
  4. public int[] fizzArray3(int start, int end) {
    int[] newArray = new int[end - start];
    int addOn = 0;

    for(int i = 0; i < end - start; i++) {
    newArray[i] = start + addOn;
    addOn++;
    }
    return newArray;
    }

    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