Tag: Data Structures
-
Data Structures & Algorithms in Java – Dynamic Programming – House Robber
Problem: A robber goes to rob in a series of houses built next to each other. He can’t rob from two consecutive houses , this will trigger an alarm. How much maximum money can he steal ? Input: The array [1,2,3,1] represents money in each house starting from the first index. Output: Maximum amount the…
-
Data Structures & Algorithms in Java – Binary – Sum of two integers
Problem : Given two integers, add them without using + and – operators. Input: a = 3 b= 5 Output: 8 Try out the solution here: https://leetcode.com/problems/sum-of-two-integers/ Hint: Use bitwise operators Solution: Let’s solve the problem using bitwise operators XOR, AND and SHIFT. Adding two binary numbers is equal to performing XOR operation between them…
-
Data Structures and Algorithms – Arrays – Container with Most Water
Problem: Given an array of heights representing the ends of a container , find the container with the maximum area. For example: Consider the below input: Input: height = [1,8,6,2,5,4,8,3,7] The height can be represented pictorially as below: Each line represents each height in the array in the order of the indices. You can join…
-
Data Structures and Algorithms in Java – Arrays – Three sum
Problem: Given an array of integers , find out triplets in the array such that their sum is 0. Constraints: There should not be any duplicates. Same number should not be added to itself. Assumptions: The array contains a minimum of 3 elements and maximum 3000 elements Numbers can vary from -100000 to 100000 Input:…
-
Data Structures and Algorithms in Java – Arrays – Search in Rotated Sorted Array
Problem: Given an array of integers which is sorted and rotated , find out if the given element is in the array or not. A rotated array is an array whose elements are shifted towards the beginning from the end. For example , consider the array: input = [1,2,3,4,5] If this array is rotated once…
-
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…