From a487c91bc51cdce3fdf312ea25826b787135c200 Mon Sep 17 00:00:00 2001 From: Arghya Das <95538110+alfaArghya@users.noreply.github.com> Date: Wed, 16 Aug 2023 23:07:42 +0530 Subject: [PATCH] Add files via upload --- 01-Array/Array29_3Sum.java | 77 +++++++++++++++++++++++ 01-Array/Array30_3SumCloset.java | 29 +++++++++ 01-Array/Array31_SubArraySumEqualsK.java | 44 +++++++++++++ 01-Array/Array32_GameOfLife.java | 34 ++++++++++ 01-Array/Array33_4Sum.java | 71 +++++++++++++++++++++ 01-Array/Array34_FIndDuplicateNumber.java | 30 +++++++++ 01-Array/Array35_SearchIn2DMatrix.java | 61 ++++++++++++++++++ 7 files changed, 346 insertions(+) create mode 100644 01-Array/Array29_3Sum.java create mode 100644 01-Array/Array30_3SumCloset.java create mode 100644 01-Array/Array31_SubArraySumEqualsK.java create mode 100644 01-Array/Array32_GameOfLife.java create mode 100644 01-Array/Array33_4Sum.java create mode 100644 01-Array/Array34_FIndDuplicateNumber.java create mode 100644 01-Array/Array35_SearchIn2DMatrix.java diff --git a/01-Array/Array29_3Sum.java b/01-Array/Array29_3Sum.java new file mode 100644 index 0000000..186a2db --- /dev/null +++ b/01-Array/Array29_3Sum.java @@ -0,0 +1,77 @@ +/* +Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. +Notice that the solution set must not contain duplicate triplets. + +Example 1: +Input: nums = [-1,0,1,2,-1,-4] +Output: [[-1,-1,2],[-1,0,1]] +Explanation: +nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0. +nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0. +nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0. +The distinct triplets are [-1,0,1] and [-1,-1,2]. +Notice that the order of the output and the order of the triplets does not matter. + +Example 2: +Input: nums = [0,1,1] +Output: [] +Explanation: The only possible triplet does not sum up to 0. + +Example 3: +Input: nums = [0,0,0] +Output: [[0,0,0]] +Explanation: The only possible triplet sums up to 0. + +Constraints: + 3 <= nums.length <= 3000 + -105 <= nums[i] <= 105 + */ + + +import java.util.ArrayList; +import java.util.Arrays; +public class Array29_3Sum { + + public static void _3Sum(int[] arr) { + ArrayList> list = new ArrayList<>(); + ArrayList num = new ArrayList<>(); + int n = arr.length; + + Arrays.sort(arr); //sorting the array + + for(int i = 0; i < n-1; i++) { + int start = i+1, end = n-1; + + while(start < end) { + int sum = arr[i]+arr[start]+arr[end]; + + if(sum == 0) { + num.add(0, arr[i]); + num.add(1, arr[start]); + num.add(2, arr[end]); + start++; + end--; + } else if(sum > 0) { + end--; + } else { + start++; + } + + list.add(num); + } + } + + System.out.println(list); + + } + + public static void main(String[] args) { + int[] arr1 = {-1,0,1,2,-1,-4}; //[-4,-1,-1,0,1,2] + int[] arr2 = {0,1,1}; + int[] arr3 = {0,0,0}; + int[] arr = {}; + + _3Sum(arr1); + + } +} diff --git a/01-Array/Array30_3SumCloset.java b/01-Array/Array30_3SumCloset.java new file mode 100644 index 0000000..4db0c38 --- /dev/null +++ b/01-Array/Array30_3SumCloset.java @@ -0,0 +1,29 @@ +/* +Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target. +Return the sum of the three integers. +You may assume that each input would have exactly one solution. + +Example 1: +Input: nums = [-1,2,1,-4], target = 1 +Output: 2 +Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). + +Example 2: +Input: nums = [0,0,0], target = 1 +Output: 0 +Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0). + +Constraints: + + 3 <= nums.length <= 500 + -1000 <= nums[i] <= 1000 + -104 <= target <= 104 + + + */ + +public class Array30_3SumCloset { + public static void main(String[] args) { + + } +} diff --git a/01-Array/Array31_SubArraySumEqualsK.java b/01-Array/Array31_SubArraySumEqualsK.java new file mode 100644 index 0000000..303b052 --- /dev/null +++ b/01-Array/Array31_SubArraySumEqualsK.java @@ -0,0 +1,44 @@ +/* +Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k. +A subarray is a contiguous non-empty sequence of elements within an array. + +Example 1: +Input: nums = [1,1,1], k = 2 +Output: 2 + +Example 2: +Input: nums = [1,2,3], k = 3 +Output: 2 + +Constraints: + 1 <= nums.length <= 2 * 104 + -1000 <= nums[i] <= 1000 + -107 <= k <= 107 + */ + +public class Array31_SubArraySumEqualsK { + + public static int subArrayNumber(int[] arr, int k) { //TC -> O(n^2) || O(1) + int count = 0; + + for(int i = 0; i < arr.length; i++) { + if(arr[i] == k) count++; + int j = i+1; + int sum = arr[i]; + while(i < j) { + sum += arr[j]; + if(sum == k) count++; + j--; + } + } + + return count; + } + + public static void main(String[] args) { + int[] arr1 = {1,1,1}; + int[] arr2 = {1,2,3}; + + System.out.println(subArrayNumber(arr1, 1)); + } +} \ No newline at end of file diff --git a/01-Array/Array32_GameOfLife.java b/01-Array/Array32_GameOfLife.java new file mode 100644 index 0000000..af2b9f0 --- /dev/null +++ b/01-Array/Array32_GameOfLife.java @@ -0,0 +1,34 @@ +/* +According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970." +The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article): + Any live cell with fewer than two live neighbors dies as if caused by under-population. + Any live cell with two or three live neighbors lives on to the next generation. + Any live cell with more than three live neighbors dies, as if by over-population. + Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. +The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state. + +Example 1: +Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] +Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]] + +Example 2: +Input: board = [[1,1],[1,0]] +Output: [[1,1],[1,1]] + +Constraints: + m == board.length + n == board[i].length + 1 <= m, n <= 25 + board[i][j] is 0 or 1. + +Follow up: + Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells. + In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems? + */ + + +public class Array32_GameOfLife { + public static void main(String[] args) { + + } +} diff --git a/01-Array/Array33_4Sum.java b/01-Array/Array33_4Sum.java new file mode 100644 index 0000000..c5ee204 --- /dev/null +++ b/01-Array/Array33_4Sum.java @@ -0,0 +1,71 @@ +/* +Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: +0 <= a, b, c, d < n +a, b, c, and d are distinct. +nums[a] + nums[b] + nums[c] + nums[d] == target +You may return the answer in any order. + +Example 1: +Input: nums = [1,0,-1,0,-2,2], target = 0 +Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]] + +Example 2: +Input: nums = [2,2,2,2,2], target = 8 +Output: [[2,2,2,2]] + +Constraints: +1 <= nums.length <= 200 +-109 <= nums[i] <= 109 +-109 <= target <= 109 +*/ + +import java.util.ArrayList; +import java.util.List; +import java.util.Arrays; +public class Array33_4Sum { + + public static void _4Sum(int[] arr, int target) { + List> output = new ArrayList<>(); + int n = arr.length; //size of array + + Arrays.sort(arr); //sort the array + + for(int i = 0; i < n; i++) { + for(int j = i+1; j < n; j++) { + int start = j+1, end = n-1; + + while(start < end) { + int sum = arr[i]+arr[j]+arr[start]+arr[end]; + + if(sum == target) { + output.add(Arrays.asList(arr[i],arr[j],arr[start],arr[end])); + start++; + end--; + + while(start < end && arr[start] == arr[start+1]) start++; + while(start < end && arr[end] == arr[end-1]) end--; + } else if(sum > target) { + end--; + } else { + start++; + } + + } + while(j+1 < n && arr[j] == arr[j+1]) j++; + } + while(i+1 < n && arr[i] == arr[i+1]) i++; + } + + System.out.println(output); + } + + public static void main(String[] args) { + int[]arr1 = {1,0,-1,0,-2,2}; + int target1 = 0; + + int[] arr2 = {2,2,2,2,2}; + int target2 = 8; + + _4Sum(arr1, target1); + } +} diff --git a/01-Array/Array34_FIndDuplicateNumber.java b/01-Array/Array34_FIndDuplicateNumber.java new file mode 100644 index 0000000..b1f9c72 --- /dev/null +++ b/01-Array/Array34_FIndDuplicateNumber.java @@ -0,0 +1,30 @@ +/* +Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. +There is only one repeated number in nums, return this repeated number. +You must solve the problem without modifying the array nums and uses only constant extra space. + +Example 1: +Input: nums = [1,3,4,2,2] +Output: 2 + +Example 2: +Input: nums = [3,1,3,4,2] +Output: 3 + +Constraints: + 1 <= n <= 105 + nums.length == n + 1 + 1 <= nums[i] <= n + All the integers in nums appear only once except for precisely one integer which appears two or more times. + +Follow up: + How can we prove that at least one duplicate number must exist in nums? + Can you solve the problem in linear runtime complexity? + */ + + +public class Array34_FIndDuplicateNumber { + public static void main(String[] args) { + + } +} diff --git a/01-Array/Array35_SearchIn2DMatrix.java b/01-Array/Array35_SearchIn2DMatrix.java new file mode 100644 index 0000000..2d15b66 --- /dev/null +++ b/01-Array/Array35_SearchIn2DMatrix.java @@ -0,0 +1,61 @@ +/* +You are given an m x n integer matrix matrix with the following two properties: + +Each row is sorted in non-decreasing order. +The first integer of each row is greater than the last integer of the previous row. +Given an integer target, return true if target is in matrix or false otherwise. + +You must write a solution in O(log(m * n)) time complexity. + + + +Example 1: +Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 +Output: true + +Example 2: +Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 +Output: false + + +Constraints: + +m == matrix.length +n == matrix[i].length +1 <= m, n <= 100 +-104 <= matrix[i][j], target <= 104 + */ + +public class Array35_SearchIn2DMatrix { + + public static boolean searchInMatrix(int[][] matrix, int target) { + int m = matrix.length; + int n = matrix[1].length; + int start = 0, end = m * n - 1; + + while(start <= end) { + int mid = start+(end-start)/2; + int midVal = matrix[mid/n][mid%n]; + + if(midVal == target) { + return true; + } else if (midVal < target) { + start = mid+1; + } else { + end = mid-1; + } + } + + return false; + + } + + public static void main(String[] args) { + int[][] matrix = {{1,3,5,7},{10,11,16,20},{23,30,34,60}}; + int target = 7; + + // System.out.println(searchInMatrix(matrix, target)); + System.out.println((matrix.length)); + + } +}