Practice Problems
Sharpen your problem-solving skills with frontend coding challenges. From easy warmups to advanced algorithms.
Palindrome
easyWrite a function `isPalindrome` that determines if a given string is a palindrome. A string is considered a palindrome if it reads the same forward and backward, ignoring cases, spaces, and punctuation. ## Function Signature ```function isPalindrome(str);``` ### Example 1: > **Input:** `s = "racecar"`\ > **Output:** `true`\ > **Explanation:** The string "racecar" reads the same forward and backward. ### Example 2: > **Input:** `s = "hello"`\ > **Output:** `false`\ > **Explanation:** The string "hello" does not read the same forward and backward. ### Example 3: > **Input:** `s = "kajak"`\ > **Output:** `true`\ > **Explanation:** The string "kajak" reads the same forward and backward, proving it is a palindrome.
StringTwo Sum
easyWrite a function `twoSum` that takes two integers `a` and `b` and returns their sum. ## Function Signature ```function twoSum(a, b);``` ### Example 1: > **Input:** `a = 3, b = 5`\ > **Output:** `8`\ > **Explanation:** The sum of `3` and `5` is `8`. ### Example 2: > **Input:** `a = -2, b = -4`\ > **Output:** `-6`\ > **Explanation:** The sum of `-2` and `-4` is `-6`. ### Example 3: > **Input:** `a = 100, b = 200`\ > **Output:** `300`\ > **Explanation:** The sum of `100` and `200` is `300`.
MathSort Array
easyWrite a function `sortArray` that takes an array of integers and returns a new array with the elements sorted in ascending order. ## Function Signature ```function sortArray(nums);``` ### Example 1: > **Input:** `nums = [34, 7, 23, 32, 5, 62]`\ > **Output:** `[5, 7, 23, 32, 34, 62]`\ > **Explanation:** After sorting the numbers from the smallest to the largest, the output is `[5, 7, 23, 32, 34, 62]`. ### Example 2: > **Input:** `nums = [-1, -3, -2, 0, 2, 1]`\ > **Output:** `[-3, -2, -1, 0, 1, 2]`\ > **Explanation:** Negative numbers are sorted first followed by zero and positive numbers, resulting in `[-3, -2, -1, 0, 1, 2]`. ### Example 3: > **Input:** `nums = [10]`\ > **Output:** `[10]`\ > **Explanation:** Since there is only one element in the array, the sorted array remains `[10]`.
ArrayTop N Frequent Words
hardWrite a function `topNFrequentWords` that takes a large text document as input and returns the top N most frequent words. The function should be optimized for performance. ## Function Signature ```function topNFrequentWords(text, N);``` ### Example 1: > **Input:** `text = "the quick brown fox jumps over the lazy dog the the the quick quick"`, `N = 2` \ > **Output:** `["the", "quick"]` \ > **Explanation:** "the" and "quick" are the most frequent words appearing 4 and 3 times respectively. ### Example 2: > **Input:** `text = "a a a b b c"`, `N = 1` \ > **Output:** `["a"]` \ > **Explanation:** "a" is the most frequent word appearing 3 times. ### Example 3: > **Input:** `text = "apple banana apple banana orange apple banana banana"`, `N = 2` \ > **Output:** `["banana", "apple"]` \ > **Explanation:** "banana" and "apple" are the most frequent words appearing 4 and 3 times respectively.
StringSort Colors
hardWrite a function `sortColors` that takes an array of integers `nums` representing colors, where: - 0 represents red, - 1 represents white, - 2 represents blue. The function should sort the array in place so that the colors are in the order red, white, and blue. ## Function Signature ```function sortColors(nums);``` ### Example 1: > **Input:** `nums = [2,0,2,1,1,0]` \ > **Output:** `[0,0,1,1,2,2]` \ > **Explanation:** The array is sorted so that all reds come first, followed by whites, and then blues. ### Example 2: > **Input:** `nums = [2,0,1]` \ > **Output:** `[0,1,2]` \ > **Explanation:** The array is sorted so that all reds come first, followed by whites, and then blues. o solve it in a single pass (O(n) time complexity) using constant space (O(1) extra space). ### Example 3: > **Input:** `nums = [1,2,0,2,1,0]` \ > **Output:** `[0,0,1,1,2,2]` \ > **Explanation:** The array is sorted so that all reds come first, followed by whites, and then blues. ### Example 4: > **Input:** `nums = [0]` \ > **Output:** `[0]` \ > **Explanation:** The array contains only one element which is already sorted.
SortingFind Max
easyWrite a function `findMax` that takes an array of integers and returns the maximum value. ## Function Signature ```function findMax(nums);``` ### Example 1: > **Input:** `nums = [1, 5, 3, 9, 2]`\ > **Output:** `9`\ > **Explanation:** The maximum value in the array is `9`. ### Example 2: > **Input:** `nums = [-1, -5, -3, -9, -2]`\ > **Output:** `-1`\ > **Explanation:** The maximum value in the array is `-1`. ### Example 3: > **Input:** `nums = [10]`\ > **Output:** `10`\ > **Explanation:** The maximum value in the array is `10`.
ArrayReverse String
easyWrite a function `reverseString` that takes a string and returns it reversed. ## Function Signature ```function reverseString(str);``` ### Example 1: > **Input:** `str = "hello"`\ > **Output:** `"olleh"`\ > **Explanation:** The string "hello" reversed is "olleh". ### Example 2: > **Input:** `str = "world"`\ > **Output:** `"dlrow"`\ > **Explanation:** The string "world" reversed is "dlrow". ### Example 3: > **Input:** `str = "a"`\ > **Output:** `"a"`\ > **Explanation:** The string "a" reversed is "a".
StringSum Array
easyWrite a function `sumArray` that takes an array of integers and returns the sum of all elements. ## Function Signature ```function sumArray(nums);``` ### Example 1: > **Input:** `nums = [1, 2, 3, 4, 5]`\ > **Output:** `15`\ > **Explanation:** The sum of the numbers is `1 + 2 + 3 + 4 + 5 = 15`. ### Example 2: > **Input:** `nums = [-1, -2, -3, -4, -5]`\ > **Output:** `-15`\ > **Explanation:** The sum of the numbers is `-1 + -2 + -3 + -4 + -5 = -15`. ### Example 3: > **Input:** `nums = [0]`\ > **Output:** `0`\ > **Explanation:** The sum of the numbers is `0`.
ArrayMultiply
easyWrite a function `multiply` that takes two integers `a` and `b` and returns their product. ## Function Signature ```function multiply(a, b);``` ### Example 1: > **Input:** `a = 3, b = 5`\ > **Output:** `15`\ > **Explanation:** The product of `3` and `5` is `15`. ### Example 2: > **Input:** `a = -2, b = -4`\ > **Output:** `8`\ > **Explanation:** The product of `-2` and `-4` is `8`. ### Example 3: > **Input:** `a = 0, b = 200`\ > **Output:** `0`\ > **Explanation:** The product of `0` and `200` is `0`.
MathCount Vowels
easyWrite a function `countVowels` that takes a string and returns the number of vowels in it. ## Function Signature ```function countVowels(str);``` ### Example 1: > **Input:** `str = "hello"`\ > **Output:** `2`\ > **Explanation:** The string "hello" contains 2 vowels: "e" and "o". ### Example 2: > **Input:** `str = "world"`\ > **Output:** `1`\ > **Explanation:** The string "world" contains 1 vowel: "o". ### Example 3: > **Input:** `str = "bcdfgh"`\ > **Output:** `0`\ > **Explanation:** The string "bcdfgh" contains no vowels.
StringIs Even
easyWrite a function `isEven` that takes an integer and returns true if it is even, false otherwise. ## Function Signature ```function isEven(num);``` ### Example 1: > **Input:** `num = 4`\ > **Output:** `true`\ > **Explanation:** The number 4 is even. ### Example 2: > **Input:** `num = 7`\ > **Output:** `false`\ > **Explanation:** The number 7 is not even. ### Example 3: > **Input:** `num = 0`\ > **Output:** `true`\ > **Explanation:** The number 0 is even.
MathFactorial
easyWrite a function `factorial` that takes a non-negative integer and returns its factorial. ## Function Signature ```function factorial(num);``` ### Example 1: > **Input:** `num = 5`\ > **Output:** `120`\ > **Explanation:** The factorial of 5 is `5! = 5 * 4 * 3 * 2 * 1 = 120`. ### Example 2: > **Input:** `num = 0`\ > **Output:** `1`\ > **Explanation:** The factorial of 0 is defined as `1`. ### Example 3: > **Input:** `num = 3`\ > **Output:** `6`\ > **Explanation:** The factorial of 3 is `3! = 3 * 2 * 1 = 6`.
MathFind Min
easyWrite a function `findMin` that takes an array of integers and returns the minimum value. ## Function Signature ```function findMin(nums);``` ### Example 1: > **Input:** `nums = [1, 5, 3, 9, 2]`\ > **Output:** `1`\ > **Explanation:** The minimum value in the array is `1`. ### Example 2: > **Input:** `nums = [-1, -5, -3, -9, -2]`\ > **Output:** `-9`\ > **Explanation:** The minimum value in the array is `-9`. ### Example 3: > **Input:** `nums = [10]`\ > **Output:** `10`\ > **Explanation:** The minimum value in the array is `10`.
ArrayMerge Intervals
mediumWrite a function `mergeIntervals` that takes an array of intervals and merges all overlapping intervals. ## Function Signature ```function mergeIntervals(intervals);``` ### Example 1: > **Input:** `intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]` \ > **Output:** `[[1, 6], [8, 10], [15, 18]]` \ > **Explanation:** Since intervals [1, 3] and [2, 6] overlap, merge them into [1, 6]. ### Example 2: > **Input:** `intervals = [[1, 4], [4, 5]]` \ > **Output:** `[[1, 5]]` \ > **Explanation:** Intervals [1, 4] and [4, 5] are considered overlapping.
ArrayBest Time to Buy and Sell Stock
mediumWrite a function `maxProfit` that takes an array where the `i-th` element is the price of a given stock on day `i`, and returns the maximum profit you can achieve by buying and selling one share of the stock. ## Function Signature ```function maxProfit(prices);``` ### Example 1: > **Input:** `prices = [7,1,5,3,6,4]` \ > **Output:** `5` \ > **Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. ### Example 2: > **Input:** `prices = [7,6,4,3,1]` \ > **Output:** `0` \ > **Explanation:** In this case, no transactions are done, i.e. max profit = 0.
ArrayGroup Anagrams
mediumWrite a function `groupAnagrams` that takes an array of strings and groups the anagrams together. ## Function Signature ```function groupAnagrams(strs);``` ### Example 1: > **Input:** `strs = ["eat", "tea", "tan", "ate", "nat", "bat"]` \ > **Output:** `[["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]` \ > **Explanation:** All strings that are anagrams are grouped together. ### Example 2: > **Input:** `strs = [""]` \ > **Output:** `[[""]]` \ > **Explanation:** An empty string is considered an anagram of itself. ### Example 3: > **Input:** `strs = ["a"]` \ > **Output:** `[["a"]]` \ > **Explanation:** A single character string is considered an anagram of itself.
StringMerge Sorted Lists
mediumWrite a function `mergeSortedLists` that takes two sorted linked lists and merges them into one sorted list. ## Function Signature ```function mergeSortedLists(l1, l2);``` ### Example 1: > **Input:** `l1 = [1, 2, 4]`, `l2 = [1, 3, 4]` \ > **Output:** `[1, 1, 2, 3, 4, 4]` \ > **Explanation:** The merged list is sorted. ### Example 2: > **Input:** `l1 = []`, `l2 = []` \ > **Output:** `[]` \ > **Explanation:** Merging two empty lists results in an empty list. ### Example 3: > **Input:** `l1 = []`, `l2 = [0]` \ > **Output:** `[0]` \ > **Explanation:** Merging an empty list with a non-empty list results in the non-empty list.
Data StructureThree Sum
mediumWrite a function `threeSum` that takes an array of integers and returns all unique triplets that sum up to zero. ## Function Signature ```function threeSum(nums);``` ### Example 1: > **Input:** `nums = [-1, 0, 1, 2, -1, -4]` \ > **Output:** `[[-1, -1, 2], [-1, 0, 1]]` \ > **Explanation:** The triplets that sum up to zero are [-1, -1, 2] and [-1, 0, 1]. ### Example 2: > **Input:** `nums = []` \ > **Output:** `[]` \ > **Explanation:** No triplets can be formed from an empty array. ### Example 3: > **Input:** `nums = [0]` \ > **Output:** `[]` \ > **Explanation:** No triplets can be formed from a single element.
ArrayProduct of Array Except Self
mediumWrite a function `productExceptSelf` that takes an array of integers and returns an array such that each element at index `i` is the product of all the numbers in the original array except the one at `i`. ## Function Signature ```function productExceptSelf(nums);``` ### Example 1: > **Input:** `nums = [1, 2, 3, 4]` \ > **Output:** `[24, 12, 8, 6]` \ > **Explanation:** The product of all the numbers except the one at each index is [24, 12, 8, 6]. ### Example 2: > **Input:** `nums = [-1, 1, 0, -3, 3]` \ > **Output:** `[0, 0, 9, 0, 0]` \ > **Explanation:** The product of all the numbers except the one at each index is [0, 0, 9, 0, 0].
ArrayLongest Palindromic Substring
hardWrite a function `longestPalindrome` that takes a string and returns the longest palindromic substring. ## Function Signature ```function longestPalindrome(s);``` ### Example 1: > **Input:** `s = "babad"` \ > **Output:** `"bab"` \ > **Explanation:** The longest palindromic substring is "bab". ### Example 2: > **Input:** `s = "cbbd"` \ > **Output:** `"bb"` \ > **Explanation:** The longest palindromic substring is "bb". ### Example 3: > **Input:** `s = "a"` \ > **Output:** `"a"` \ > **Explanation:** The longest palindromic substring is "a".
StringSearch in Rotated Sorted Array
hardWrite a function `search` that takes a rotated sorted array and a target value, and returns the index if the target is found. If not, returns -1. ## Function Signature ```function search(nums, target);``` ### Example 1: > **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 0` \ > **Output:** `4` \ > **Explanation:** Target 0 is at index 4. ### Example 2: > **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 3` \ > **Output:** `-1` \ > **Explanation:** Target 3 is not in the array.
ArrayFirst Missing Positive
mediumWrite a function `firstMissingPositive` that takes an array of integers and finds the smallest missing positive integer. ## Function Signature ```function firstMissingPositive(nums);``` ### Example 1: > **Input:** `nums = [1,2,0]` \ > **Output:** `3` \ > **Explanation:** The smallest missing positive integer is 3. ### Example 2: > **Input:** `nums = [3,4,-1,1]` \ > **Output:** `2` \ > **Explanation:** The smallest missing positive integer is 2. ### Example 3: > **Input:** `nums = [7,8,9,11,12]` \ > **Output:** `1` \ > **Explanation:** The smallest missing positive integer is 1.
ArrayContainer With Most Water
mediumWrite a function `maxArea` that takes an array of integers where each element represents the height of a vertical line, and returns the maximum area of water it can contain. ## Function Signature ```function maxArea(height);``` ### Example 1: > **Input:** `height = [1,8,6,2,5,4,8,3,7]` \ > **Output:** `49` \ > **Explanation:** The maximum area is formed between the lines at index 1 and 8, resulting in an area of 49. ### Example 2: > **Input:** `height = [1,1]` \ > **Output:** `1` \ > **Explanation:** The maximum area is formed between the lines at index 0 and 1, resulting in an area of 1.
ArrayLongest Consecutive Sequence
mediumWrite a function `longestConsecutive` that takes an array of integers and returns the length of the longest consecutive elements sequence. ## Function Signature ```function longestConsecutive(nums);``` ### Example 1: > **Input:** `nums = [100, 4, 200, 1, 3, 2]` \ > **Output:** `4` \ > **Explanation:** The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore, its length is 4. ### Example 2: > **Input:** `nums = [0,3,7,2,5,8,4,6,0,1]` \ > **Output:** `9` \ > **Explanation:** The longest consecutive elements sequence is [0, 1, 2, 3, 4, 5, 6, 7, 8]. Therefore, its length is 9.
ArrayFind Peak Element
mediumWrite a function `findPeakElement` that takes an array of integers and finds a peak element, and returns its index. A peak element is an element that is greater than its neighbors. ## Function Signature ```function findPeakElement(nums);``` ### Example 1: > **Input:** `nums = [1,2,3,1]` \ > **Output:** `2` \ > **Explanation:** 3 is a peak element and its index is 2. ### Example 2: > **Input:** `nums = [1,2,1,3,5,6,4]` \ > **Output:** `5` \ > **Explanation:** 6 is a peak element and its index is 5.
ArrayTop K Frequent Elements
hardWrite a function `topKFrequent` that takes an array of integers and an integer k, and returns the k most frequent elements. ## Function Signature ```function topKFrequent(nums, k);``` ### Example 1: > **Input:** `nums = [1,1,1,2,2,3]`, `k = 2` \ > **Output:** `[1,2]` \ > **Explanation:** The two most frequent elements are 1 and 2. ### Example 2: > **Input:** `nums = [1]`, `k = 1` \ > **Output:** `[1]` \ > **Explanation:** The most frequent element is 1.
ArrayPermutations
hardWrite a function `permute` that takes an array of distinct integers and returns all possible permutations. ## Function Signature ```function permute(nums);``` ### Example 1: > **Input:** `nums = [1,2,3]` \ > **Output:** `[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]` \ > **Explanation:** The permutations of [1,2,3] are [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]. ### Example 2: > **Input:** `nums = [0,1]` \ > **Output:** `[[0,1],[1,0]]` \ > **Explanation:** The permutations of [0,1] are [[0,1],[1,0]]. ### Example 3: > **Input:** `nums = [1]` \ > **Output:** `[[1]]` \ > **Explanation:** The permutations of [1] are [[1]].
ArraySubsets
hardWrite a function `subsets` that takes an array of distinct integers and returns all possible subsets. ## Function Signature ```function subsets(nums);``` ### Example 1: > **Input:** `nums = [1,2,3]` \ > **Output:** `[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]` \ > **Explanation:** The subsets are [[] [1] [2] [1,2] [3] [1,3] [2,3] [1,2,3]]. ### Example 2: > **Input:** `nums = [0]` \ > **Output:** `[[],[0]]` \ > **Explanation:** The subsets are [[] [0]].
ArrayWord Break
hardWrite a function `wordBreak` that takes a string s and a dictionary of strings wordDict, and returns true if s can be segmented into a space-separated sequence of one or more dictionary words. ## Function Signature ```function wordBreak(s, wordDict);``` ### Example 1: > **Input:** `s = "bigdevsoon"`, `wordDict = ["big","dev","soon"]` \ > **Output:** `true` \ > **Explanation:** The string "bigdevsoon" can be segmented as "big dev soon". ### Example 2: > **Input:** `s = "applepenapple"`, `wordDict = ["apple","pen"]` \ > **Output:** `true` \ > **Explanation:** The string "applepenapple" can be segmented as "apple pen apple". ### Example 3: > **Input:** `s = "catsandog"`, `wordDict = ["cats","dog","sand","and","cat"]` \ > **Output:** `false` \ > **Explanation:** The string "catsandog" cannot be segmented as a space-separated sequence of one or more dictionary words.
StringMaximum Subarray
mediumWrite a function `maxSubArray` that takes an array of integers and returns the contiguous subarray (containing at least one number) which has the largest sum. ## Function Signature ```function maxSubArray(nums);``` ### Example 1: > **Input:** `nums = [-2,1,-3,4,-1,2,1,-5,4]` \ > **Output:** `6` \ > **Explanation:** The subarray [4,-1,2,1] has the largest sum = 6. ### Example 2: > **Input:** `nums = [1]` \ > **Output:** `1` \ > **Explanation:** The largest sum subarray is [1]. ### Example 3: > **Input:** `nums = [5,4,-1,7,8]` \ > **Output:** `23` \ > **Explanation:** The subarray [5,4,-1,7,8] has the largest sum = 23.
ArrayAdd Digits
easyGiven a non-negative integer num, repeatedly add all its digits until the result has only one digit. ## Function Signature ```function addDigits(num);``` ### Example 1: > **Input:** `num = 38` \ > **Output:** `2` \ > **Explanation:** The process is 3+8 = 11, 1+1 = 2. Since 2 has only one digit, 2 is returned. ### Example 2: > **Input:** `num = 999` \ > **Output:** `9` \ > **Explanation:** The process is 9+9+9 = 27, 2+7 = 9. Since 9 has only one digit, 9 is returned.
MathString Compression
mediumCompress a string using the counts of repeated characters. ## Function Signature ```function compressString(chars);``` ### Example 1: > **Input:** `chars = ["a","a","b","b","c","c","c"]` \ > **Output:** `["a","2","b","2","c","3"]` \ > **Explanation:** The string compression of "aabbbccc" is "a2b2c3". ### Example 2: > **Input:** `chars = ["a","b","c"]` \ > **Output:** `["a","b","c"]` \ > **Explanation:** The string compression of "abc" is "abc" as each character appears only once.
StringFind All Duplicates in an Array
hardGiven an array of integers, find all elements that appear twice. ## Function Signature ```function findDuplicates(nums);``` ### Example 1: > **Input:** `nums = [4,3,2,7,8,2,3,1]` \ > **Output:** `[2,3]` \ > **Explanation:** The numbers 2 and 3 appear twice in the array. ### Example 2: > **Input:** `nums = [1,1,2,3,3,4,4,4,5]` \ > **Output:** `[1,3,4]` \ > **Explanation:** The numbers 1, 3, and 4 appear twice or more in the array.
ArrayValid Anagram
mediumDetermine if two strings are anagrams of each other. ## Function Signature ```function isAnagram(s, t);``` ### Example 1: > **Input:** `s = "anagram", t = "nagaram"` \ > **Output:** `true` \ > **Explanation:** The strings "anagram" and "nagaram" are anagrams of each other. ### Example 2: > **Input:** `s = "rat", t = "car"` \ > **Output:** `false` \ > **Explanation:** The strings "rat" and "car" are not anagrams.
StringLongest Substring Without Repeating Characters
hardFind the length of the longest substring without repeating characters. ## Function Signature ```function lengthOfLongestSubstring(s);``` ### Example 1: > **Input:** `s = "abcabcbb"` \ > **Output:** `3` \ > **Explanation:** The longest substring without repeating characters is "abc". ### Example 2: > **Input:** `s = "bbbbb"` \ > **Output:** `1` \ > **Explanation:** The longest substring without repeating characters is "b" of length 1.
StringHappy Number
mediumDetermine if a number is 'happy' which is defined by repeatedly replacing the number with the sum of the squares of its digits, until it is one. ## Function Signature ```function isHappy(n);``` ### Example 1: > **Input:** `n = 19` \ > **Output:** `true` \ > **Explanation:** 19 is a happy number. ### Example 2: > **Input:** `n = 2` \ > **Output:** `false` \ > **Explanation:** 2 is not a happy number.
MathDivide Two Integers
hardDivide two integers without using multiplication, division, and mod operator. ## Function Signature ```function divide(dividend, divisor);``` ### Example 1: > **Input:** `dividend = 10, divisor = 3` \ > **Output:** `3` \ > **Explanation:** The quotient of 10/3 is 3. ### Example 2: > **Input:** `dividend = 7, divisor = -3` \ > **Output:** `-2` \ > **Explanation:** The quotient of 7/-3 is -2.
MathLength of Last Word
mediumCalculate the length of the last word in a string separated by space characters. ## Function Signature ```function lengthOfLastWord(s);``` ### Example 1: > **Input:** `s = "Hello World"` \ > **Output:** `5` \ > **Explanation:** The length of the last word "World" is 5. ### Example 2: > **Input:** `s = " fly me to the moon "` \ > **Output:** `4` \ > **Explanation:** The length of the last word "moon" is 4.
StringCalculate Fibonacci Number
easyCalculate the n-th Fibonacci number. ## Function Signature ```function fibonacci(n);``` ### Example 1: > **Input:** `n = 5` \ > **Output:** `5` \ > **Explanation:** The 5th Fibonacci number is 5 (0, 1, 1, 2, 3, 5). ### Example 2: > **Input:** `n = 10` \ > **Output:** `55` \ > **Explanation:** The 10th Fibonacci number is 55.
MathMaximum Product of Two Elements
easyFind the maximum product of two distinct elements in an array of integers. ## Function Signature ```function maxProduct(nums);``` ### Example 1: > **Input:** `nums = [3,4,5,2]` \ > **Output:** `20` \ > **Explanation:** The maximum product (5*4 = 20) is obtained by multiplying the two largest elements. ### Example 2: > **Input:** `nums = [1,5,4,5]` \ > **Output:** `25` \ > **Explanation:** The maximum product (5*5 = 25) is obtained by multiplying the two largest elements.
ArrayReady to practice?
Get full access to all practice problems with our built-in code editor and instant feedback.
Start 7-Day Free Trial