Category: Algorithms
-
Data Structures and Algorithms in Java – Arrays – Product of Array Except Self – faster than 100% of submissions on leetcode!
Problem: Given an array of integers , find an array of products where each product is the product of all the numbers except the element in that index. ie) For every number number[i] , find the product of all the numbers except number[i] Input: numbers = [1,2,3,4] Output: [24,12,8,6] Calculated as below: 24 = 2*3*4…
-
Data Structures and Algorithms in Java – Arrays – Contains Duplicate
Problem: Given an array of numbers , find if there are any duplicates Input: numbers = [ 4, 7, 3, 9, 3] Output: true, since 3 is present twice Assumptions: The number of elements in the array can be from 1 to a lakh. The elements can be in the range of – 1 billion…
-
Data Structures and Algorithms in Java – Arrays – Best time to buy and sell stock
Problem: You are given an array of stock prices with each index of the array representing each day. You need to buy stock one one day and sell it on another subsequent day. What is the maximum profit you can earn? Input: An integer array of prices: prices = [7,1,5,2,6,9] prices[i] denotes the price on…
-
Data Structures & Algorithms in Java – Arrays – Two sum
Problem: Given an array of integers and a sum , find two numbers in the array which match that sum and return their indices. Example: Input: [2, 6, 3, 9] – array of integers 8 – sum Output: 2 + 6 = 8 , index of 2 is 0 index of 6 is 1 So…