CAN ANYONE PLEASE SAY WHAT'S WRONG IN THIS PROGRAM?

/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
int n=sc.nextInt();
String s=sc.next();
int p=0;
int q=0;
int r=0;
int m=0;
for(int j=1;j<=s.length();j++){

	    if(s.charAt(j)=='R')
	    {
	        p++;
	    }
	    else if(s.charAt(j)=='B')
	    {
	        q++;
	    }
	    else if(s.charAt(j)=='G')
	    {
	        r++;
	    }
	    if(p==n||q==n||r==n)
	    {
	        System.out.println("0");
	    }
	    else{
	        m=Math.max(p,Math.max(q,r));
	       System.out.println(n-m);
	    }
	    }
	}
}

}

Please post the problem link.

format the code or share a link (to your submission , not the question)

this is the link to the question :worried:

https://www.codechef.com/viewsolution/34182813

this the link of my solution,please watch it…and if you need the question…it’s already posted and please say what changes should be made so that the program executes

I saw your solution. The problem is you are printing the results before the for j loop ends i.e. Before p, q, r are fully calculated.

So move the code the calculates and prints the output outside the for j loop.

Plus, you don’t need to check if p==n || q == n || r == n, the else case can solve it as well.

thanks for your help