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

Tags: #<Tag:0x00007f2425dff700>

public static int solve(ArrayList<Integer> a, int b) {
        int i = 0;
        return a.size();

Someone, please explain whats the error here


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.


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