This is the test case here, first 2 numbers are the numerator and denominator respectively of the first fraction.
The second line shows the number of queries, each line in the query has an option 1 or 2, followed by the numerator and denominator respectively of the second fraction.
Here if the option is 1 then we have to add first fraction, the second fraction, and update the answer to the first fraction. And if the option is 2 then we have multiply first fraction and the second fraction and update the answer to the first fraction.
52 71
6
2 99 6
1 86 5
2 2 51
2 27 98
2 12 75
1 11 92
Expected output should be:
858/71
10396/355
20792/18105
93564/295715
374256/7392875
115753177/680144500
But my output is coming out to be:
858/71
10396/355
20792/18105
93564/295715
374256/7392875
-383558772/441161968
Here's my code, where is the error?
import java.util.* ;
import java.io.*;
class Fraction {
Fraction(int a, int b){
System.out.print(a/gcd(a,b)+"/"+b/gcd(a,b));
}
public static int gcd(int a, int b){
int i;
if (a < b){
i = a;
}
else{
i = b;
}
for (i = i; i > 1; i--) {
if (a % i == 0 && b % i == 0){
return i;
}
}
return 1;
}
}
class Solution {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int numerator1 = sc.nextInt();
int denominator1 = sc.nextInt();
int query = sc.nextInt();
for(int i=0;i<query;i++){
int option = sc.nextInt();
int numerator2 = sc.nextInt();
int denominator2 = sc.nextInt();
if(option == 1){
numerator1 = (numerator1*denominator2) + (numerator2*denominator1);
denominator1 = denominator1 * denominator2;
}
else if(option == 2){
numerator1 = numerator1 * numerator2;
denominator1 = denominator1 * denominator2;
}
Fraction fc = new Fraction(numerator1, denominator1);
System.out.println();
}
}
}
This is the test case here, first 2 numbers are the numerator and denominator respectively of the first fraction.
The second line shows the number of queries, each line in the query has an option 1 or 2, followed by the numerator and denominator respectively of the second fraction.
Here if the option is 1 then we have to add first fraction, the second fraction, and update the answer to the first fraction. And if the option is 2 then we have multiply first fraction and the second fraction and update the answer to the first fraction.
52 71
6
2 99 6
1 86 5
2 2 51
2 27 98
2 12 75
1 11 92
Expected output should be:
858/71
10396/355
20792/18105
93564/295715
374256/7392875
115753177/680144500
But my output is coming out to be:
858/71
10396/355
20792/18105
93564/295715
374256/7392875
-383558772/441161968
Here's my code, where is the error?
import java.util.* ;
import java.io.*;
class Fraction {
Fraction(int a, int b){
System.out.print(a/gcd(a,b)+"/"+b/gcd(a,b));
}
public static int gcd(int a, int b){
int i;
if (a < b){
i = a;
}
else{
i = b;
}
for (i = i; i > 1; i--) {
if (a % i == 0 && b % i == 0){
return i;
}
}
return 1;
}
}
class Solution {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int numerator1 = sc.nextInt();
int denominator1 = sc.nextInt();
int query = sc.nextInt();
for(int i=0;i<query;i++){
int option = sc.nextInt();
int numerator2 = sc.nextInt();
int denominator2 = sc.nextInt();
if(option == 1){
numerator1 = (numerator1*denominator2) + (numerator2*denominator1);
denominator1 = denominator1 * denominator2;
}
else if(option == 2){
numerator1 = numerator1 * numerator2;
denominator1 = denominator1 * denominator2;
}
Fraction fc = new Fraction(numerator1, denominator1);
System.out.println();
}
}
}
Share
Improve this question
edited Feb 9 at 18:21
halfer
20.3k19 gold badges109 silver badges202 bronze badges
asked Jan 28 at 10:27
IshaniIshani
111 bronze badge
4
|
3 Answers
Reset to default 4You're hitting an Integer Overflow.
If you replace your handling of int
s with long
s in all places, you'll get the output you expect.
To detect integer overflow, call the Math.…Exact
methods such as Math.multiplyExact
. Trap the ArithmeticException
thrown during overflow.
Like other people already said it's probably an integer overflow.
Java uses 4 Bytes for Integers. And the number range is from -2147483648 to 2147483647.
Like other people suggest u can use long wich uses 8 Bytes. The Range is from -9223372036854775808 to 9223372036854775807.
Also in other languages like C# also exist unsigned numbers like uInt. They are only positive numbers wich gives them a range from 0 and double of the positive numbers.
Just look up primitive data types in your language.
It was showing time limit exceeded
when I tried using long
instead of int
so I tried using BigInteger
and it worked out.
Here's my final code:
import java.util.* ;
import java.io.*;
import java.math.BigInteger;
class Fraction {
// Complete the class
Fraction(BigInteger A, BigInteger B){
System.out.print(A+"/"+B);
}
}
class Solution {
public static int gcd(int a, int b){//method to find GCD of two numbers
int i;
if (a < b){
i = a;
}
else{
i = b;
}
for (i = i; i > 1; i--) {
if (a % i == 0 && b % i == 0){
return i;
}
}
return 1;
}
public static void main(String args[]) {
// Write your code here
Scanner sc = new Scanner(System.in);
int numerator1 = sc.nextInt();
int denominator1 = sc.nextInt();
int query = sc.nextInt();
for(int i=0;i<query;i++){
int option = sc.nextInt();
int numerator2 = sc.nextInt();
int denominator2 = sc.nextInt();
if(option == 1){
numerator1 = (numerator1*denominator2) + (numerator2*denominator1);
denominator1 = denominator1 * denominator2;
}
else if(option == 2){
numerator1 = numerator1 * numerator2;
denominator1 = denominator1 * denominator2;
}
int GCD = gcd(numerator1, denominator1);//calling the method
numerator1 = numerator1/GCD;
denominator1 = denominator1/GCD;
BigInteger A = BigInteger.valueOf(numerator1);//convert int to BigInteger
BigInteger B = BigInteger.valueOf(denominator1);//convert int to BigInteger
Fraction fc = new Fraction(A,B);
System.out.println();
}
}
}
115753177/680144500
and the negative results, suggest there is an integer overflow) – user85421 Commented Jan 28 at 10:58long
orBigInteger
. – teapot418 Commented Jan 28 at 11:03numerator1
anddenominator1
after every step of the calculation instead of only when printing them. – Thomas Kläger Commented Jan 28 at 12:32