Compress Video Code: COMPRESSVD

So i did solved the Compress Video Code, but when am implementing the logic it is not passinng 2 test cases, one of which has 6202 input and the correct output is 6201, whereas my output is 6202.

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n= sc.nextInt();
        while(n-->0){
            int t=sc.nextInt();
            ArrayList<Integer> frames= new ArrayList<>();
            while(t-->0){
                int f=sc.nextInt();
                frames.add(f);
            }
            compressVideo(frames);
        }
    }

    public static void compressVideo(ArrayList<Integer> frames){
        int count=1;
        for(int i=0;i<frames.size()-1;i++){
            if(frames.get(i) != frames.get(i+1)) count++;
        }
        System.out.println(count);
    }

this is the code that I did, I can’t seem to find what the error is, I saw the solution and when I implement the same logic with array it works.

	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
	Scanner sc = new Scanner(System.in);
		    int t1 = sc.nextInt();
		    while(t1-->0){
		        int n = sc.nextInt();
		        int[] arr = new int[n];
		        for(int i = 0; i<n; i++) {
		            arr[i] = sc.nextInt();
		        }
		        int count = 1;
		        for(int i = 0; i<n-1; i++) {
		            if(arr[i] != arr[i+1]) count++;
		        }
		        System.out.println(count);
	    
		    }

        }

Can anyone tell me what am i doing wrong??

it is the exact same logic but you are using != to compare Integers.

If you want to compare values, you use == and != for primitive datatypes (char, int, long) - datatypes that start with a lowercase letter. But you have to use equals() and !equals() when comparing Objects (String, Integer, Long) - Object types that starts with an uppercase letter.

So to fix your above code, you have to replace

if(frames.get(i) != frames.get(i+1)) count++;

with

if(!frames.get(i).equals(frames.get(i+1))) count++;

Here is a fixed solution (only 1 line changed):
https://www.codechef.com/viewsolution/69360587

Note: if you had used Integer-arrays in the below code, you would have gotten the same error.

1 Like