Why does it give Exceptions (MSV)

Exception in thread “main” java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:14)

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

public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
int t;
// java.io.BufferedReader t = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
Scanner sc = new Scanner(System.in );
t = sc.nextInt();
for (int i=0;i<t;i++ )
{
int n;
// java.io.BufferedReader n = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
n = sc.nextInt();
int a[] =new int[n];
for ( i=0;i<n;i++)
{
// java.io.BufferedReader a[i] = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
a[i] = sc.nextInt();
}
int count[]= new int[n];
for (i=0;i<n;i++)
count[i]=0;
for (i=0;i<n;i++)
{ for (int j=n-1;j>i;j–)
{
if (a[i]%a[j]==0)
count[j]++;
}
}
int max = count[0];

     for (i = 1; i <n; i++) 
         if (count[i] > max) 
             max = count[i];
             System.out.println(max);
	}
}

}

Please either format your code or link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Also - what Problem are you trying to solve?

2 Likes



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) 
	{
		int t;
		Scanner sc = new Scanner(System.in);
		t = sc.nextInt();
		for (int i=0;i<t;i++ )
		{
		    int n;
		    n = sc.nextInt();
		    int a[] =new int[n];
		    for ( i=0;i<n;i++)
		    {
		       a[i] = sc.nextInt();
		    }
		    int count[]= new int[n];
		    for (i=0;i<n;i++)
		    count[i]=0;
		    for (i=0;i<n;i++)
		   { for (int j=n-1;j>i;j--)
		       {
		           if (a[i]%a[j]==0)
		           count[j]++;
		       }   
		   }
		   int max = count[0]; 
        
          
         for (i = 1; i <n; i++) 
             if (count[i] > max) 
                 max = count[i];
                 System.out.println(max);
		}
	}
}
1 Like

Oh, wait - are you clicking “Run” without providing “Custom Input”? If you do that, you’ll likely get an Exception like this one. See e.g. New to Codechef. Can't understand the platform - #3 by ssjgz

1 Like

As for why you’re getting a WA - the following testcase should help you debug :slight_smile:

2
1
10
1
10
2 Likes

can you explain i am getting output as 3 which is correct i think…

I get the output

0

from the code in Why does it give Exceptions (MSV) - #4 by neelesh_garg .

How many numbers should it be outputting?

1 Like