Solved

fizzArray3  challenge

Posted on 2016-08-08
1
95 Views
Last Modified: 2016-08-09
Hi,

I am working on below challenge
http://codingbat.com/prob/p142539

Psedo code description of approach :
1. find length of new array by subtracting end-start
2. create new array with above lenght
3. assign each element 0f new array using numbers between start(inclusive) and end(exclusive)

I wrote my code as below

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

Open in new window




I am passing all tests
Expected      Run            
fizzArray3(5, 10) → [5, 6, 7, 8, 9]      [5, 6, 7, 8, 9]      OK      
fizzArray3(11, 18) → [11, 12, 13, 14, 15, 16, 17]      [11, 12, 13, 14, 15, 16, 17]      OK      
fizzArray3(1, 3) → [1, 2]      [1, 2]      OK      
fizzArray3(1, 2) → [1]      [1]      OK      
fizzArray3(1, 1) → []      []      OK      
fizzArray3(1000, 1005) → [1000, 1001, 1002, 1003, 1004]      [1000, 1001, 1002, 1003, 1004]      OK      
other tests
OK

How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
1 Comment
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 41749677
There is nothing wrong with your code at all, but just to give you another alternative, I might do it like this...

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

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question