Java > Array-3 > seriesUp (CodingBat Solution)

Problem:

Given n>=0, create an array with the pattern {1, 1, 2, 1, 2, 3, ... 1, 2, 3 .. n} (spaces added to show the grouping). Note that the length of the array will be 1 + 2 + 3 ... + n, which is known to sum to exactly n*(n + 1)/2.

seriesUp(3) → {1, 1, 2, 1, 2, 3}
seriesUp(4) → {1, 1, 2, 1, 2, 3, 1, 2, 3, 4}
seriesUp(2) → {1, 1, 2}


Solution:

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


20 comments :

  1. Here's my solution:

    public int[] seriesUp(int n) {
    int[] arr = new int[n*(n+1)/2];
    int m = 2;
    for(int i=0, j=1;i=m){
    j=1;
    m++;
    }
    }
    return arr;
    }

    ReplyDelete
    Replies
    1. I can't get this to compile, Kyle.

      Delete
  2. My solution, with one loop:

    public int[] seriesUp(int n) {
    int[] result = new int[n*(n+1)/2];
    for (int i=0,j=1,k=1; i<n*(n+1)/2&j<=n; k++){
    if(i==j*(j+1)/2){k=1;j++;}
    result[i++] = k;
    }
    return result;
    }

    ReplyDelete
  3. public int[] seriesUp(int n) {
    int[] nums = new int[n*(n+1)/2];

    int z = 1; //max number in pattern
    int i = 0; //starting index

    while(i <= nums.length - 1){
    for(int j = 1; j <= z; j++){
    nums[i] = j;
    i++;
    }
    z++;
    }
    return nums;
    }

    ReplyDelete
  4. public int[] seriesUp(int n) {
    int len = n*(n+1)/2;
    int[] res = new int[len];
    int count = 0;
    for(int i=0; i<len; i++)
    {
    for(int b=1; b<=n; b++)
    {
    if(count==0){
    res[i]=b;
    i++;
    }
    if(i==b*(b+1)/2)
    count=1;
    }
    i--;
    count=0;
    }
    return res;
    }

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

    ReplyDelete
  6. public int[] seriesUp(int n) {
    int size = n * (n + 1) / 2;
    int[] array = new int[size];
    int counter = 0;
    int limit = 1;
    for (int i = 0; i < size; i++) {
    array[i] = ++counter;
    if (counter == limit) {
    counter = 0;
    limit++;
    }
    }
    return array;
    }

    ReplyDelete
  7. public int[] seriesUp(int n) {
    int[] result = new int[n*(n+1)/2];
    int limit = 1;
    for(int i=0, j=1;ilimit){
    j=1;
    limit++;
    }
    }
    return result;
    }

    ReplyDelete
  8. public int[] seriesUp(int n) {
    int arr[] = new int[n*(n+1)/2];
    for(int i = 0,j = 0;j<n;j++,i+=j)
    {
    for(int it = i,c = 1;c<=j+1;it++,c++)arr[it]+=c;
    }
    return arr;
    }

    ReplyDelete
  9. public int[] seriesUp(int n) {
    int[] arr = new int[n*(n+1)/2];
    int k=0;
    for(int i=0; i<n*(n+1)/2; i+=k) {
    for(int j=0; j<n; j++) {
    arr[i+j] = j+1;
    }
    k++;
    }
    return arr;
    }

    ReplyDelete
  10. public int[] seriesUp(int n) {
    int[] arr = new int[n*(n+1)/2];
    int size = n*(n+1)/2;
    for(int i=size-1, j=n, sub=1; i>=0; i--,j--){
    if(j==0) {
    j=n-sub;
    sub++;
    }
    arr[i] = j;
    }

    return arr;
    }

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

    ReplyDelete
  12. public int[] seriesUp(int n) {
    //Recursive Solution Below!//
    if (n == 0)
    {
    return new int[0];
    }
    int counter = 1;
    int [] sq = seriesUp(n - 1);
    int [] sq2 = new int [sq.length + n];
    for (int i = 0; i < sq2.length; i++)
    {
    if (i < sq.length)
    {
    sq2[i] = sq[i];
    }
    else
    {
    sq2[i] = counter;
    counter++;
    }
    }
    return sq2;

    }

    ReplyDelete
  13. public int[] seriesUp(int n) {
    int[] arr = new int[n*(n+1)/2];
    for(int i = 1; i<=n; i++) {
    int j = 1;
    for(int p = i*(i-1)/2; p<i*(i+1)/2; p++) {
    arr[p] = j;
    j++;
    }
    }
    return arr;
    }
    My idea is to divide the array into n group with increasing length (1,2,3,4,....n). In each group, I start with 1 at p = i*(i-1)/2 index and increment the value of subsequent elements all the way through p<i*(i+1)/2

    ReplyDelete
  14. public int[] seriesUp(int n) {
    int size=n*(n+1)/2;
    int k=0;

    int[]arr=new int[size];
    for(int i=1;i<=n;i++)
    {
    int count=1;
    for(int j=1;j<=i;j++)
    {
    arr[k++]=count;
    count+=1;
    }

    }
    return arr;

    ReplyDelete
  15. With Java Stream

    public int[] seriesUp(int n) {
    return (java.util.stream.IntStream.rangeClosed(1, n)
    .flatMap(i -> java.util.stream.IntStream.rangeClosed(1, i)))
    .toArray();
    }

    ReplyDelete
  16. int[] arr = new int[n*(n+1)/2];
    for(int i = 0, k = 0; i0; j--) {
    arr[arr.length-1-k] = j;
    k++;
    }
    }
    return arr;

    ReplyDelete
  17. public Integer[] seriesUp(int n) {

    Stack arr=new Stack();

    while(n!=0){
    for(int i=n;i>0;i--){
    arr.push(i);
    }
    n--;
    }

    Integer[] newarr = new Integer[arr.size()];
    //stack to array conversion
    for (int i = 0; i < newarr.length; i++)
    newarr[i] = arr.pop();
    return newarr;
    }

    ReplyDelete
  18. public static int[] seriesUp(int n) {
    int arr[]=new int[n*(n+1)/2];
    int count=0;
    for(int i=0; i<n;i++) {

    for(int j=0; j<=i; j++) {
    arr[count]=j+1;
    count++;

    }
    }

    return arr;
    }

    ReplyDelete
  19. //Solution with 1 for loop

    public int[] seriesUp(int n)
    {
    int[] result = new int[n*(n+1)/2];
    int pos = 0;
    for (int i = 0, j = 1, k = 1; i < result.length; i++)
    {
    if (i == pos) {
    j++;
    pos = ((j*j)-j)/2;
    k = 1;
    }
    result[i] = k++;
    }
    return result;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact