Skip to content

Commit

Permalink
Removed AI Tools and moved to Google gemini AI repo.
Browse files Browse the repository at this point in the history
  • Loading branch information
neerazz committed Feb 6, 2024
1 parent 719dbf7 commit 85c6e37
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 1 deletion.
97 changes: 97 additions & 0 deletions Algorithms/island/FindTheGridOfRegionAverage.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
37 changes: 37 additions & 0 deletions dataStructures/arrays/CheckFirstUnique.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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);
}

}
20 changes: 20 additions & 0 deletions dataStructures/arrays/CheckMinimum.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
77 changes: 77 additions & 0 deletions dataStructures/arrays/CheckReArrange.java
Original file line number Diff line number Diff line change
@@ -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<arr.length; i++){
if (i % 2 == 0){
arr[i] += (arr[end] % maxElement) * maxElement;
end--;
}else{
arr[i] += (arr[start] % maxElement) * maxElement;
start++;
}
}
for(int i=0; i<arr.length; i++){
arr[i] = arr[i] / maxElement;
}
}

}
57 changes: 57 additions & 0 deletions dataStructures/arrays/CheckSecondMax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.*;
import java.io.*;

/**
* Created on: Jan 24, 2024
* Ref:
* In this problem, you have to implement the int findSecondMaximum(int[] arr) method, which will traverse the whole array and return the second largest element present in the array.
* <p>
* Assumption: Array should contain at least two unique elements.
* <p>
* Method Prototype
* int findSecondMaximum(int[] arr)
* Output
* Second-largest element present in the array.
* <p>
* 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;
}
}

}
2 changes: 1 addition & 1 deletion dataStructures/arraysAndString/GroupAnagrams.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static List<List<String>> groupAnagrams(List<String> 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<String> values = map.getOrDefault(sorted, new ArrayList<>());
values.add(word);
map.put(sorted, values);
Expand Down

0 comments on commit 85c6e37

Please sign in to comment.