Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8966173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:58:09+00:00 2026-06-15T16:58:09+00:00

Given n>=0 , create an array with the pattern {1,1, 2,1, 2, 3, …

  • 0

Given n>=0, create an array with the pattern {1,1, 2,1, 2, 3, ... 1, 2, 3 .. n}.

As an example if you given n=3, your method should return array as {1,1,2,1,2,3}.

My solution is here….

public int[] upSeries(int n) {

    int var1 = n + 1;        
    int var2 = n;
    int var3 = (var1*var2) / 2;
    int arr_length = var3;
    int value = 1;
    int index = 0;
    int[] arr = new int[arr_length];
    for (int j = 0; j < arr.length; j++) {
        for (int p = 0; p < j + 1; p++) {
            arr[index] = value;
            value++;

            if (index == arr.length - 1) {
                arr[index] = n;
                break;
            } else {
                index++;
            }
        }
        value = 1;
    }
    return arr;
}

What will be the best solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:58:10+00:00Added an answer on June 15, 2026 at 4:58 pm

    I cleaned it up a bit. But its the same general idea. You had a few redundancies that you could have removed.

    public int[] upSeries(int i) {
        assert i >= 0;        
    
        int[] array = new int[(i * (i+1)) / 2)];  // Standard Sum
        int seriesnum = 1;
        int seriesmax= 1;
        for (int index=0; index < array.length; index++) {
            array[index] = seriesnum;            
    
            if (seriesnum++ == seriesmax) {
                seriesnum = 1;
                seriesmax++;
            }
        }
    
        return array;
    }
    

    This solution is better by a constant multiple. If you are looking for Big-Oh they are both O(i^2) and it is not possible to do better.

    If you need a proof to why it is not possible to do better. First prove that the size of the returned array is ((i) * (i+1)) /2). Then make the argument that you have to fill every position in the array. If you do better than O(i^2) you have not filled every position in the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a route, that matches on an array. example: Name_show: pattern:
How to create one array in a method with user given length? I would
Given that EVAL is Evil how do I create an Array name dynamically: I
How do I create an array in smarty from a given string like 22||33||50
How do I create and initialize an array in F# based on a given
Given these tables: create table Orders ( Id INT IDENTITY NOT NULL, primary key
Given this example : // Create an arary of car objects. car[] arrayOfCars= new
Basically I need to create this array (given x = 3) array('?','?','?'); I could
I need to create an array or an object from a given string. String
Given a large string, what is the best way to create an array of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.