简体   繁体   中英

Determine the time complexity

Can someone help determine the time complexity of this code. Is this just O(1)? If it is not can someone help explain it? I'm not 100% sure about my answer so I need a second opinion to see if this function is O(1).

    public static void secMax(int[] arr)
    {
        int n = arr.length - 1;
       

        if (some condition not relate to n)
        {
           
            if(arr[a] * arr[b] > temp)
            {
                //print
            }
            else
            {
                //do something
            }
        }
        else
        {
            if(arr[y] * arr[x] > second)
            {
                //something
            }
            else
            {
                //something else
            }
        }
        
    }

It is O(1). You don't have any loop that traverses through the array. And you don't have any recursive call. Your method only have few if/else conditions. If you check carefully, your method only do fixed number of operations & that doesn't vary depending on the input array length. So time complexity is constant.

It is O(1). The program just simply flows from top to bottom through a few conditional statements. No block of code executes more than once, so it has constant complexity.

It's O(1) in time complexity, since you don't do any loops to traverse your argument array, neither recursive calls

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM