Array Nesting

Question: Given a zero-indexed array ‘A’ of length ‘N’ which contains all integers from 0 to N-1. Find and return the longest length of set S, where S[i] = {A[i], A[A[i]], A[A[A[i]]], …} subjected to a particular condition. Suppose the first element in S starts with the selection of element A[i] of index = i, the next element… Read More »

CamelCase matching

Question: Given a set of queries and a pattern, we need to determine if some of those queries can be made from the given pattern by adding one or more lower case character. Output a boolean array where array[i] says true, if the query is possible, else false. Input: queries= {“StudyAlgorithms”,”StudyAlgorithmsTest”,”StanAlluring”,”SimulationAlteration”,”StopStayAlive”} pattern = “StAl” Output: {true, false, true,… Read More »

Container with maximum water

Question: Let us suppose we have a two dimensional plane where the the length of lines determine the height of a container. We need to determine the maximum capacity of water this kind of an arrangement can hold. The heights are represented by an array. Input: [1, 8, 6, 2, 5, 4, 8, 3, 7] Output: 49 Let… Read More »

Length of longest palindrome that can be built from a string

Question: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. Input: “abccccdd” Output: 7 In the above example, the longest palindrome in the given string is “dccaccd” whose length is 7. A palindrome consists of letters with equal partners, plus possibly a unique… Read More »

Find cycle in a linked list. (Method 2)

Question: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Output: 0 The easier method to find the cycle has been discussed in this post. This method is based upon the idea of a circular race track. If there is a slow runner and a fast runner, eventually the… Read More »

Find cycle in a linked list. (Method 1)

Question: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Output: 0 We are given a linked list which may or may not have a loop. A loop in a linked list means that if we keep doing a next, we will never reach null. Thus, we can be… Read More »

Next Greater Element in an array. [NGE]

Question: There is an array A[N] of N numbers. You have to compose an array Output[N] such that each element in Output[i] will tell the next greater element to the right in the original array. If there is no greater number to the right, then the output array should contain -1 in that position. Array 1: {4, 6,… Read More »

Prime factors of a number. [Prime Factorization]

Question: Given an integer N, find all the prime factors of the number. Input: N = 45 Output: 3 5 Finding prime factors of any numbers is a very important concept in number theory. One sample problem is available here in which we have to find the largest prime factor. The mathematical concept behind this problem is simple.… Read More »