1 / 9
Oct 2021

Hi,

I am getting WA for the problem What’s next - the problem in which we’ll find whether a series is AP or GP.
Please have a look at my code and correct me where I’m wrong.

import java.util.;
import java.lang.
;

class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int[] a = new int[3];
for(int i=0; i<3; i++)
{
a[i] = sc.nextInt();
}
System.out.println(“0 0 0”);
if(a[2]-a[1] == a[1]-a[0] && a[2]-a[1] != 0)
{
int d = a[2]-a[1];
System.out.println("AP " + (a[2]+d));
}
else if(a[1]*a[1] == a[0]*a[2])
{
if(Math.abs(a[2]) > Math.abs(a[1]))
{
int p = a[2]/a[1];
System.out.println("GP " + (a[2]*p));
}
else
{
int p = a[1]/a[2];
System.out.println("GP " + (a[2]/p));
}
}
}
}

  • created

    Oct '21
  • last reply

    Oct '21
  • 8

    replies

  • 622

    views

  • 2

    users

  • 2

    links

When I run your code with the sample input, it outputs

0 0 0
AP 13

instead of

AP 13
GP 54

ACPC10A4

Thanks for pointing the error. But one thing i don’t understand is, we haven’t been given how many test cases are there right? I assumed we will enter only one sequence at a time.
Also, for the part where the last line is 0 0 0 after the sequence, do we have to give it manually in the program?

0 0 0 is in the input section, not outputs, and it tells you that you’ve reached the end of the test cases. So keep processing test cases until you read that line.

Hi
I’ve made the changes and ran the program, but it still showing WA. Would be very helpful if you could help again!

import java.util.;
import java.lang.
;

class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int[][] a = new int[10][3];
for(int i=0; i<10; i++)
{
for(int j=0; j<3; j++)
{
a[i][j] = sc.nextInt();
}
if(a[i][0]==0 && a[i][1]==0 && a[i][2]==0)
{
break;
}
else
{
if(a[i][2]-a[i][1] == a[i][1]-a[i][0] && a[i][2] - a[i][1] != 0)
{
int d = a[i][2]-a[i][1];
System.out.println("AP " + (a[i][2]+d));
}
else if(a[i][1]*a[i][1] == a[i][2]*a[i][0])
{
if(Math.abs(a[i][2]) > Math.abs(a[i][1]))
{
int p = a[i][2]/a[i][1];
System.out.println("GP " + (a[i][2]*p));
}
else
{
int p = a[i][1]/a[i][2];
System.out.println("GP " + (a[i][2]/p));
}
}
}
}
}
}

Don’t assume there won’t be more than 10 test cases.

It’s usually easier to read each test case, process it, then write the answer, before reading the next test case.

I tried to change the number of test cases to 100 and then submitted it. But it is still showing WA

Don’t assume there won’t be more than 100 test cases. Change the code so the number of test cases doesn’t matter.