Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute of Savitribai Phule Pune
University)
Department of AIDS
Division A
Roll Number 83
PRN Number 12320039
Name Bhavesh Patil
Title:-
Implement the following programs in JavaScript
a) Continuous Subarray Sum: Given an unsorted array of non-negative integers and a
target sum, find a continuous subarray that sums up to the target sum and return the
left and right indices (1-based indexing) of that subarray.
https://www.geeksforgeeks.org/problems/subarray-with-given-sum-1587115621/1
b) Missing Number: Given an array of size N-1 containing distinct integers in the range
of 1 to N, find the missing element.
https://www.geeksforgeeks.org/problems/missing-number-in-array1416/1
c) Maximum Subarray Sum: Given an integer array, find the contiguous subarray
(containing at least one number) with the largest sum and return its sum.
https://leetcode.com/problems/maximum-subarray/description/
d) Longest Consecutive Sequence: Given an unsorted array of integers, find the length of
the longest consecutive elements sequence.
https://leetcode.com/problems/longest-consecutive-sequence/description/
It's specified that these problems should be solved using JavaScript and the solutions should
be submitted as a Word file containing code and screenshots of the successful submissions on
either LeetCode or GeeksforGeeks.
a) Continuous Subarray Sum: Given an unsorted array of non-negative integers and a target
sum, find a continuous subarray that sums up to the target sum and return the left and right
indices (1-based indexing) of that subarray.
https://www.geeksforgeeks.org/problems/subarray-with-given-sum-1587115621/1
Code:
class Solution
{
//Function to find a continuous sub-array which adds up to a given number.
subarraySum(arr, n, s)
{
//your code here
let result = [];
let found = false;
let st = 0, end = 0;
let sum = 0;
for (let i = 0; i < n; i++) {
sum += arr[i];
if (sum >= s) {
end = i;
while (s < sum && st < end) {
sum -= arr[st++];
}
}
if (sum === s) {
result.push(st + 1);
result.push(end + 1);
found = true;
break;
}
}
if (!found)
result.push(-1);
return result;
}
}
Output:
b) Missing Number: Given an array of size N-1 containing distinct integers in the range of 1
to N, find the missing element.
Code :-
class Solution{
missingNumber(a,N){
let xor1 = 0;
let xor2 = 0;
for (let i = 0; i < N - 1; i++) {
xor2 = xor2 ^ a[i];
xor1 = xor1 ^ (i + 1);
}
xor1 = xor1 ^ N; // XOR up to [1...N]
return xor1 ^ xor2;
}
}
Output:
c)Maximum Subarray Sum: Given an integer array, find the contiguous subarray (containing
at least one number) with the largest sum and return its sum.
Code:
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
let sum = 0;
let maxi = Number.MIN_SAFE_INTEGER;
for (let i of nums) {
sum += i;
maxi = Math.max(sum, maxi);
if (sum < 0) sum = 0;
}
return maxi;
};
Output:
d)Longest Consecutive Sequence: Given an unsorted array of integers, find the length of the
longest consecutive elements sequence.
Code:
/**
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function(arr) {
let n = arr.length;
if (n === 0) return 0;
// sort the array:
arr.sort((a, b) => a - b);
let lastSmaller = -Infinity;
let cnt = 0;
let longest = 1;
// find longest sequence:
for (let i = 0; i < n; i++) {
if (arr[i] - 1 === lastSmaller) {
// arr[i] is the next element of the
// current sequence.
cnt += 1;
lastSmaller = arr[i];
} else if (arr[i] !== lastSmaller) {
cnt = 1;
lastSmaller = arr[i];
}
longest = Math.max(longest, cnt);
}
return longest;
};
Output: