Posts

Showing posts from December, 2022

Solution to LeetCode Problem 31. Next Permutation in JavaScript

Image
     A solution to LeetCode Problem 31. Next Permutation in JavaScript  If you're preparing for technical interviews or want to improve your coding skills, solving practice problems on LeetCode is a great way.  In this post, we'll discuss a solution to the "31. Next Permutation" problem on LeetCode. Problem Statement: A  permutation  of an array of integers is an arrangement of its members into a sequence or linear order. For example, for  arr = [1,2,3] , the following are all the permutations of  arr :  [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1] . The  next permutation  of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the  next permutation  of that array is the permutation that follows it in the sorted container. If such arrangement is no...

A solution to LeetCode Problem 560. Subarray Sum Equals K in JavaScript

Image
    A solution to LeetCode Problem 560. Subarray Sum Equals K in JavaScript  If you're preparing for technical interviews or want to improve your coding skills, solving practice problems on LeetCode is a great way.  In this post, we'll discuss a solution to the "560. Subarray Sum Equals K" problem on LeetCode. Problem Statement: Given an array of integers  nums  and an integer  k , return  the total number of subarrays whose sum equals to   k . A subarray is a contiguous  non-empty  sequence of elements within an array.   Example 1: Input: nums = [1,1,1], k = 2 Output: 2 Example 2: Input: nums = [1,2,3], k = 3 Output: 2   Constraints: 1 <= nums.length <= 2 * 10 4 -1000 <= nums[i] <= 1000 -10 7 <= k <= 10 7

Solution to LeetCode Problem 380. Insert Delete GetRandom O(1) in JavaScript

Image
   A solution to LeetCode Problem 380. Insert Delete GetRandom O(1) in JavaScript  If you're preparing for technical interviews or want to improve your coding skills, solving practice problems on LeetCode is a great way.  In this post, we'll discuss a solution to the "380. Insert Delete GetRandom O(1)" problem on LeetCode. Implement the  RandomizedSet  class: RandomizedSet()  Initializes the  RandomizedSet  object. bool insert(int val)  Inserts an item  val  into the set if not present. Returns  true  if the item was not present,  false  otherwise. bool remove(int val)  Removes an item  val  from the set if present. Returns  true  if the item was present,  false  otherwise. int getRandom()  Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the  same probab...

Solution to LeetCode Problem 238. Product of Array Except Self in JavaScript

Image
   A solution to LeetCode Problem 238. Product of Array Except Self in JavaScript  If you're preparing for technical interviews or want to improve your coding skills, solving practice problems on LeetCode is a great way.  In this post, we'll discuss a solution to the "238. Product of Array Except Self" problem on LeetCode. Given an integer array  nums , return  an array   answer   such that   answer[i]   is equal to the product of all the elements of   nums   except   nums[i] . The product of any prefix or suffix of  nums  is  guaranteed  to fit in a  32-bit  integer. You must write an algorithm that runs in  O(n)  time and without using the division operation.   Example 1: Input: nums = [1,2,3,4] Output: [24,12,8,6] Example 2: Input: nums = [-1,1,0,-3,3] Output: [0,0,9,0,0]   Constraints: 2 <= nums.length <= 10 5 -30 <= nums[i] <= 30 The product of any pref...