Close

## Finding three elements with zero sum in an array.

Given an array of positive and negative numbers, how do we write a program to check if it has three elements with sum 0. For example, the array  {-31, 19, -6, -45, 12, -8, 3, -77, 56, 29} contains {-31,12,19} with sum zero. One obvious solution is to examine each possible triplet of the array…

## Searching for an element in the sorted matrix

Given a row wise and column wise sorted two dimensional array or matrix. How to search for an element efficiently? Example of the array can be int[][] matrix = {                   {1,3,5,7,9},                   {10,12,14,16,18},                   {19,21,23,25,27},                   {28,30,32,34,36},                   {37,39,41,43,45}                }; This can be done using the following algorithm.  We start our search with the top right element.  If it…

## Printing biggest elements to its right in an array

Given array of elements we have to write a program to print the biggest element to it’s right for each of the elements in the given array. For example let us consider the array {3, 9, 2, 4, 6, 1}. The output for this array should be {9, 6, 6, 6, 1, -1}. As a…

## Alternative arrangement of numbers in the array

Given an array of 2n numbers of the following format {a1, a2, a3,…an, b1, b2, b3, …bn} We have to write a program to transform this into the following format. {a1, b1, a2, b2, a3, b3, …, an, bn} This can be done using the following algorithm. We iterate over the array for (n-1) times. In…

## Finding a number in a sorted array with the least difference from given number

Given a sorted array and a value, we have to find the closest value (with least difference). For example let the input array be {-2, 4, 7, 11, 17} and the input number is 10, the output should be 11. This problem is actually a variation of binary search where in we have to find…

## Moving zeroes to the end of array

Given an array of elements which contains some zeros in between, we have to write a program to move all zeros to the end without disturbing the original order of the elements.For example if the input is {0, 6, 2, 0, 0, 3, 1, 0, 4, 5, 0}, the output should be {6, 2, 3,…

## Printing last K lines from a file

Given a large file containing many lines of text, How do we write an efficient program to print last k lines from that file. One obvious approach is to read the entire file first to find the number of lines (n). In the second iteration skip first (n-k) lines, and print the remaining k lines. …

## Finding the majority element of an array

Given an array of  N elements, the majority element is defined as the one which occurs more than N/2 times. One simple approach is to find the mode (which appears for most number of times) and check if its frequency is greater than N/2. Three strategies for finding the mode are discussed in my previous…

## Inserting an element into a sorted linked list

Given a sorted linked list, how do we insert data into the list in proper order? This is nothing but a step in insertion sort. Simply iterate through the elements to find the position for given number and insert at that position. Here is the Java code to do that. I have used the LinkedList…

## How does a counting sort work?

Counting sort as its name indicates sort a list of integers by counting the occurrence each number appearing in the list. This is possible when the range of integers (particularly small) is given. Unlike comparison based sorting algorithms which run in O( n log n) time in the worst case, Counting sort runs O(n) time.…