_Day 1 Problem Statements & Systematic Approaches
_Day 1 Problem Statements & Systematic Approaches
//* Session Focus: Interpreting problem statements, breaking down problems systematically.
function isPositive(num) {
if (num > 0) {
return "Positive";
} else if (num == 0) {
return "Zero";
} else {
return "Negative";
// console.log(maxMiddleMin(1, 2, 3));
// console.log(maxMiddleMin(3, 2, 1));
// console.log(maxMiddleMin(2, 3, 1));
// console.log(maxMiddleMin(2, 1, 3));
// console.log(maxMiddleMin(1, 3, 2));
// console.log(maxMiddleMin(3, 1, 2));
function countDigits(num) {
if (num == 0) return 1;
let count = 0;
count++;
return count;
// return regex.test(str);
// }
function isAlphabetOnly(str) {
if (
){
return false;
return true;
// console.log("a".charCodeAt(0));
// console.log("z".charCodeAt(0));
// console.log("A".charCodeAt(0));
// console.log("Z".charCodeAt(0));
function areaOfCircle(rad) {
function isVowel(char) {
// return char == 'a' || char == 'e' || char == 'i' || char == 'o' || char == 'u' || char == 'A' || char == 'E' || char == 'I' ||
char == 'O' || char == 'U'
module.exports = {
isPositive,
sumOfTwoNumbers,
maxMiddleMin,
countDigits,
isAlphabetOnly,
areaOfCircle,
isVowel,
};
function difference1(a, b) {
return a > b ? a - b : b - a;
function difference2(a, b) {
function isEvenOrOdd(num) {
function findLargest(a, b, c, d) {
function averageOfThree(a, b, c) {
return (a + b + c) / 3;
function countVowels(str) {
let count = 0;
if (vowels.includes(char)) {
count++;
return count;
function isUpperCase(char) {
return /^[A-Z]$/.test(char);
}
function reverseString(str) {
return str.split("").reverse().join("");
function square(num) {
return num ** 2;
module.exports = {
isPositive,
sumOfTwoNumbers,
maxMiddleMin,
countDigits,
isAlphabetOnly,
areaOfCircle,
isVowel,
difference1,
difference2,
};
Session 2
// console.log(num1, num2);
// num2 = temp;
// console.log(num1, num2);
// }
// console.log(num1, num2);
// }
// console.log(num1, num2);
// }
// console.log(num1, num2)
// }
// console.log(num1, num2)
// console.log(num1, num2)
// }
// swap(10, 15);
// swapWithoutTemp(10, 15)
//* Session Focus: Solving fundamental math problems and creating patterns.
function sumOfDigits(num) {
let sum = 0;
// console.log(sumOfDigits(1372))
function fact(num) {
// 5 => 5 * 4 * 3 * 2 * 1 = 120
if (num == 0) return 1;
let factorial = 1;
return factorial;
}
// console.log(fact(5));
// 0 1 1 2 3 5 8 13 21 34 55 89
function fibonacci(n) {
let x = 0;
let y = 1;
if (n < 1) return;
if (n < 2) {
console.log(0);
return;
if (n < 3) {
console.log(0);
console.log(1);
return;
console.log(x);
x = x + y;
y = x - y;
// console.log(...fibonacci(10));
function isPrime(num) {
if (num < 2) return false;
// let x = num / 2;
return true;
// console.time("isPrime");
// const x = isPrime(1057438801);
// console.timeEnd("isPrime");
// console.log(x);
// 5
// * * * * *
// * *
// * *
// * *
// * * * * *
function hallowSquare(n) {
if (
row == 0 ||
col == 0 ||
row == col ||
row == n - col - 1 ||
row == Math.floor(n / 2) ||
col == Math.floor(n / 2) ||
row == n - 1 ||
col == n - 1
line.push("*");
console.log(line.join(" "));
// hallowSquare(15);
function patterns(n) {
if ((row < n / 2 && col < n / 2) || (row >= n / 2 && col >= n / 2)) {
if ((row+col) % 2 == 0) line.push("*");
else line.push("#");
console.log(line.join(" "));
patterns(10);
module.exports = { sumOfDigits };
function findLCM_GCD(a, b) {
function gcd(x, y) {
function numberPyramid_Iterative(n) {
console.log(str);
}
numberPyramid_Iterative(5);
function findGCD_Iterative(a, b) {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
return a;
function isPalindrome_String(num) {
function isPalindrome_Numeric(num) {
function invertedTriangle_Iterative(n) {
console.log("*".repeat(i));
invertedTriangle_Iterative(5);
function areCoprime_GCD(a, b) {
function gcd(x, y) {
// Bottom half
diamondPattern_TwoLoops(5);
function pascalTriangle_Iterative(n) {
row.push(1);
triangle.push(row);
}
pascalTriangle_Iterative(5);
function findDivisors_Loop(n) {
if (n % i === 0) {
divisors.push(i);
function checkerboardPattern_Nested(n) {
console.log(row);
checkerboardPattern_Nested(5);
Session 3
//* Session Focus: Basic string operations, string traversal, and manipulation techniques.
// return str.split("").reverse().join("")
revStr += str.charAt(ind);
return revStr;
// console.log(reverseStr("Hello World!"));
function countVowAndCons(str) {
if ("aeiou".indexOf(str.charAt(ind)) != -1) {
obj.vowels++;
obj.consonants++;
}
return obj;
function toLowerCase(str) {
} else {
res += str.charAt(ind);
return res;
function toUpperCase(str) {
} else {
res += str.charAt(ind);
return res;
}
function convertTo(str, toUpper = true) {
if (toUpper) {
convertedStr = toUpperCase(str);
} else {
convertedStr = toLowerCase(str);
// if (toUpper) {
// convertedStr += char;
// } else {
// }
// if (toUpper) {
// } else {
// // to lowercase?
// convertedStr += char;
// }
// } else {
// // it is not an alphabet?
// convertedStr += char;
// }
// }
return convertedStr;
function findLongestWord(str) {
return longest;
function isPalindrome(str) {
let left = 0,
right = str.length - 1;
while (left < right) {
if (str[left] != str[right]) {
return false;
left++;
right--;
return true;
// console.log(isPalindrome("madam"));
// console.log(isPalindrome("racecar"));
// console.log(isPalindrome("javascript"));
function removeDuplicates(str) {
if (!uniqueStr.includes(char)) {
uniqueStr += char;
return uniqueStr;
function allSubstrings(str) {
substring += str[ind];
substrings.push(substring);
return substrings;
// console.log(allSubstrings("string"));
result += str1[i];
result += str2[i];
return result;
console.log(concatenateStrings("Hello", "World"));
function frequencyCounter(str) {
let freq = {};
if (freq[char]) {
freq[char]++;
} else {
freq[char] = 1;
return freq;
console.log(frequencyCounter("banana"));
function replaceSpaces(str) {
result += "%20";
} else {
result += str[i];
return result;
console.log(replaceSpaces("hello world"));
freq1[str1[i]] = (freq1[str1[i]] || 0) + 1;
freq2[str2[i]] = (freq2[str2[i]] || 0) + 1;
return true;
console.log(isAnagram("listen", "silent"));
function isPangram(str) {
let count = 0;
let ch = str[i].toLowerCase();
letters[ch] = true;
count++;
}
}
function countWords(sentence) {
count++;
inWord = true;
inWord = false;
return count;
function firstNonRepeatingChar(str) {
freq[str[i]] = (freq[str[i]] || 0) + 1;
}
for (let i = 0; i < str.length; i++) {
return null;
console.log(firstNonRepeatingChar("swiss"));
function removeVowels(str) {
let ch = str[i].toLowerCase();
if (ch !== "a" && ch !== "e" && ch !== "i" && ch !== "o" && ch !== "u") {
result += str[i];
return result;
function shortestWord(sentence) {
minLen = word.length;
minWord = word;
word = "";
} else {
word += sentence[i];
return minWord;
let count = 0;
match = false;
break;
if (match) count++;
return count;
}
console.log(countSubstring("ababababa", "aba"));
Session 4
let sum = 0;
sum += arr[ind];
return sum;
min = arr[ind];
max = arr[ind];
}
return [min, max];
// console.log(arr);
secLargest = max;
max = arr[ind];
secLargest = arr[ind];
return secLargest;
// console.log(secondLargest([1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5]));
// O(n)
arr[ind - 1] = arr[ind];
}
arr[arr.length - 1] = temp;
return arr;
// rotate([1,2,3,4,5])
// console.log(k);
// k = k % arr.length;
// console.log(k);
// while (k > 0) {
// rotate(arr);
// k--;
// }
// console.log(arr);
// }
start++;
end--;
// O(n)
k = k % arr.length;
reverse(arr, 0, k - 1);
reverse(arr, k);
reverse(arr);
console.log(arr);
// [2, 1, 3, 4, 5, 6, 7];
// [2, 1, 7, 6, 5, 4, 3];
// [3, 4, 5, 6, 7, 1, 2];
// console.log(...arr);
arr[ind] = arr[ind1];
arr[ind1] = temp;
// console.log(...arr);
return arr;
// console.log(...sort(
// ));
// ! Remove duplicates from a sorted array.
console.log(arr);
slowInd++;
arr[slowInd] = arr[ind];
if (arr.length - 1 == ind) {
slowInd++;
arr.length = slowInd;
return arr;
// console.log(
// removeDuplicates(
// )
// );
let count = 0;
count++;
return count;
let i = 0, j = 0;
merged.push(arr1[i]);
i++;
} else {
merged.push(arr2[j]);
j++;
merged.push(arr1[i]);
i++;
j++;
return merged;
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
return arr;
return -1;
sum += arr[i];
result.push(sum);
return result;
let product = 1;
product *= arr[i];
}
return product;
start++;
end--;
return true;
result.push(arr1[i]);
break;
}
return result;
Session 5
// * Algorithm
if (arr[ind] == target) {
return ind;
}
}
return -1;
// console.log(
// );
// * Algorithm
// * 4. If the target element is greater than the middle element, search the right half of the array.
// * 5. If the target element is less than the middle element, search the left half of the array.
// * 6. Repeat the process until the target element is found or the array is empty.
let start = 0;
}
return -1;
// console.log(
// binarySearch(
// (a, b) => a - b
// ),
// Math.floor(Math.random() * 100)
// )
// );
let count = 0;
if (arr[ind] == target) {
last = ind;
count++;
if (first == -1) {
first = ind;
}
function firstAndLastOccurrenceSortedArr(arr = [], target = 0) {
let start = 0;
if (arr[mid] == target) {
foundInd = mid;
return foundInd;
console.log(...arr);
console.log(target);
// console.log(
// firstAndLastOccurrenceSortedArr(
// ),
// Math.floor(Math.random() * 10)
// )
// );
// console.log(
// countOccurrences(
// (a, b) => a - b
// ),
// Math.floor(Math.random() * 10)
// )
// );
let start = 0;
return start;
let start = 0;
if (
return mid;
else start++;
return -1;
// console.log(peakElement([1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]));
// console.log(peakElement([1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1]));
// console.log(peakElement([1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1]));
console.log(...arr);
console.log(target);
let start = 0;
if (arr[mid] == target)
} else {
return -1;
console.log(
searchRotated(
rotateByK(
(a, b) => a - b
),
Math.floor(Math.random() * 10)
),
Math.floor(Math.random() * 10)
);
floor = arr[mid];
start = mid + 1;
} else {
ceiling = arr[mid];
end = mid - 1;
}
// console.log(findFloorCeiling([1, 2, 8, 10, 10, 12, 19], 5)); // { floor: 2, ceiling: 8 }
function findSmallestMissing(arr) {
end = mid - 1;
} else {
start = mid + 1;
return start;
// console.log(findSmallestMissing([0, 1, 2, 3, 5, 6, 7, 8])); // 4
end = mid1 - 1;
start = mid2 + 1;
} else {
start = mid1 + 1;
end = mid2 - 1;
return -1;
start = end;
end *= 2;
return -1;
function findMinInRotated(arr) {
return arr[start];
// console.log(findMinInRotated([4, 5, 6, 7, 0, 1, 2])); // 0
function findOccurrence(isFirst) {
return index;
}
if (start === 0) return arr[0];
return arr[start];
let index = 1;
index *= 2;
return -1;
}
return binarySearch(index / 2, Math.min(index, arr.length - 1));
function findPeakBitonic(arr) {
end = mid;
} else {
start = mid + 1;
return start;