Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# fizzArray3  challenge

Posted on 2016-08-08
Medium Priority
169 Views
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;
}
``````

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

0
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
1 Comment

LVL 36

Accepted Solution

mccarl earned 2000 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;
}
``````
0

## Featured Post

Question has a verified solution.

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

A solution for Fortify Path Manipulation.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month11 days, 17 hours left to enroll