My issue
explain
My code
class Chef{
public static void main(String args[]){
Scanner i =new Scanner(System.in);
int T =i.nextInt();
while(T>0){
int N =i.nextInt();
int M =i.nextInt();
T--;
}
}
}
Learning course: Beginner DSA in Java
Problem Link: CodeChef: Practical coding for everyone
Hi @dhanushgowda17
Scanner i = new Scanner(System.in);
–>it’s used to read input from the standard input stream (System.in
).
int T = i.nextInt();
–>This part of the code reads an integer T
from the user, which typically represents the number of test cases.
while (T > 0) {
–>Then, it enters a while
loop that will execute as long as T
is greater than zero.
int N = i.nextInt();
int M = i.nextInt();
–>Inside the while
loop, it reads two integers N
and M
for each test case.
T–;
–>At the end of each iteration of the while
loop, it decrements the value of T
. This is a common pattern used to iterate through a specific number of test cases.
Here is the correct code for the mentioned problem.
import java.util.Scanner;
public class Main
{
public static void main(String args) {
Scanner s = new Scanner(System.in);
int T = s.nextInt();
while (T > 0) {
int r = s.nextInt();
int l = s.nextInt();
if(l==0){
System.out.println(r+r);
}
else if(l/r==2){
System.out.println(r);
}
else{
System.out.println((r*2)-l);
}
T–;
}
}
}
If you want detailed explanation kindly reply to this msg.
Thank you
