Array (1)
Array (1)
Class Solution {
[Link] LARGEST ELEMENT IN ARRAY // First pass to find the largest element
Large = [Link](large,arr[i]);
Optimised Solution
}
class TUF {
For (int I = 0;I < n;i++)
static boolean isSorted(int arr[], int n) {
{
for (int i = 1; i < n; i++) {
If (arr[i] < second_small && arr[i] != small)
if (arr[i] < arr[i - 1]).
{
return false;
Second_small = arr[i];
}
}
return true;
If (arr[i] > second_large && arr[i] != large)
}
{
i++;
nums[i] = nums[j];
}}
return i + 1;
}}
class Solution { [Link] ARRAY BY one place
public boolean check(int[] nums) { class Solution {
int count = 0;
int n = [Link];
// Store the first element
int temp = nums[0];
for (int i = 0; i < n; i++) { // Shift all elements one step to the right
class Solution {
Second option public int removeDuplicates(int[]
class Solution { nums) {
public void moveZeroes(int[] nums) { if ([Link] == 0) return 0;
}}
int i = 0; 11. MAXIMUM CONSECUTIVE ONES
class Solution {
for (int j = 1; j < [Link]; public int findMaxConsecutiveOnes(int[] nums) {
j++) { int size=[Link];
int count=0;
if (nums[j] != nums[i]) {
int maxi=0;
i++; for (int i=0;i<size;i++) {
nums[i] = nums[j]; if(nums[i]==1) {
} count++;
maxi =[Link](maxi,count);
}
}
else {
return i + 1; count=0;
} }}
return maxi;
} }}
[Link] NUMBER [Link] the number that appears once, and
Optimal Solution other numbers twice.
import [Link];
while (mid <= high) {
if (nums[mid] == 0) {
public class Solution { // Swap nums[low] and nums[mid]
public int[] twoSum(int[] nums, int target) { int temp = nums[low];
HashMap<Integer, Integer> map = new HashMap<>(); // nums[low] = nums[mid];
value -> index nums[mid] = temp;
for (int i = 0; i < [Link]; i++) { low++;
int complement = target - nums[i]; mid++;
if ([Link](complement)) { } else if (nums[mid] == 1) {
return new int[] { [Link](complement), i }; mid++;
} } else {
[Link](nums[i], i); // nums[mid] == 2
} // Swap nums[mid] and nums[high]
throw new IllegalArgumentException("No two int temp = nums[mid];
sum solution"); nums[mid] = nums[high];
}} nums[high] = temp;
SORT 0’s 1’s 2’s in array high--;
}}}}
Example 1:
Leaders in array
// Count consecutive elements
import [Link].*; while ([Link](currentNum + 1))
{
currentNum++;
public class Solution {
currentStreak++;
public List<Integer> findLeaders(int[] arr) {
}
List<Integer> leaders = new ArrayList<>();
int n = [Link];
longestStreak = [Link](longestStreak,
currentStreak);
int maxFromRight = arr[n - 1];
}
[Link](maxFromRight); // Rightmost
return longestStreak;
element is always a leader
}}
SET MATRIX ZEROES
// Traverse from second last to the beginning
for (int i = n - 2; i >= 0; i--) { class Solution {
if (arr[i] > maxFromRight) { public void setZeroes(int[][] matrix) {
maxFromRight = arr[i]; int rows = [Link];
[Link](maxFromRight); int cols = matrix[0].length;
}}
// Leaders collected in reverse order,
// First pass: mark rows and columns
reverse to maintain original order
for (int i = 0; i < rows; i++) {
[Link](leaders);
for (int j = 0; j < cols; j++) {
return leaders;
if (matrix[i][j] == 0) {
}}
markRow(matrix, i);
markCol(matrix, j);
}}}
Longest Consecutive Sequence // Second pass: convert all -1 to 0
for (int i = 0; i < rows; i++) {
Input: nums = [100,4,200,1,3,2]
for (int j = 0; j < cols; j++) {
Output: 4
Explanation: The longest consecutive elements sequence is [1, if (matrix[i][j] == -1) {
2, 3, 4]. Therefore its length is 4. matrix[i][j] = 0;
}}}}
Optimised Solution private void markRow(int[][] matrix, int row) {
for (int j = 0; j < matrix[0].length; j++) {
import [Link]; if (matrix[row][j] != 0) {
matrix[row][j] = -1; // mark
}}} for (int i = 1; i < rows; i++) {
private void markCol(int[][] matrix, int col) { for (int j = 1; j < cols; j++) {
for (int i = 0; i < [Link]; i++) { if (matrix[i][0] == 0 || matrix[0][j] == 0)
if (matrix[i][col] != 0) { {
matrix[i][col] = -1; // mark matrix[i][j] = 0;
}}}} }}}
// Step 3: Update the first row if needed
if (matrix[0][0] == 0) {
class Solution { for (int j = 1; j < cols; j++) {
public void setZeroes(int[][] matrix) { matrix[0][j] = 0;
int rows = [Link]; }}
int cols = matrix[0].length;
// Step 4: Update the first column if needed
int[] row = new int[rows]; // default 0 if (col0 == 0) {
int[] col = new int[cols]; // default 0 for (int i = 0; i < rows; i++) {
matrix[i][0] = 0;
// Step 1: Mark rows and columns }}}}
for (int i = 0; i < rows; i++) { ROTATE IMAGE
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 0) { class Solution {
row[i] = 1; public void rotate(int[][] matrix) {
col[j] = 1; int n = [Link];
}}} int[][] temp = new int[n][n]; // extra matrix
// Step 2: Set matrix cells to 0 if their // Copy elements to temp in rotated position
row or column is marked for (int i = 0; i < n; i++) {
for (int i = 0; i < rows; i++) { for (int j = 0; j < n; j++) {
for (int j = 0; j < cols; j++) { temp[j][n - 1 - i] = matrix[i][j];
if (row[i] == 1 || col[j] == 1) { }}
matrix[i][j] = 0; // Copy back to original matrix
}}}} for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = temp[i][j];
Optimised solution }}}}
Optimised solution
class Solution {
public void setZeroes(int[][] matrix) { class Solution {
int rows = [Link]; public void rotate(int[][] matrix) {
int cols = matrix[0].length; int n = [Link];
int col0 = 1; // Track whether the first // Step 1: Transpose the matrix
column needs to be zeroed for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Swap matrix[i][j] with matrix[j][i]
// Step 1: Mark the first row, column, and
int temp = matrix[i][j];
col0 based on zero positions
matrix[i][j] = matrix[j][i];
for (int i = 0; i < rows; i++) {
matrix[j][i] = temp;
if (matrix[i][0] == 0) col0 = 0; // first
}}
column marker
// Step 2: Reverse each row
for (int j = 1; j < cols; j++) {
for (int i = 0; i < n; i++) {
if (matrix[i][j] == 0) {
int left = 0, right = n - 1;
matrix[i][0] = 0; // mark row
while (left < right) {
matrix[0][j] = 0; // mark column
// Swap matrix[i][left] and matrix[i][right]
}}}
int temp = matrix[i][left];
// Step 2: Use the markers to update the
matrix[i][left] = matrix[i][right];
cells (except first row and col)
matrix[i][right] = temp; }
left++; [Link]();
right--; }
}}}}
HARD PROBLEMS
public class Solution {
PASCAL TRIANGLE
// Helper function to calculate nCr
[Link] the value of element by number of row public int nCr(int n, int r) {
long res = 1;
and column
public class Pascal { for (int i = 0; i < r; i++) {
res = res * (n - i);
res = res / (i + 1);
// Function to calculate nCr efficiently
}
public static long nCr(int n, int r) {
return (int) res;
long res = 1;
}
// Main function to generate Pascal's Triangle
// Since C(n, r) = C(n, n - r) public List<List<Integer>> generate(int numRows)
if (r > n - r) {
r = n - r; List<List<Integer>> ans = new ArrayList<>();
for (int i = 0; i < r; i++) { for (int row = 1; row <= numRows; row++) {
res = res * (n - i); List<Integer> tempList = new ArrayList<>();
res = res / (i + 1); for (int col = 1; col <= row; col++) {
} [Link](nCr(row - 1, col - 1));
return res; }
} [Link](tempList);
}
public static int pascalTriangle(int r, int c) {
int element = (int) nCr(r - 1, c - 1);
return ans;
return element;
}
}
}
R=5,C=3 5*4*3/3*2*1 =6 Optimal Solution
[Link] elements in row import [Link].*;
int count = 0;
for (int j = 0; j < n; j++) {
if (nums[j] == nums[i]) { Optimal Solution
count++;
} class Solution {
} public List<Integer> majorityElement(int[] nums)
{
if (count > n / 3) { int n = [Link];
[Link](nums[i]); int count1 = 0, count2 = 0;
} int candidate1 = Integer.MIN_VALUE,
} candidate2 = Integer.MIN_VALUE;
Optimal Solution
class Solution {
public static int subarraysWithXorK(int []a, int
public int subarraysWithXorK(int[] nums, int k)
k) {
{
int n = [Link]; //size of the given array.
int count = 0;
int xr = 0;
int n = [Link];
Map<Integer, Integer> mpp = new HashMap<>();
//declaring the map.
for (int i = 0; i < n; i++) { [Link](xr, 1); //setting the value of 0.
for (int j = i; j < n; j++) { int cnt = 0;
int xor = 0;
for (int l = i; l <= j; l++) {
for (int i = 0; i < n; i++) {
xor ^= nums[l];
// prefix XOR till index i:
}
xr = xr ^ a[i];
if (xor == k) count++;
}
} //By formula: x = xr^k:
int x = xr ^ k;
return count;
} // add the occurrence of xr^k
} // to the count:
if ([Link](x)) {
cnt += [Link](x);
}