Solved

Big O notation for an array

Posted on 2009-07-05
7
671 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now