# Sort squares of a sorted array in linear runtime (O(n))

Given a sorted array that contains both negative and positive integers we aim to get its squares array in sorted orders. Let us understand this with an example. Let’s understand how we can solve this problem in linear time. To solve this in linear time, we have to take two handles which represent the startRead More »

# An algorithm to move all zeros to rightmost in an integer array.

Given an integer array, which contains some ‘k’ zeros. We aim to move all ‘k’ zeros to the right of most of the integer array. Let us see this with an example. Algorithm Iterate through each integer in the array and take a variable “pos,” representing the end of the array at first. If theRead More »

# Find if there exists a pair with a given sum in a sorted array.

Given an array of integers which are in ascending order and a sum. Our aim is to find if there is any pair of integers which total is equal to input sum. Let us discuss this problem with below example. Above example contains numbers 4 & 9 which sum is equal to our input. WeRead More »

# Parentheses matching problem.

Given a set of parentheses (a string expression), we aim to validate those parentheses. Let us see this with some examples. We can solve this problem using stack data structure. Here we have taken three sets of braces which are ‘{}’, ‘()’ and ‘[]’. Algorithm Check if stack not empty and current bracket character isRead More »

# Find the starting point of a sorted rotated array.

Given an ascending sorted rotated array, we aim to get the starting point of the array (the smallest element in the input array). Let us discuss this with an example. We use binary search to find the starting point of the sorted array. Let us see how this works. As we are using binary searchRead More »

# Find the number of visible boxes after inserting smaller size boxes into bigger size boxes.

Given an array of integers, each number represents the size of its box. A smaller size box can fit into a larger size box, whereas the same or larger size boxes can’t fit into one another. Let us see this problem with the below example. If we observe the above example, whatever the sized boxRead More »

# Find all possible combinations of decodings of a number.

Given an encoding number as explained below, we aim to print all combinations of decoding strings to that input number. The encoding number is as follows. Defining a recursion for this problem is very simple. Let us see the recursion tree for the input number. Let us now write the code for this recursion algorithm.Read More »

# Remove duplicate characters from a string in-place.

Given a string of length ‘N’ (characters), we aim to remove all the characters which are duplicates in the input string. Let us understand this with the following example. To solve this kind of problem, we follow the same approach as we observed in removing alternate characters in a string in-place. The steps are asRead More »

# Find the next higher number with the same digits as in the input number.

Given a number as a character digits format (i.e., “12345”), we aim to find the next higher number (which is “12354”) in place. Let’s see some examples to understand this better. “12345” converts as “12354” which is its next higher number. “4512987” converts as “4517289” which is its next higher number. Input “4321” remains theRead More »

# Find the number of 1’s in a sorted binary array.

A binary array is an unusual array which only contains binary values (i.e., either 0s or 1s). Given a sorted binary array, we aim to find the number of 1s in the array. Let’s take below array example. We solve this problem using a binary search algorithm as input is a sorted array of 0sRead More »

# Shuffle an integer array

Shuffling is a procedure to randomize the input values by swapping back and forth. In this article, we aim to rearrange the input integer array uniquely. Let’s see an example step by step approach. Step by step approach Firstly, we pick a random index below size-1 (by skipping the last item) and tries to swapRead More »

# Check if two string are anagrams to one another.

Every character in a string exists in another, and the characters in second-string also exist in the first string, then both strings are anagrams. Let us see this with an example. To solve this problem, We take an array of size 26 (to handle 26 alphabets) as showing in below picture. Once we have theRead More »

# Find the smallest and largest integers in an unsorted integer array.

We can solve this by simply iterating through all the input values and then comparing the current value with the existing largest & smallest values. We update the lowest integer if the current value is lesser than that. Similarly, we update the largest value if the value is higher than it. See below example forRead More »

# Find the missing number in given 1 to N natural numbers (integers).

Given an array of integers, which are natural numbers from one to N. In those numbers, one integer is missing. We aim to find the missing number in the array. For example, the integer array {1, 2, 3, 4, 5, 7, 8, 9, 10} is a continuous integer array starting from 1 to 10 exceptRead More »

# Minimum cost to reach the end of the array using recursion and dynamic programming

Given an array of different costs(which is positive integers) of size ‘N.’ And a maximum possible jump from the current index to other index is ‘K.’ We aim to find the minimum cost to reach the end of the array. Let’s take below example to understand this problem better. In the above example, starting fromRead More »