Time complexity of my code is O(N) and space complexity is O(1), still got partial ans

amazon
Tags: #<Tag:0x00007f2425dff700>

#1
public static int solve(ArrayList<Integer> a, int b) {
        int i = 0;
        while(i<a.size()){
            if(a.get(i)==b){
                a.remove(i);
            }else{
                i++;
            }
        }
        return a.size();
    }

Someone, please explain whats the error here


#2

I am not sure about arraylist implementation in java but most probably the erase method is not O(1). It shifts all elements to left after the removal. It would probably be linear in time with the remaining elements after your position.


#3

Remove function itself has its complexity
instead take variable A=len(A)
and for every occurence of same element a=a-1
return a