Showing GCD and LCM side by side

this program of mine giving the wrong answer but has been submitted correctly by other users in the solution section
what is the problem?

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

/* Name of the class has to be "Main" only if the class is public. */
class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc =  new Scanner(System.in);
		int gcdans=0;
		int n = sc.nextInt();
		int ar[]=new int[n*2];
		for(int i=0;i<(n*2);i++){
		    ar[i]=sc.nextInt();
		}
		for(int j=0;j<(n*2);j+=2){
		    gcdans = gcd(ar[j],ar[j+1]);
		    System.out.println(gcdans+" "+(ar[j]*ar[j+1]/gcdans));
		}
	}
	static int gcd(int a, int b){
	    if(b==0){
	        return a;
	    }else {
	        return gcd(b, a%b);
	    }
	}
}

I’m beginner to codechef. Please help!

Here’s the problem:- FLOW016 Problem - CodeChef

And my solution:- CodeChef: Practical coding for everyone

You are using an Integer array. Say the given Integers are

999983 999979

The LCM of these two Integers is

999962000357

This doesn’t fit in an Integer. So, long has to be used.

4 Likes

Thanks, @suman_18733097. Made changes as you said. It is Accepted :slight_smile:

1 Like