From ba814089bd79b835aa6917b734aa462fe92c6e87 Mon Sep 17 00:00:00 2001 From: Disha Thakurata <146114938+dishathakurata@users.noreply.github.com> Date: Tue, 17 Dec 2024 21:13:06 +0530 Subject: [PATCH] Create Aggressive cows --- Aggressive cows | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Aggressive cows diff --git a/Aggressive cows b/Aggressive cows new file mode 100644 index 0000000..e245be1 --- /dev/null +++ b/Aggressive cows @@ -0,0 +1,64 @@ +//Aggressive cows + +import java.io.*; +import java.util.*; + +class GFG { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int tc = Integer.parseInt(br.readLine().trim()); + + while(tc-- > 0) { + String[] str = br.readLine().trim().split(" "); + int[] a = new int[str.length]; + + for(int i = 0; i < str.length; i++) { + a[i] = Integer.parseInt(str[i]); + } + + String[] nk = br.readLine().trim().split(" "); + int k = Integer.parseInt(nk[0]); + Solution sln = new Solution(); + int ans = sln.aggressiveCows(a, k); + System.out.println(ans); + System.out.println("~"); + } + } +} + +class Solution { + public static int aggressiveCows(int[] stalls, int k) { + Arrays.sort(stalls); + int res = 0; + int lo = 1; + int hi = stalls[stalls.length - 1] - stalls[0]; + + while(lo <= hi) { + int mid = lo + (hi - lo) / 2; + + if(check(stalls, k, mid)) { + res = mid; + lo = mid + 1; + } + else { + hi = mid - 1; + } + } + + return res; + } + + static boolean check(int[] stalls, int k, int dist) { + int cnt = 1; + int prev = stalls[0]; + + for(int i = 1; i < stalls.length; i++) { + if(stalls[i] - prev >= dist) { + prev = stalls[i]; + cnt++; + } + } + + return (cnt >= k); + } +}