-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08cccc1
commit 6bcb5bc
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//{ Driver Code Starts | ||
// Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class GFG { | ||
public static void main(String args[]) throws IOException { | ||
BufferedReader read = | ||
new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(read.readLine()); | ||
while (t-- > 0) { | ||
String S[] = read.readLine().split(" "); | ||
long p[] = new long[2]; | ||
long q[] = new long[2]; | ||
long r[] = new long[2]; | ||
p[0] = Long.parseLong(S[0]); | ||
p[1] = Long.parseLong(S[1]); | ||
q[0] = Long.parseLong(S[2]); | ||
q[1] = Long.parseLong(S[3]); | ||
r[0] = Long.parseLong(S[4]); | ||
r[1] = Long.parseLong(S[5]); | ||
Solution ob = new Solution(); | ||
long ans = ob.InternalCount(p, q, r); | ||
System.out.println(ans); | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
// User function Template for Java | ||
|
||
class Solution { | ||
long InternalCount(long p[], long q[], long r[]) { | ||
// code here | ||
long area = Math.abs(p[0]*(q[1]-r[1]) + q[0]*(r[1]-p[1]) + r[0]*(p[1]-q[1])); | ||
long b = gcd(p,q) + gcd(q,r) +gcd(r,p); | ||
return (area-b+2)/2; | ||
} | ||
|
||
long gcd(long a[],long b[]) | ||
{ | ||
return calculate(Math.abs(a[0]-b[0]),Math.abs(a[1]-b[1])); | ||
} | ||
|
||
long calculate(long p1,long p2) | ||
{ | ||
while(p2!=0) | ||
{ | ||
long temp=p2; | ||
p2=p1%p2; | ||
p1=temp; | ||
} | ||
return p1; | ||
} | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(logn) where n = 10^9 | ||
Space complexity - O(1) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(sort + nlog(max-min)) | ||
Space complexity - O(sort) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class Solution | ||
{ | ||
public int maxDistance(int[] position, int m) | ||
{ | ||
Arrays.sort(position); | ||
|
||
int l = 1; | ||
int r = position[position.length - 1] - position[0]; | ||
|
||
while (l < r) | ||
{ | ||
int mid = r - (r - l) / 2; | ||
if (numBalls(position, mid) >= m) | ||
l = mid; | ||
else | ||
r = mid - 1; | ||
} | ||
|
||
return l; | ||
} | ||
|
||
private int numBalls(int[] position, int force) | ||
{ | ||
int balls = 0; | ||
int prevPosition = -force; | ||
for (int pos : position) | ||
if (pos - prevPosition >= force) | ||
{ | ||
balls++; | ||
prevPosition = pos; | ||
} | ||
|
||
return balls; | ||
} | ||
} |