DEV Community

Scale to zero
Scale to zero

Posted on

Help! rectify Big O Notation, if wrong

Please help me understand, what is the Order of Growth?
What is the Big O notation for this program

What i did so far?
I wrote this problem.
What is the Order of Growth according to me?
There are two access to array

  1. once during this check, c[jumpLevel2]==0 , Worst case, n time access
  2. Another during this, c[jumpLevel1]==0 , Worst case, n time access

So, 2N access
Therefore O(n)

Please correct me if I am wrong.

import java.io.;
import java.math.
;
import java.security.;
import java.text.
;
import java.util.;
import java.util.concurrent.
;
import java.util.regex.*;

public class Solution {

// Complete the jumpingOnClouds function below.
static int jumpingOnClouds(int[] c) {
    int i=0;
    int path = 0;
    int clouds = c.length;
    while (i<clouds){
        int jumpLevel2 = i+2;
        int jumpLevel1 = i+1;
        if ((jumpLevel2<clouds) && c[jumpLevel2]==0){
            path+=1;
            i=jumpLevel2;
        } else if ((jumpLevel1<clouds) && c[jumpLevel1]==0){
            path+=1;
            i=jumpLevel1;
        } else {
            return path;
        }
    }
    return path;
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

    int n = scanner.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    int[] c = new int[n];

    String[] cItems = scanner.nextLine().split(" ");
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    for (int i = 0; i < n; i++) {
        int cItem = Integer.parseInt(cItems[i]);
        c[i] = cItem;
    }

    int result = jumpingOnClouds(c);

    bufferedWriter.write(String.valueOf(result));
    bufferedWriter.newLine();

    bufferedWriter.close();

    scanner.close();
}

}

Note: Code from Hackerrank, for my personal practise.
I am using dev.to platform to help understand my understanding. haha

Top comments (1)

Collapse
 
mindmath profile image
Scale to zero

Hi dev.toer's

I want suggestion on my conclusion about the Order of growth as 2N which becomes O(N)

thanks in advance