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 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