Solved

Big O notation for an array

Posted on 2009-07-05
7
675 Views
Last Modified: 2013-11-23
Hi,

What is the complexity of an array for getting an element using index like a[10].
0
Comment
Question by:SunScreenCert
  • 3
  • 2
7 Comments
 
LVL 17

Accepted Solution

by:
Thomas4019 earned 500 total points
ID: 24781766
The complexity is O(1) as it takes the exact smae amount of time to access an element regardless of the size of the list.
0
 
LVL 20

Expert Comment

by:a_b
ID: 24782553
Since you the index number that you want to address you donot have to traverse the array you can directly access it without looping through the array, so it is just one operation, therefore O(1).

It would be a different story if you had to access maybe the largest or the smallest element of the array.
0
 
LVL 2

Author Comment

by:SunScreenCert
ID: 24782738
I asked this question coz somebody was suggesting that this program be split into different arrays. Please tell me if this would give me any additional advantage. Also i am novice to programming, so all tips to make it efficient and coding standards etc are welcome.

class Say{
      static String[] print= {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninty", "hundred"};
      public static void main(String... args){
            try{
                  int i = Integer.parseInt(args[0]);
                  if(i >= 0 && i < 21)
                  System.out.println(print[i]);
                  else{
                        int length = args[0].length();
                        char[] arr= args[0].toCharArray();
                        if(length == 2){
                              //int i = Integer.parseInt(args[0].charAt(0));
                              switch(arr[0]){
                                    case 2:
                                          System.out.print(print[20]);
                                          break;
                                    case 3:
                                          System.out.print(print[21]);
                                          break;
                                    case 4:
                                          System.out.print(print[22]);
                                          break;
                                    case 5:
                                          System.out.print(print[23]);
                                          break;
                                    case 6:

                                          System.out.print(print[24]);
                                          break;
                                    case 7:
                                          System.out.print(print[25]);
                                          break;
                                    case 8:
                                          System.out.print(print[26]);
                                          break;
                                    case 9:
                                          System.out.print(print[27]);
                                          break;
                                    }
                                 System.out.print(print[(int)arr[1]]);
                                                                           
                        }
}
                  
            }catch(NumberFormatException e){
                  System.out.println("Please print only digits");
            }catch(ArrayIndexOutOfBoundsException e){
                  System.out.println("Print only valid numbers");
            }
      }
}
0
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.

 
LVL 20

Expert Comment

by:a_b
ID: 24782755
What is the purpose of the program?
0
 
LVL 2

Author Comment

by:SunScreenCert
ID: 24782969
It takes input as numbers ranging from 0- 100 and return them in words
0
 
LVL 20

Expert Comment

by:a_b
ID: 24783099
Try this out -

 
public class Test
{
    
 
    public static void main (String... args)
    {
        String[] print = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
                "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
                "eighty", "ninty", "hundred"};
 
        try
        {
            String str = "45";
            int i = Integer.parseInt(str);
            if (i >= 0 && i < 21)
                System.out.println(print[i]);
            else
            {
                int length = str.length();
                char[] arr = str.toCharArray();
                if (i < 21)
                    System.out.println(print[arr[0]]);
                else
                {
                    int a = arr[0]-48+18; 
                    int b = arr[1]-48; 
                    System.out.println(print[a]+" "+print[b]);
 
                }
           }
 
        }
        catch (NumberFormatException e)
        {
            System.out.println("Please print only digits");
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            System.out.println("Print only valid numbers");
            e.printStackTrace();
        }
    }
}

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reverse digits of a number using for loop 5 40
table example 4 29
hibernate jars 4 28
null output 3 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

786 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