Solved

# seriesUp challenge

Posted on 2016-08-14
105 Views
Hi,

I am working on below challenge

http://codingbat.com/prob/p104090

My psuedo code of the logical approach is
1. create array of given number
2. loop though each element to set each value with given pattern(not sure how to get other previous patterns?)
3. return the modified array

I wrote my code as below
public int[] seriesUp(int n) {
int[] nums=new int[n];
for(int i=0;i<nums.length;i++){
nums[i]=i;
}
return nums;
}

I am not passing all tests

0
Question by:gudii9
• 4
• 3

LVL 37

Accepted Solution

Gerwin Jansen earned 500 total points
Which tests are failing? You just need to create a second loop to fill the array. Your first step is not correct (array size too short).
0

LVL 7

Author Comment

Expected      Run
seriesUp(3) → [1, 1, 2, 1, 2, 3]      [0, 1, 2]      X
seriesUp(4) → [1, 1, 2, 1, 2, 3, 1, 2, 3, 4]      [0, 1, 2, 3]      X
seriesUp(2) → [1, 1, 2]      [0, 1]      X
seriesUp(1) → [1]      [0]      X
seriesUp(0) → []      []      OK
seriesUp(6) → [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6]      [0, 1, 2, 3, 4, 5]      X
Your progress graph for this problem

i am failing above tests
0

LVL 37

Expert Comment

You have to create a second loop.
0

LVL 7

Author Comment

why we need second loop?
0

LVL 37

Expert Comment

Some of your test case fail because you start with 0 - look at the problem description!

A second loop would be needed (in my pseudo code) for this:

1
1 2
1 2 3
1 2 3 4

one loop for the whole series, one loop for the 'subset' of a serie
0

LVL 7

Author Comment

public int[] seriesUp(int n) {
int x=0;
int[] nums=new int[(n*(n+1))/2];
for(int i=1;i<nums.length;i++){
for(int j=1;i<nums.length;j++){
nums[x]=j;
}
}
return nums;
}

Expected      Run
seriesUp(3) → [1, 1, 2, 1, 2, 3]      Timed out      X
seriesUp(4) → [1, 1, 2, 1, 2, 3, 1, 2, 3, 4]      Timed out      X
seriesUp(2) → [1, 1, 2]      Timed out      X
seriesUp(1) → [1]      Timed out      X
seriesUp(0) → []      Timed out      X
seriesUp(6) → [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6]      Timed out      X
Your progress graph for this problem
0

LVL 7

Author Comment

public int[] seriesUp(int n) {
int x=0;
int[] nums=new int[(n*(n+1))/2];
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
nums[x++]=j;
}
}
return nums;
}
Expected      Run
seriesUp(3) → [1, 1, 2, 1, 2, 3]      [1, 1, 2, 1, 2, 3]      OK
seriesUp(4) → [1, 1, 2, 1, 2, 3, 1, 2, 3, 4]      [1, 1, 2, 1, 2, 3, 1, 2, 3, 4]      OK
seriesUp(2) → [1, 1, 2]      [1, 1, 2]      OK
seriesUp(1) → [1]      [1]      OK
seriesUp(0) → []      []      OK
seriesUp(6) → [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6]      [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6]      OK

above passed all tests. any improvement, refinements to it?
0

## Featured Post

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.