Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions java/Arrays/LongestSubseqwithXOR.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class longestSubsequencewithXOR {
public int longestSubsequence(int[] nums) {
int xor = 0; // Variable to store cumulative XOR of all elements

// Compute XOR of all numbers in the array
for (int num : nums) {
xor = xor ^ num;
}

// If the total XOR is non-zero, the entire array itself is a valid subsequence
if (xor != 0) {
return nums.length;
}

// If total XOR is zero, check if there's at least one non-zero element
// Removing any one non-zero element will make the XOR non-zero
for (int num : nums) {
if (num != 0) {
return nums.length - 1;
}
}

// If all elements are zero, no non-zero XOR subsequence exists
return 0;
}
}
14 changes: 14 additions & 0 deletions java/Arrays/MaximumSubarray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class MaximumSubarray {
public int maxSubArray(int[] nums) {
int maxSum = nums[0]; // Best sum so far
int currentSum = nums[0]; // Current subarray sum

for (int i = 1; i < nums.length; i++) {
// If currentSum becomes negative, start new subarray
currentSum = Math.max(nums[i], currentSum + nums[i]);
maxSum = Math.max(maxSum, currentSum);
}

return maxSum;
}
}
17 changes: 17 additions & 0 deletions java/Arrays/MoveZeroes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class MoveZeroes {
public void moveZeroes(int[] nums) {
int index = 0; // Tracks where to place the next non-zero element

// First, move all non-zero elements to the front
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[index++] = nums[i];
}
}

// Fill the rest of the array with zeroes
while (index < nums.length) {
nums[index++] = 0;
}
}
}
22 changes: 22 additions & 0 deletions java/Arrays/RearrangeArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class RearrangeArray {
public int[] rearrangeArray(int[] nums) {
int n = nums.length; // Get the total number of elements in the input array
int ans[] = new int[n]; // Create a new array to store the rearranged elements

int pos = 0; // Pointer for placing positive numbers (even indices)
int neg = 1; // Pointer for placing negative numbers (odd indices)

// Traverse through each element in the input array
for (int i = 0; i < n; i++) {
if (nums[i] > 0) { // If the current number is positive
ans[pos] = nums[i]; // Place it at the current positive index
pos = pos + 2; // Move to the next positive position (skip one index)
} else { // Otherwise, it's a negative number
ans[neg] = nums[i]; // Place it at the current negative index
neg = neg + 2; // Move to the next negative position
}
}

return ans; // Return the rearranged array
}
}
20 changes: 20 additions & 0 deletions java/Arrays/TwoSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.HashMap;

class TwoSum {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>(); // Stores number → index

for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i]; // The number needed to reach target

if (map.containsKey(complement)) {
return new int[]{map.get(complement), i}; // Found the pair
}

map.put(nums[i], i); // Store current number and its index
}

return new int[]{}; // If no pair found
}
}