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;
}


13 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
  5. public int[] fizzArray3(int start, int end) {

    int[] fizzArray = new int[end - start];

    for (int i = 0, j = start; j < end; i++, j++) {

    fizzArray[i] = j;
    }
    return fizzArray;

    ReplyDelete
  6. public int[] fizzArray3(int start, int end) {
    if(start >= end) return new int[0];

    int[] out = new int[end - start];
    for(int i = 0; i < out.length; i++) {
    out[i] = start + i;
    }
    return out;
    }

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

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

    ReplyDelete
  9. public int[] fizzArray3(int start, int end) {
    int size=end-start;
    int a[]=new int[(size)];

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

    ReplyDelete
  10. int len= end-start;

    int [] newArray= new int [len];

    for (int i=0; i<len ; i++){
    newArray [i] = start+i;
    }

    return newArray;

    ReplyDelete
  11. With Java Stream

    public int[] fizzArray3(int start, int end) {
    return java.util.stream.IntStream.range(start, end).toArray();
    }

    ReplyDelete
  12. public int[] fizzArray3(int start, int end) {
    int s = end - start;
    int[] nums = new int[s];

    for(int i = start,j=0; i < end; i++,j++){
    nums[j] += i;
    }
    return nums;
    }

    ReplyDelete
  13. public int[] fizzArray3(int start, int end) {

    int[] b=new int[end-start];
    for(int i=start,j=0;i<end;i++,j++){
    b[j]=i;
    }
    return b;

    }

    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