Solved

fizzArray3  challenge

Posted on 2016-08-08
1
124 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
[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
  • Learn & ask questions
1 Comment
 
LVL 36

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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project

717 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