Skip to content

Commit

Permalink
Added codes for 20 June
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jun 20, 2024
1 parent 08cccc1 commit 6bcb5bc
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
57 changes: 57 additions & 0 deletions GeeksForGeeks/June/20-6-24/GFG.java
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;
}
};
2 changes: 2 additions & 0 deletions GeeksForGeeks/June/20-6-24/README.md
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)
2 changes: 2 additions & 0 deletions LeetCode/June/20-6-24/README.md
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)
35 changes: 35 additions & 0 deletions LeetCode/June/20-6-24/Solution.java
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;
}
}

0 comments on commit 6bcb5bc

Please sign in to comment.