From 85c6e37e6d88f337f1914b0ffc48d34646f8bd1b Mon Sep 17 00:00:00 2001 From: Neeraj Kumar Singh B Date: Tue, 6 Feb 2024 16:05:49 -0500 Subject: [PATCH] Removed AI Tools and moved to Google gemini AI repo. --- .../island/FindTheGridOfRegionAverage.java | 97 +++++++++++++++++++ dataStructures/arrays/CheckFirstUnique.java | 37 +++++++ dataStructures/arrays/CheckMinimum.java | 20 ++++ dataStructures/arrays/CheckReArrange.java | 77 +++++++++++++++ dataStructures/arrays/CheckSecondMax.java | 57 +++++++++++ .../arraysAndString/GroupAnagrams.java | 2 +- 6 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 Algorithms/island/FindTheGridOfRegionAverage.java create mode 100644 dataStructures/arrays/CheckFirstUnique.java create mode 100644 dataStructures/arrays/CheckMinimum.java create mode 100644 dataStructures/arrays/CheckReArrange.java create mode 100644 dataStructures/arrays/CheckSecondMax.java diff --git a/Algorithms/island/FindTheGridOfRegionAverage.java b/Algorithms/island/FindTheGridOfRegionAverage.java new file mode 100644 index 00000000..b55ff120 --- /dev/null +++ b/Algorithms/island/FindTheGridOfRegionAverage.java @@ -0,0 +1,97 @@ +import java.util.*; +import java.io.*; + +/** + * Created on: Feb 06, 2024 + * Ref: https://leetcode.com/problems/find-the-grid-of-region-average/description/ + */ + +public class FindTheGridOfRegionAverage { + + public static void main(String[] args) { + System.out.println(Arrays.deepToString(resultGrid(new int[][]{{5, 6, 7, 10}, {8, 9, 10, 10}, {11, 12, 13, 10}}, 3))); + System.out.println(Arrays.deepToString(resultGrid(new int[][]{{0, 14, 5, 15}, {20, 12, 2, 11}, {8, 11, 0, 3}}, 14))); + System.out.println(Arrays.deepToString(resultGrid(new int[][]{{1, 1, 4, 1}, {10, 8, 13, 17}, {2, 12, 1, 16}}, 14))); + System.out.println(Arrays.deepToString(resultGrid(new int[][]{{1, 18, 6, 4}, {4, 5, 17, 12}, {10, 1, 15, 19}}, 14))); + } + + public static int[][] resultGrid(int[][] image, int threshold) { + int rows = image.length, cols = rows > 0 ? image[0].length : 0; + int[][] result = new int[rows][cols]; + int[] dirs = {0, 1, 2}; + int[][] counts = new int[rows][cols]; + for (int row = 0; row < rows; row++) { + for (int col = 0; col < cols; col++) { +// Check if Region is valid or not. + int xMax = Math.min(rows, row + 3), yMax = Math.min(col + 3, cols); + int count = 0; + int sum = 0; + boolean isPossible = true; + checkRegion: + for (int dir1 : dirs) { + for (int dir2 : dirs) { + int x = row + dir1, y = col + dir2; + if (inRange(x, y, 0, 0, rows, cols)) { + if (!validNeighbours(x, y, row, col, xMax, yMax, image, threshold)) { + isPossible = false; + break checkRegion; + } else { + count++; + sum += image[x][y]; + } + } + } + } + isPossible = isPossible && count == 9; +// If valid Loop through the region and set that its valid. + if (isPossible) { + int curAvg = sum / 9; + for (int dir1 : dirs) { + for (int dir2 : dirs) { + int x = row + dir1, y = col + dir2; + if (inRange(x, y, 0, 0, rows, cols)) { + result[x][y] += curAvg; + counts[x][y]++; + } + } + } + } + } + } +// System.out.println("result = " + Arrays.deepToString(result)); + + for (int row = 0; row < rows; row++) { + for (int col = 0; col < cols; col++) { + int count = counts[row][col]; + if(count == 0){ + result[row][col] = image[row][col]; + }else{ + result[row][col] /= count; + } + } + } + +// System.out.println("result = " + Arrays.deepToString(result)); + return result; + } + + private static boolean inRange(int row, int col, int xStart, int yStart, int xEnd, int yEnd) { + return row >= xStart && row < xEnd && col >= yStart && col < yEnd; + } + + private static boolean validNeighbours(int row, int col, int xStart, int yStart, int xEnd, int yEnd, int[][] image, int threshold) { + int[] up = new int[]{row - 1, col}; + int[] left = new int[]{row, col - 1}; + int[] down = new int[]{row + 1, col}; + int[] right = new int[]{row, col + 1}; + int curNum = image[row][col]; + for (int[] ids : List.of(up, left, right, down)) { + int x = ids[0], y = ids[1]; + if (inRange(x, y, xStart, yStart, xEnd, yEnd) && Math.abs(curNum - image[x][y]) > threshold) { + return false; + } + } + return true; + } + +} diff --git a/dataStructures/arrays/CheckFirstUnique.java b/dataStructures/arrays/CheckFirstUnique.java new file mode 100644 index 00000000..a80955ea --- /dev/null +++ b/dataStructures/arrays/CheckFirstUnique.java @@ -0,0 +1,37 @@ +import java.util.*; +import java.io.*; + +/** + * Created on: Jan 24, 2024 + * Ref: + * In this problem, you have to implement the int findFirstUnique(int[] arr) method that will look for a first unique integer, which appears only once in the whole array. The function returns -1 if no unique number is found. + * + * Method Prototype + * int findFirstUnique(int[] arr) + * Output + * The first unique element in the array. + * + * Sample Input + * arr = {9, 2, 3, 2, 6, 6} + * Sample Output + * 9 + */ + +public class CheckFirstUnique { + + public static void main(String[] args) { + + } + + public static int findFirstUnique(int[] arr){ + LinkedHashMap map = new LinkedHashMap<>(); + for(int num: arr){ + map.put(num, map.getOrDefault(num, 0)+1); + } + return map.entrySet().stream() + .filter(e -> e.getValue() == 1) + .map(e -> e.getKey()) + .findFirst().orElse(-1); + } + +} diff --git a/dataStructures/arrays/CheckMinimum.java b/dataStructures/arrays/CheckMinimum.java new file mode 100644 index 00000000..96baeeed --- /dev/null +++ b/dataStructures/arrays/CheckMinimum.java @@ -0,0 +1,20 @@ +import java.util.*; +import java.io.*; + +/** + * Created on: Jan 23, 2024 + * Ref: In this problem, you have to implement the int findMinimum(int[] arr) method, which will traverse the whole array and find the smallest number in the array. + */ + +public class CheckMinimum { + + public static void main(String[] args) { + + } + + public static int findMinimum(int[] arr) { + if(arr == null || arr.length == 0) return -1; + return Arrays.stream(arr).min().getAsInt(); + } + +} diff --git a/dataStructures/arrays/CheckReArrange.java b/dataStructures/arrays/CheckReArrange.java new file mode 100644 index 00000000..80732f09 --- /dev/null +++ b/dataStructures/arrays/CheckReArrange.java @@ -0,0 +1,77 @@ +import java.util.*; +import java.io.*; + +/** + * Created on: Jan 24, 2024 + * Ref: + * In this problem, you have to implement the void reArrange(int[] arr) method, which will sort the elements, such that all the negative elements appear at the left and positive elements appear at the right. + * + * Note: Consider 0 as a positive number. + * + * Method Prototype + * void reArrange(int[] arr) + * Output + * A sorted array with negative elements at the left and positive elements at the right. + * + * Sample Input + * arr = {10, -1, 20, 4, 5, -9, -6} + * Sample Output + * arr = {-1, -9, -6, 10, 20, 4, 5} + * Note: Order of the numbers doesn’t matter. + * + * {-1, -9, -6, 10, 20, 4, 5} = {-9, -1, -6, 10, 4, 20, 5} + * + */ + +public class CheckReArrange { + + public static void main(String[] args) { + maxMin_optimal(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}); + } + + public static void reArrange(int[] arr) { + int len = arr.length; + int p1 = 0, p2 = len-1; + while(p2 >= 0){ + while(arr[p2] < 0 && p2 > p1){ + int temp = arr[p1]; + arr[p1] = arr[p2]; + arr[p2] = temp; + p1++; + } + p2--; + } + } + + public static void maxMin(int[] arr) { + int start =0, end = arr.length-1, p =0; + int[] temp = Arrays.copyOf(arr, arr.length); + while(start < end){ + arr[p++] = temp[end]; + arr[p++] = temp[start]; + start++; + end--; + } + if(start == end){ + arr[p++] = temp[start]; + } + } + + public static void maxMin_optimal(int[] arr) { + int start =0, end = arr.length-1, p =0; + int maxElement = arr[end] + 1; + for(int i=0; i + * Assumption: Array should contain at least two unique elements. + *

+ * Method Prototype + * int findSecondMaximum(int[] arr) + * Output + * Second-largest element present in the array. + *

+ * Sample Input + * arr = {9,2,3,6} + * Sample Output + * 6 + */ + +public class CheckSecondMax { + + public static void main(String[] args) { + + } + + public int findSecondMaximum(int[] arr) { + if (arr == null || arr.length < 2) { + return -1; + } + int firstMax = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE; + for (int num : arr) { + if (num > firstMax) { + secondMax = firstMax; + firstMax = num; + } else if (num > secondMax) { + secondMax = num; + } + } + return secondMax; + } + + public static void rotateArray(int[] arr) { + if (arr == null || arr.length < 1) { + return; + } + int len = arr.length; + int pre = arr[len - 1]; + for (int i = 0; i < len; i++) { + int temp = arr[i]; + arr[i] = pre; + pre = temp; + } + } + +} diff --git a/dataStructures/arraysAndString/GroupAnagrams.java b/dataStructures/arraysAndString/GroupAnagrams.java index af2f608a..5b5c5815 100644 --- a/dataStructures/arraysAndString/GroupAnagrams.java +++ b/dataStructures/arraysAndString/GroupAnagrams.java @@ -10,7 +10,7 @@ public static List> groupAnagrams(List words) { for (String word : words) { char[] chars = word.toCharArray(); Arrays.sort(chars); - String sorted = new StringBuilder().append(chars).toString(); + String sorted = String.valueOf(chars); List values = map.getOrDefault(sorted, new ArrayList<>()); values.add(word); map.put(sorted, values);