2Sum Problem

Problem You are given an array of n integers and a target sum T. The goal is to determine whether or not there are two numbers x, y in A with x+y=T. OR Given an array of integers, find two numbers such that they add up to a specific target number. Variant: Return the indices instead of the elements in array. Example Example 1: ...

Non-decreasing Array Problem

Problem Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most one element. We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2). Examples Example 1: Input: nums = [4,2,3] Output: true Explanation: You could modify the first 4 to 1 to get a non-decreasing array. ...

Check if given string is palindrome or not

Problem Write an algorithm to find Whether Given String is palindrome or Not. Definition Palindrome Definition Examples Example 1: Input: str = "malayalam" Output: true Example 2: Input: str = "abc" Output: false Solution Method 1 - Recursion Algorithm If the first and last characters don’t match, return false (not a palindrome). If they match, remove the first and last characters and call the function recursively with the remaining substring. This continues until the entire string is checked. Code Java public Boolean isPalindrome(String s) { if (s.length()<2) { return true; } if (s.charAt(0) == s.charAt(s.length() - 1)) { return isPalindrome(s.substring(1, s.length() - 1)); } else { return false; } } ...

Move Zeroes at the end of array

Problem Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. Examples Example 1: Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] Example 2: Input: nums = [0] Output: [0] ...

Linear Search Algorithm on array

Problem Given an array of n elements and a element ‘x’, write a program to search an element ‘x’ in the array. Examples // Example 1 Input [] = {20, 30, 40, 10, 5, 2, 60, 73}, int x = 10 Output: Index 3 // Example 2 Input [] = {20, 30, 40, 10, 5, 2, 60, 73}, int x = 60 Output: Index 6 // Example 3 Input [] = {20, 30, 40, 10, 5, 2, 60, 73}, int x = 9 Output: No Found ...

Find fixed point in sorted array

Problem Given a sorted array of distinct integers, write a function to find the magic index or fixed point in the array, else return -1. Definition Magic index or a Fixed point in an array is an index i in the array A such that A[i] = i. Examples Example 1: Input: nums = [-6, 0, 2, 40] Output: 2 ...

Find smallest and second smallest elements in an array

Problem Given an array of integers, our task is to write a program that efficiently finds the smallest and second smallest element present in the array. This problem is similar to Find Second largest element in an array. Examples Example 1: Input: arr[] = {12, 13, 1, 10, 34, 1} Output: [1, 10] Explanation: 1 is smallest and 10 is second smallest ...

August 10, 2022 · 2 min · TagsList of tags for the post  array ·  array-algo

Average Waiting Time Problem

Problem There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]: arrivali is the arrival time of the ith customer. The arrival times are sorted in non-decreasing order. timei is the time needed to prepare the order of the ith customer. When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits till the chef finishes preparing his order. The chef does not prepare food for more than one customer at a time. The chef prepares food for customers in the order they were given in the input. ...

Array implementation of stack

Here will discuss the array implementation of stack. Problems with array implementation Underflow - Array may be empty but people may try to pop the element Overflow - Array is full. To over come we have to use re-szing of array. We can see how we can solve this problem here - http://k2code.blogspot.com/2013/09/resizing-array-implementation-of-stack.html Null items - Can nulls be added - Yes in this case, nulls can be added in stack Loitering (java specific) - Holding a reference to an object wen it is no longer needed. To over come this we should explicitly set array index to null. For eg. ...

Array implementation of stack

Here will discuss the array implementation of stack. Problems with array implementation Underflow - Array may be empty but people may try to pop the element Overflow - Array is full. To over come we have to use re-szing of array. We can see how we can solve this problem here - http://k2code.blogspot.com/2013/09/resizing-array-implementation-of-stack.html Null items - Can nulls be added - Yes in this case, nulls can be added in stack Loitering (java specific) - Holding a reference to an object wen it is no longer needed. To over come this we should explicitly set array index to null. For eg. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy