Posts

Search a 2D Matrix LeetCode

Image
 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 Brute Force : class Solution {     public boolean searchMatrix ( int [][] matrix , int target ) {             int rows = matrix . length ;         int cols = matrix[ 0 ]. length ;         for ( int i = 0 ; i < rows; i++) {             for ( int j = 0 ; j < cols; j++) {                 if ...

Richest Customer Wealth problem LeetCode

 You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ bank. Return the wealth that the richest customer has. A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.   Example 1: Input: accounts = [[1,2,3],[3,2,1]] Output: 6 Explanation: 1st customer has wealth = 1 + 2 + 3 = 6 2nd customer has wealth = 3 + 2 + 1 = 6 Both customers are considered the richest with a wealth of 6 each, so return 6. Example 2: Input: accounts = [[1,5],[7,3],[3,5]] Output: 10 Explanation:  1st customer has wealth = 6 2nd customer has wealth = 10  3rd customer has wealth = 8 The 2nd customer is the richest with a wealth of 10. Example 3: Input: accounts = [[2,8,7],[7,1,3],[1,9,5]] Output: 17 code :- public class codefile {     static int solve ( List < List < Integer >>  inp...

Minimum Size Subarray Sum LeetCode

 Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.   Example 1: Input: target = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: The subarray [4,3] has the minimal length under the problem constraint. Example 2: Input: target = 4, nums = [1,4,4] Output: 1 Example 3: Input: target = 11, nums = [1,1,1,1,1,1,1,1] Output: 0 Optimal Solution: Optimized Sliding Window Approach (O(N)) Instead of checking all subarrays, we use a sliding window : Expand the window (right pointer) until sum ≥ target. Shrink the window (left pointer) while sum ≥ target. Keep track of the minimum valid window size. class Solution {     public int minSubArrayLen ( int target , int [] nums ) {         int left = 0 , sum = 0 , minLength = Integer . MAX_VALUE ;         for ( int right = 0 ...

Sum of Subarray Ranges LeetCode

 You are given an integer array nums. The range of a subarray of nums is the difference between the largest and smallest element in the subarray. Return the sum of all subarray ranges of nums. A subarray is a contiguous non-empty sequence of elements within an array.   Example 1: Input: nums = [1,2,3] Output: 4 Explanation: The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0  [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4. Example 2: Input: nums = [1,3,3] Output: 4 Explanation: The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. Example 3: Input: nums = [4,-2,-3,4,1] Output: 59 Explanation: The sum of...

Spiral Matrix problem Leet Code

Image
 Given an m x n matrix, return all elements of the matrix in spiral order. Example 1: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] Output: [1,2,3,4,8,12,11,10,9,5,6,7]   class Solution {     public List < Integer > spiralOrder ( int [][] matrix ) {                   List < Integer > result = new ArrayList <>();         int rows = matrix . length ;         int cols = matrix[ 0 ]. length ;         int top = 0 , bottom = rows - 1 , left = 0 , right = cols - 1 ;         while (top <= bottom && left <= right) {             // Move right             for ( int i = left; i <= right; i++) {                 result . add (...

Sprial Matrix || problem leetcode

Image
 Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.   Example 1: Input: n = 3 Output: [[1,2,3],[8,9,4],[7,6,5]] Example 2: Input: n = 1 Output: [[1]]   Constraints: 1 <= n <= 20 class Solution {     public int [][] generateMatrix ( int n ) {         int [][] matrix = new int [n][n];         int top = 0 , bottom = n - 1 , left = 0 , right = n - 1 ;         int num = 1 ; // Start filling from 1 to n^2         while (num <= n * n) {             // Move right             for ( int i = left; i <= right; i++) {                 matrix[top][i] = num++;             }             top++;             // Move down     ...

Maximum of Absolute Value Expression LeetCode Problem

 Given two arrays of integers with equal lengths, return the maximum value of: |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j| where the maximum is taken over all 0 <= i, j < arr1.length. Example 1: Input: arr1 = [1,2,3,4], arr2 = [-1,4,5,6] Output: 13 Example 2: Input: arr1 = [1,-2,-5,0,10], arr2 = [0,-2,-1,-7,-4] Output: 20 Deriving the Four Cases We consider all possible combinations of signs for the expression: (arr1[i]−arr1[j])+(arr2[i]−arr2[j])+(i−j) To handle absolute values, we expand this expression into four possible cases: Case 1: arr1[i]>arr1[j] & arr2[i]>arr2[j] (arr1[i]+arr2[i]+i)−(arr1[j]+arr2[j]+j) Case 2:arr1[i]>arr1[j] & arr2[i]<arr2[j] (arr1[i]-arr2[i]+i)−(arr1[j]-arr2[j]+j) Case 3::arr1[i]<arr1[j] & arr2[j]>arr2[j] (arr2[j]- arr1[i]+i)−(arr2[j]- arr1[j]+j) Case 4:arr1[i]<arr1[j] & arr2[i]<arr2[j] (i-arr1[i]−arr2[i])−(j-arr1[j]−arr2[j]) Both arr2 and i contribute negatively. Why These Four Cases? Since |X - Y| mea...

Next Permutation LeetCode Problem

 A permutation of an array of integers is an arrangement of its members into a sequence or linear order. For example, for arr = [1,2,3], the following are all the permutations of arr: [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1]. The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order). For example, the next permutation of arr = [1,2,3] is [1,3,2]. Similarly, the next permutation of arr = [2,3,1] is [3,1,2]. While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement. Given an array of integers nums, find the ...

Permutation of Array Problem

 Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Example 1: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example 2: Input: nums = [0,1] Output: [[0,1],[1,0]] Example 3: Input: nums = [1] Output: [[1]] class Solution {     public List < List < Integer >> permute ( int [] nums ) {         int start = 0 ;         List < List < Integer >> result = new ArrayList <>();       generatePermutation (nums, start, result); // Call the function (no return here)         return result; // Now return the result     }     public static void swap ( int [] arr , int i , int j ) {         int temp = arr[i];         arr[i] = arr[j];         arr[j]=temp;           } ...

Maximum SubArray Sum Problem LeetCode

 Given an integer array nums, find the subarray with the largest sum, and return its sum.   Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has the largest sum 6. Example 2: Input: nums = [1] Output: 1 Explanation: The subarray [1] has the largest sum 1. Example 3: Input: nums = [5,4,-1,7,8] Output: 23 Explanation: The subarray [5,4,-1,7,8] has the largest sum 23. Correct Approach (Kadane’s Algorithm) We need: A variable currentSum to track the running sum. A variable maxSum to store the maximum encountered sum. If currentSum becomes negative, reset it to 0 . class Solution {     public int maxSubArray ( int [] nums ) {             int maxSum = nums[ 0 ];   // Initialize with first element         int currentSum = 0 ;         for ( int num : nums) {             if (currentSum < 0 ) {   ...