Solved

fizzArray3  challenge

Posted on 2016-08-08
1
86 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now