No Di
No Di

Reputation: 3

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
            }
        }
        
    }

Upvotes: 0

Views: 85

Answers (3)

Kolepcruz
Kolepcruz

Reputation: 61

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

Upvotes: 1

Suyash Krishna
Suyash Krishna

Reputation: 241

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.

Upvotes: 2

aatwork
aatwork

Reputation: 2270

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.

Upvotes: 2

Related Questions