JsPrQs
JsPrQs
JsPrQs
Beginner Questions
1. Find a Pair with a Specific Sum in a Sorted Array
function findPairWithSum(arr, target) {
let left = 0;
let right = arr.length - 1;
// Test
console.log(findPairWithSum([1, 2, 3, 4, 5, 6], 9)); // Output: [3, 6]
console.log(findPairWithSum([1, 2, 3, 4], 10)); // Output: null
// Test
console.log(isPalindrome([1, 2, 3, 2, 1])); // Output: true
console.log(isPalindrome([1, 2, 3])); // Output: false
// Test
console.log(mergeSortedArrays([1, 3, 5], [2, 4, 6])); // Output: [1, 2, 3, 4, 5, 6]
Intermediate Questions
4. Move Zeros to the End
function moveZeros(arr) {
let left = 0;
return arr;
}
// Test
console.log(moveZeros([0, 1, 0, 3, 12])); // Output: [1, 3, 12, 0, 0]
// Test
console.log(findClosestPair([1, 3, 4, 7, 10], 8)); // Output: [3, 4]
return count;
}
// Test
console.log(countPairsWithSum([1, 2, 3, 4, 5, 6], 7)); // Output: 2 (pairs: [1,6] and
[2,5])
Advanced Questions
7. Find Triplets with a Specific Sum
function findTriplets(arr, target) {
let triplets = [];
arr.sort((a, b) => a - b);
return triplets;
}
// Test
console.log(findTriplets([1, 2, 3, 4, 5], 9)); // Output: [[1, 3, 5], [2, 3, 4]]
return max;
}
// Test
console.log(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])); // Output: 49
deque.push(i);
return result;
}
// Test
console.log(maxSubarrayK([1, 3, -1, -3, 5, 3, 6, 7], 3)); // Output: [3, 3, 5, 5, 6, 7]
Let me know if you need further clarification on any specific question or topic! 😊