Created
December 2, 2019 05:01
-
-
Save chaudharisuresh997/b97af2086f52fc715c0452ee6a98597b to your computer and use it in GitHub Desktop.
Divide int
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int divide(int A, int B) { | |
if(A==Integer.MIN_VALUE && B<0){ | |
return Integer.MAX_VALUE; | |
}else if(A==Integer.MIN_VALUE && B>0){ | |
return Integer.MIN_VALUE; | |
} | |
if(A==Integer.MAX_VALUE){ | |
return Integer.MAX_VALUE; | |
} | |
int sign=-1; | |
if(A<0||B<0){ | |
sign=-1; | |
}else{ | |
sign=1; | |
} | |
int q=0; | |
A=Math.abs(A); | |
B=Math.abs(B); | |
while(A>=B){ | |
A-=B; | |
q++; | |
} | |
return sign*q; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment