## Problem Solving Patterns => Sliding Window

Sliding Window Pattern In a lot of problems we are expected to find something among all the contiguous subarrays.A brute force way is to loop over the elements again and again till we find the result. This type of problem can be solved in a more optimized way by maintaining a window which satisfies problem constraints.This window can be increased or decreased based on the changing constraints and inputs.

## Arrays in JS- Binary Search

Problem Statement Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1. Here is the leetcode link to the problem Binary Search Examples Example 1: Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4 Example 2:

## Longest Substring with At Most K Distinct Characters

Problem Statement Given a string, find the length of the longest substring T that contains at most k distinct characters.Here is the leetcode link to the problem Longest Substring Here is some discussion around it K unique characters Examples Example 1: Input: s = "eceba", k = 2 Output: 3 Explanation: T is "ece" which its length is 3. Example 2: Input: s = "aa", k = 1 Output: 2 Explanation: T is "aa" which its length is 2.

## Longest Substring Without Repeating Characters

Problem Statement Given a string, find the length of the longest substring without repeating characters. Let’s take a look at some examples: Examples Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3.

## Three Sum

Problem Statement Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Here it the leetcode link to the problem 3Sum Examples Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] Solution /** * @param {number[]} nums * @return {number[][]} */ var sortFunc = (a,b) => a-b; var threeSum = function(nums) { const sum = 0; const len = nums.

Problem Statement Every email consists of a local name and a domain name, separated by the @ sign. For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain ‘.’s or ‘+’s. If you add periods (‘.’) between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name.

## Arrays in JS- Diagonal Difference

Problem Statement Given a square matrix, calculate the absolute difference between the sums of its diagonals. Here is the link to problem HackerRank. Example 1 2 3 4 5 6 9 8 9 The left-to-right diagonal = 1+5+9=15. The right to left diagonal = 3+5+9=17. Their absolute difference is |15-17| = 2. Solution Let’s take an example: 11 2 4 4 5 6 10 8 -12 If we notice a pattern emerges.