From 6630b6097e57ec13ad113fab32bdcdef85f8953c Mon Sep 17 00:00:00 2001 From: Pramod Patel Date: Wed, 28 May 2025 00:29:43 +0530 Subject: [PATCH 1/2] Add new Problem in Adobe Interview Questions Named "Aggressive Cows" --- company/adobe/AggressiveCows.java | 66 +++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 company/adobe/AggressiveCows.java diff --git a/company/adobe/AggressiveCows.java b/company/adobe/AggressiveCows.java new file mode 100644 index 00000000..b24b40d4 --- /dev/null +++ b/company/adobe/AggressiveCows.java @@ -0,0 +1,66 @@ +/* + Time Complexity = O(N * log(N)) + Space Complexity = O(log(N)) + + Where N is the number of elements in the given array/list. +*/ + +import java.util.Arrays; + +public class AggressiveCows +{ + // check if a distance of x is possible b/w each cow + private static boolean check(int x, int k, int []stalls) + { + + // Greedy approach, put each cow in the first place you can. + int cowsPlaced = 1, lastPos = stalls[0]; + + int n = stalls.length; + + // Traverse through the array stalls + for (int i = 1; i < n; i++) + { + if ((stalls[i] - lastPos) >= x) + { + cowsPlaced = cowsPlaced + 1; + + if (cowsPlaced == k) + { + return true; + } + + // Assign current position of stall as the lastPos. + lastPos = stalls[i]; + } + } + + return false; + } + + public static int aggressiveCows(int []stalls, int k) + { + + Arrays.sort(stalls); + + // binary search + int low = 0, high = 1000000000, mid, pos = 0; + + while (high >= low) + { + mid = (high + low) / 2; + + if (check(mid, k, stalls)) + { + low = mid + 1; + pos = mid; + } + else + { + high = mid - 1; + } + } + + return pos; + } +} \ No newline at end of file From ec053e8fe35c62321720b6f0db0e0ad387e54397 Mon Sep 17 00:00:00 2001 From: Pramod Patel Date: Wed, 28 May 2025 00:38:14 +0530 Subject: [PATCH 2/2] Add " Maximum Subarray Sum" to Adobe Interview Problem --- company/adobe/MaximumSubarraySum.java | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 company/adobe/MaximumSubarraySum.java diff --git a/company/adobe/MaximumSubarraySum.java b/company/adobe/MaximumSubarraySum.java new file mode 100644 index 00000000..3bfc2abe --- /dev/null +++ b/company/adobe/MaximumSubarraySum.java @@ -0,0 +1,32 @@ +/* + Time Complexity - O(N * K) + Space Complexity - O(1) + + where N is the length of the array and K is the size of subarrays +*/ + +public class MaximumSubarraySum { + + + + void printSubarrayMax(int[] arr, int n, int k) + { + if (n == 0 || k == 0) + { + return; + } + + for (int i = 0; i <= n - k; ++i) + { + int maxElement = Integer.MIN_VALUE; + + for (int j = i; j < i + k; ++j) + { + maxElement = Math.max(maxElement, arr[j]); + } + + System.out.println("Maximum element in subarray [" + i + ", " + (i + k - 1) + "] is: " + maxElement); + } + } + + } \ No newline at end of file