ARRAY
EASY QUESTIONS int first = Integer.MIN_VALUE;
[Link] ELEMENT IN ARRAY int second = Integer.MIN_VALUE;
Class Solution {
Public int findLargest( int[] nums ) { for (int num : arr) {
Int max = nums[0]; // Assume first element is if (num > first) {
largest
second = first;
For (int I = 1; I < [Link]; i++) {
first = num;
If (nums[i] > max) {
} else if (num > second && num != first) {
Max = nums[i]; // Update max if current
second = num;
element is larger
}}
}}
if (second == Integer.MIN_VALUE) {
Return max;
[Link]("No second largest
}}
element");
Optimal solution
} else {
Class Solution {
[Link]("Largest: " + first);
Public int findLargest(int[] nums) {
[Link]("Second Largest: " +
Int max = Integer.MIN_VALUE; second);
For (int num : nums) { } }}
If (num > max) { 2nd
Max = num; Class Solution {
}} Public int secondLargest(int[] nums) {
Return max; Int max = Integer.MIN_VALUE;
}} Int secondMax = Integer.MIN_VALUE;
[Link] LARGEST ELEMENT IN ARRAY // First pass to find the largest element
public class SecondLargest { For (int num : nums) {
public static void main(String[] args) { If (num > max) {
int[] arr = {12, 35, 1, 10, 34, 1}; Max = num;
}} Second_large = arr[i];
// Second pass to find the second largest }}
element
[Link](“Second smallest is
For (int num : nums) { “+second_small);
If (num > secondMax && num < max) { [Link](“Second largest is
“+second_large);
secondMax = num;
}
}}
[Link] if Array Is Sorted and Rotated
Return (secondMax == Integer.MIN_VALUE) ? -
1 : secondMax;
class TUF {
}} static boolean isSorted(int
Optimized Solution arr[], int n) {
for (int i = 0; i < n;
Int small = Integer.MAX_VALUE;
i++){
Int second_small = Integer.MAX_VALUE; for (int j = i + 1; j < n;
Int large = Integer.MIN_VALUE; j++){
if (arr[j] <arr[i])
Int second_large = Integer.MIN_VALUE;
return false;
For (int I = 0;I < n;i++) }}
{ return true;
}
Small = [Link](small,arr[i]);
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 n = [Link]; public void rotate(int[] nums) {
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
if (nums[i] > nums[(i + 1) % n]) { for (int i = n - 1; i > 0; i--) {
nums[i] = nums[i - 1];
count++; }
// Place the first element at the last position
} nums[n-1] = temp;
}}
if (count > 1) return false; // More than 1 break → [Link] ARRAY BY N times
not sorted & rotated
class Solution {
}
public void rotate(int[] nums, int k) {
return true;
int n = [Link];
}} k %= n; //handle cases where k > n
Example: nums = [3,4,5,1,2]: true
// Step 1: Reverse the whole array
[1,2,3,4,5] is the original sorted array. reverse(nums, 0, n - 1);
You can rotate the array by x = 3 // Step 2: Reverse first k elements
positions to begin on the element of reverse(nums, 0, k - 1);
// Step 3: Reverse remaining n - k elements
value 3: [3,4,5,1,2].
reverse(nums, k, n - 1);
[Link] Duplicates from Sorted Array }
private void reverse(int[] nums, int start, int end)
class Solution { {
public int removeDuplicates(int[] nums) while (start < end) {
// swap nums[start] and nums[end]
{
if ([Link] == 0) return 0;
int temp = nums[start];
nums[start] = nums[end];
int i = 0; nums[end] = temp;
start++;
end--;
for (int j = 1; j < [Link]; j++) {
}}}
if (nums[j] != nums[i]) { Second option
class Solution { int left = 0;
// Move all non-zero elements to the front
public void rotate(int[] arr, int k) { for (int i = 0; i < [Link]; i++) {
int n = [Link]; if (nums[i] != 0) {
k = k % n; // In case k > n nums[left] = nums[i];
int d = n - k; // Convert right rotation to left++;
equivalent left rotation }}
// Fill the remaining with zeros
// Step 1: Store first 'd' elements in temp
int[] temp = new int[d]; while (left < [Link]) {
for (int i = 0; i < d; i++) { nums[left] = 0;
temp[i] = arr[i]; left++;
} }}}
// Step 2: Shift remaining elements to the left [Link] Search
for (int i = d; i < n; i++) {
arr[i - d] = arr[i]; int search(int arr[],int n,int num) {
}
// Step 3: Copy temp elements to the end int i;
for(i=0;i<n;i++)
for (int i = n - d; i < n; i++) { {
if(arr[i]==num)
return i;
arr[i] = temp[i - (n - d)]; }
}}} return -1;
}
[Link]
[Link] ZEROES TO LAST OF ARRAY
class Solution {
Optimal Solution public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5};
class Solution { int[] arr2 = {3, 4, 5, 6, 7};
public void moveZeroes(int[] nums) {
int pointer = 0; // Use Set to store unique elements
Set<Integer> unionSet = new HashSet<>();
for (int i = 0; i < [Link]; i++) {
for (int num : arr1) {
if (nums[i] != 0) {
[Link](num);
// Swap non-zero element with left }
pointer for (int num : arr2) {
int temp = nums[pointer]; [Link](num);
}
nums[pointer] = nums[i]; // Print Union
nums[i] = temp; [Link]("Union of Arrays: " +
unionSet);
}}
pointer++;
[Link] NUMBER
}}}}
Optimal Solution
Second option
class Solution {
class Solution { public int missingNumber(int[] nums) {
public void moveZeroes(int[] nums) {
int n = [Link];
int actualSum= (n*(n+1))/2; for(int i=0;i<n;i++){
int num=nums[i];
int sum=0;
int count=0;
for(int i=0;i<n ; i++){ for (int j = 0; j < n; j++) {
sum= sum+nums[i]; if (arr[j] == num)
} count++;
}
return actualSum -sum;
// if the occurrence is 1 return ans:
}} if (count == 1) return num;
//This line will never execute
class Solution { //if the array contains a single element.
return -1;
public int missingNumber(int[] nums) { }}
int n = [Link]; Optimal Solution
for (int i = 0; i <= n; i++) {
class Solution {
public int singleNumber(int[] nums) {
boolean found = false;
int result = 0;
for (int j = 0; j < n; j++) {
for (int num : nums) {
result = result ^ num; // XOR operation
if (nums[j] == i) { }
found = true; return result;
break; }}
}}
MEDIUM QUESTIONS
if (!found) {
return i;
2SUM
}}
return -1; // Should never reach here class Solution {
}} public int[] twoSum(int[] nums, int target) {
11. MAXIMUM CONSECUTIVE ONES for (int i = 0; i < [Link]; i++) {
for (int j = i + 1; j < [Link]; j++) {
class Solution { if (nums[i] + nums[j] == target) {
public int findMaxConsecutiveOnes(int[] nums) { return new int[]{i, j};
int size=[Link]; }}}
int count=0; return new int[]{};
int maxi=0; }}
for (int i=0;i<size;i++) {
if(nums[i]==1) {
count++; import [Link];
maxi =[Link](maxi,count);
}
public class Solution {
else {
public int[] twoSum(int[] nums, int target) {
count=0;
HashMap<Integer, Integer> map = new HashMap<>(); //
}}
value -> index
return maxi;
for (int i = 0; i < [Link]; i++) {
}}
int complement = target - nums[i];
[Link] the number that appears once, and if ([Link](complement)) {
other numbers twice. return new int[] { [Link](complement), i };
}
class Solution { [Link](nums[i], i);
public int singleNumber(int[] nums) { }
int n = [Link]; throw new IllegalArgumentException("No two
sum solution");
}} nums[high] = temp;
SORT 0’s 1’s 2’s in array high--;
}}}}
Example 1:
Input: nums = [2,0,2,1,1,0] MAJORITY ELEMENT
Output: [0,0,1,1,2,2]
Example: nums = [2,2,1,1,1,2,2] output:2
Example 2: Output:
Input: nums = [2,0,1] class Solution {
Output: [0,1,2] public int majorityElement(int[] nums) {
int n=[Link];
class Solution {
public void sortColors(int[] nums) { for(int i=0;i<n;i++){
int count0 = 0, count1 = 0, count2 = 0; int count=0;
for(int j=0;j<n;j++){
// Count the number of 0s, 1s, and 2s if(nums[j]==nums[j])
for (int num : nums) { {
if (num == 0) count0++; count++;
else if (num == 1) count1++; }}
else count2++; if(count >n/2){
} return nums[i];
}}
// Overwrite the array based on counts return -1;
int index = 0; }}
Optimised solution
while (count0-- > 0) nums[index++] = 0; class Solution {
while (count1-- > 0) nums[index++] = 1; public int majorityElement(int[] nums) {
while (count2-- > 0) nums[index++] = 2; int count = 0;
}} int candidate = 0;
Optimised solution for (int num : nums) {
if (count == 0) {
public class SortColors { candidate = num;
public void sortColors(int[] nums) { }
int low = 0, mid = 0, high = [Link] - 1; count += (num == candidate) ? 1 : -1;
}
while (mid <= high) { return candidate;
if (nums[mid] == 0) { }}
// Swap nums[low] and nums[mid] KADANE ‘S MAXIMUM SUBARRAY SUM
int temp = nums[low];
nums[low] = nums[mid]; Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
nums[mid] = temp; Output: 6
low++;
Explanation: The subarray [4,-1,2,1] has the
mid++;
} else if (nums[mid] == 1) {
largest sum 6.
mid++; class Solution {
} else { public int maxSubArray(int[] nums) {
// nums[mid] == 2 int maxSum = Integer.MIN_VALUE; // to store max sum
// Swap nums[mid] and nums[high] int n = [Link];
int temp = nums[mid];
nums[mid] = nums[high];
for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { //
int currentSum = 0; Sell day after buy day
for (int j = i; j < n; j++) { int profit = prices[j] - prices[i];
currentSum += nums[j]; // sum of maxProfit = [Link](maxProfit, profit);
subarray from i to j }}
maxSum = [Link](maxSum, currentSum); return maxProfit;
}} }}
return maxSum; Optimised solution
}}
Optimised solution class Solution {
public int maxProfit(int[] prices) {
class Solution { int minPrice = Integer.MAX_VALUE;
public int maxSubArray(int[] nums) { int maxProfit = 0;
int maxSum = nums[0];// Initialize with
first element for (int price : prices) {
int currentSum = 0; if (price < minPrice) {
minPrice = price; // update the
for (int num : nums) { lowest price seen so far
currentSum += num; // Add current number to sum } else {
maxSum = [Link](maxSum, currentSum); // maxProfit = [Link](maxProfit, price - minPrice);
Update max if needed // potential profit
if (currentSum < 0) { }}
currentSum = 0; // Reset if sum is negative return maxProfit;
}} }}
return maxSum;
Rearrange the array in alternating positive and
}}
KADANES negative items
class Solution { class Solution {
public int maxSubArray(int[] nums) { public int[] rearrangeArray(int[] nums) {
int currentSum = nums[0]; int result[] = new int[[Link] ];
int maxSum = nums[0]; int positive=0;
int negative=1;
for (int i = 1; i < [Link]; i++) {
// Either start new subarray or extend for(int i=0;i<[Link];i++){
the existing one if(nums[i]>0){
currentSum = [Link](nums[i], currentSum + result[positive ]=nums[i];
nums[i]); positive=positive+2;
}
else{
// Update max if current is better result[negative]=nums[i];
maxSum = [Link](maxSum, currentSum); negative=negative+2;
} }}
return maxSum; return result;
}} }}
Stock Buy and Sell Leaders in array
class Solution {
import [Link].*;
public int maxProfit(int[] prices) {
int maxProfit = 0;
int n = [Link]; public class Solution {
public List<Integer> findLeaders(int[] arr) {
List<Integer> leaders = new ArrayList<>();
for (int i = 0; i < n; i++) { // Buy day
int n = [Link];
longestStreak = [Link](longestStreak,
int maxFromRight = arr[n - 1]; currentStreak);
[Link](maxFromRight); // Rightmost }
element is always a leader return longestStreak;
}}
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,
reverse to maintain original order // First pass: mark rows and columns
[Link](leaders); for (int i = 0; i < rows; i++) {
return leaders; for (int j = 0; j < cols; j++) {
}} 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]
Output: 4 for (int j = 0; j < cols; j++) {
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
class Solution { }}}
public int longestConsecutive(int[] nums) { private void markCol(int[][] matrix, int col) {
HashSet<Integer> set = new HashSet<>(); for (int i = 0; i < [Link]; i++) {
if (matrix[i][col] != 0) {
for (int num : nums) { matrix[i][col] = -1; // mark
[Link](num); // Add all elements to the HashSet }}}}
}
class Solution {
int longestStreak = 0;
public void setZeroes(int[][] matrix) {
int rows = [Link];
for (int num : set) { int cols = matrix[0].length;
// Check if it's the start of a sequence
if () {
int[] row = new int[rows]; // default 0
int currentNum = num;
int[] col = new int[cols]; // default 0
int currentStreak = 1;
// Step 1: Mark rows and columns
// Count consecutive elements
for (int i = 0; i < rows; i++) {
while ([Link](currentNum + 1))
for (int j = 0; j < cols; j++) {
{
if (matrix[i][j] == 0) {
currentNum++;
row[i] = 1;
currentStreak++;
col[j] = 1;
}
}}}
// 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++) {
// Step 1: Mark the first row, column, and // Swap matrix[i][j] with matrix[j][i]
col0 based on zero positions int temp = matrix[i][j];
for (int i = 0; i < rows; i++) { matrix[i][j] = matrix[j][i];
if (matrix[i][0] == 0) col0 = 0; // first matrix[j][i] = temp;
column marker }}
for (int j = 1; j < cols; j++) { // Step 2: Reverse each row
if (matrix[i][j] == 0) { for (int i = 0; i < n; i++) {
matrix[i][0] = 0; // mark row int left = 0, right = n - 1;
matrix[0][j] = 0; // mark column while (left < right) {
}}} // Swap matrix[i][left] and matrix[i][right]
// Step 2: Use the markers to update the int temp = matrix[i][left];
cells (except first row and col) matrix[i][left] = matrix[i][right];
for (int i = 1; i < rows; i++) { matrix[i][right] = temp;
for (int j = 1; j < cols; j++) { left++;
if (matrix[i][0] == 0 || matrix[0][j] == 0) right--;
{ }}}}
matrix[i][j] = 0; HARD PROBLEMS
}}}
// Step 3: Update the first row if needed PASCAL TRIANGLE
if (matrix[0][0] == 0) {
for (int j = 1; j < cols; j++) { [Link] the value of element by number of row
matrix[0][j] = 0;
}} and column
public class Pascal {
// Step 4: Update the first column if needed
if (col0 == 0) {
for (int i = 0; i < rows; i++) { // Function to calculate nCr efficiently
matrix[i][0] = 0; public static long nCr(int n, int r) {
}}}} long res = 1;
ROTATE IMAGE
// Since C(n, r) = C(n, n - r)
class Solution { if (r > n - r)
public void rotate(int[][] matrix) { r = n - r;
int n = [Link];
int[][] temp = new int[n][n]; // extra matrix for (int i = 0; i < r; i++) {
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].*;
public class Main {
public class Solution {
public static long nCr(int n, int r) {
public List<List<Integer>> generate(int numRows)
long res = 1;
{
// calculating nCr:
List<List<Integer>> triangle = new
for (int i = 0; i < r; i++) {
ArrayList<>();
res = res * (n - i);
res = res / (i + 1);
} for (int row = 0; row < numRows; row++) {
return res; List<Integer> currentRow = new
} ArrayList<>();
public static void pascalTriangle(int n) { [Link](1); // first element is always 1
// printing the entire row n: // Fill the middle elements based on previous row
for (int c = 1; c <= n; c++) { for (int col = 1; col < row; col++) {
[Link](nCr(n - 1, c - 1) + " int val = [Link](row -
"); 1).get(col - 1) + [Link](row - 1).get(col);
} [Link](val);
[Link](); }
}
if (row > 0) {
[Link](1); // last element
public class Solution { is also 1 if row > 0
}
// Helper function to calculate nCr
public int nCr(int n, int r) { [Link](currentRow);
long res = 1; }
for (int i = 0; i < r; i++) { return triangle;
res = res * (n - i); }
res = res / (i + 1); }
}
return (int) res;
} Majority Elements(N/3 times)
// Main function to generate Pascal's Triangle
public List<List<Integer>> generate(int numRows) class Solution {
{ public List<Integer> majorityElement(int[] nums)
List<List<Integer>> ans = new ArrayList<>(); {
int n = [Link];
List<Integer> result = new ArrayList<>();
for (int row = 1; row <= numRows; row++) {
for (int i = 0; i < n; i++) {
// Skip if nums[i] is already in result }
if ([Link]() == 0 ||
[Link](0) != nums[i]) { return result;
if ([Link]() == 2 && }
[Link](1) == nums[i]) continue; }
int count = 0;
for (int j = 0; j < n; j++) {
if (nums[j] == nums[i]) {
count++; Optimal Solution
}
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;
if ([Link]() == 2) break; // 1st pass: Find potential candidates
} for (int num : nums) {
if (count1 == 0 && num != candidate2) {
return result; count1 = 1;
} candidate1 = num;
} } else if (count2 == 0 && num !=
import [Link].*; candidate1) {
class Solution { count2 = 1;
public List<Integer> majorityElement(int[] nums) candidate2 = num;
{ } else if (num == candidate1) {
int n = [Link]; count1++;
List<Integer> result = new ArrayList<>(); } else if (num == candidate2) {
count2++;
} else {
for (int i = 0; i < n; i++) {
count1--;
// Skip if nums[i] is already in result
count2--;
if ([Link]() == 0 ||
}
[Link](0) != nums[i]) {
}
if ([Link]() == 2 &&
[Link](1) == nums[i]) continue;
// 2nd pass: Verify actual counts
count1 = 0;
int count = 0;
count2 = 0;
for (int j = 0; j < n; j++) {
for (int num : nums) {
if (nums[j] == nums[i]) {
if (num == candidate1) count1++;
count++;
else if (num == candidate2) count2++;
}
}
}
List<Integer> result = new ArrayList<>();
if (count > n / 3) {
int threshold = n / 3;
[Link](nums[i]);
}
} if (count1 > threshold)
[Link](candidate1);
if ([Link]() == 2) break;
if (count2 > threshold) public List<List<Integer>> threeSum(int[] nums)
[Link](candidate2); {
int n = [Link];
return result; Set<List<Integer>> st = new HashSet<>();
} for (int i = 0; i < n; i++) {
} Set<Integer> hashset = new HashSet<>();
for (int j = i + 1; j < n; j++) {
int third = -(nums[i] + nums[j]);
if ([Link](third)) {
List<Integer> temp =
[Link](nums[i], nums[j], third);
[Link](temp);
[Link](temp);
}
[Link](nums[j]);
}
}
return new ArrayList<>(st);
}
}
Optimal Solution
import [Link].*;
3 SUM
class Solution {
class Solution { public List<List<Integer>> threeSum(int[] nums)
public List<List<Integer>> threeSum(int[] nums) {
{ List<List<Integer>> ans = new ArrayList<>();
int n = [Link]; [Link](nums);
Set<List<Integer>> set = new HashSet<>(); int n = [Link];
// Try every triplet combination for (int i = 0; i < n; i++) {
for (int i = 0; i < n; i++) { // Skip duplicates for the first number
for (int j = i + 1; j < n; j++) { if (i > 0 && nums[i] == nums[i - 1])
for (int k = j + 1; k < n; k++) { continue;
if (nums[i] + nums[j] + nums[k] == 0) {
List<Integer> triplet =
int j = i + 1;
[Link](nums[i], nums[j], nums[k]);
int k = n - 1;
[Link](triplet);
// Sort to avoid duplicate permutations
[Link](triplet); while (j < k) {
// Set automatically handles duplicates int sum = nums[i] + nums[j] +
} nums[k];
}
} if (sum < 0) {
} j++;
return new ArrayList<>(set); } else if (sum > 0) {
} k--;
} } else {
[Link]([Link](nums[i], nums[j], nums[k]));
class Solution { j++;
k--;
//Skip duplicates for second and third numbers
while (j < k && nums[j] == nums[j - 1]) j++; class Solution {
while (j < k && nums[k] == nums[k + 1]) k--; public int maxLengthZeroSumSubarray(int[] nums)
} {
} int max = 0;
} int n = [Link];
for (int i = 0; i < n; ++i) {
int sum = 0;
return ans;
for (int j = i; j < n; ++j) {
}
sum += nums[j];
}
if (sum == 0) {
max = [Link](max, j - i + 1);
}
}
}
return max;
}
}
Largest Subarray with 0 Sum
Optimal Solution
int maxLen(int A[], int n)
{
// Your code here
HashMap<Integer, Integer> mpp = new
HashMap<Integer, Integer>();
int maxi = 0;
int sum = 0;
for(int i = 0;i<n;i++) {
sum += A[i];
if(sum == 0) {
maxi = i + 1;
}
else {
if([Link](sum) != null) {
maxi = [Link](maxi, i -
[Link](sum));
}
else {
[Link](sum, i);
}
}
}
return maxi;
}
Count number of subarrays with given xor K public class tUf {
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);
}
public class tUf {
// Insert the prefix xor till index i
// into the map:
public static int subarraysWithXorK(int []a, int
if ([Link](xr)) {
k) {
[Link](xr, [Link](xr) + 1);
int n = [Link]; //size of the given array.
} else {
int cnt = 0;
[Link](xr, 1);
}
// Step 1: Generating subarrays: }
for (int i = 0; i < n; i++) { return cnt;
int xorr = 0; }
for (int j = i; j < n; j++) {
//step 2:calculate XOR of all
// elements:
xorr = xorr ^ a[j];
// step 3:check XOR and count:
if (xorr == k) cnt++;
}
}
return cnt;
}