# Category Archives: Arrays

View algorithms on Arrays

## Find element in a row wise and column wise sorted matrix.

Question: Given a matrix that is sorted in row wise and column wise, find a number if present in the most efficient way. Input: Find if 54 is present in matrix. 4 8 15 16 23 6 9 20 21 44 8 11 24 26 49 9 13 25 27 54 10 17 29 30 66 Output: Present… Read More »

## Print a matrix in spiral form.

Question: Write a program in C to print the given matrix in spiral order. Input: 1    2    3    4 5    6    7    8 9   10   11  12 13  14  15  16 Output: 1 2 3 4 8 12 16 15 14 13 9 5 6  7 11 10 Printing a matrix in… Read More »

## Find an element in a sorted array rotated unknown times. (Method 2)

Question: Given a sorted array of n integers that has been rotated an unknown number of times, give a O(log (n)) solution that finds an element in the array? Input: arr [] = {15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14}. Find 5 . Output: 8 (the index of 5 in the array) We… Read More »

## Find the median of a sequence of n integers.

Question: Given a sequence of integers, find the median of the integers? Input: arr [] = {15, 23, 4, 16, 8, 42}. Find median. Output: 15 Since the question does not specify anything we cannot assume that the given array is a sorted one. The definition of median is the (n/2)th element of a sorted sequence of integers.… Read More »

## Find an element in a sorted array rotated unknown times.

Question: Given a sorted array of n integers that has been rotated an unknown number of times, give a O(log (n)) solution that finds an element in the array? Input: arr [] = {15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14}. Find 5 . Output: 8 (the index of 5 in the array) Let… Read More »

## Find two elements in an array such that their sum is closest to 0 (ZERO). (Method 2)

Question: Given an array with both positive and negative numbers, find the two elements such that their sum is closest to zero. ? Input: arr [] = {1, 60, -10, 70, -80, 85} Output: -80, 85 (Their sum is -5 that is closest to 0(zero)) What we discussed was the most naive approach by using two for loops.… Read More »

## Find two elements in an array such that their sum is closest to 0 (ZERO).

Question: Given an array with both positive and negative numbers, find the two elements such that their sum is closest to zero. ? Input: arr [] = {1, 60, -10, 70, -80, 85} Output: -80, 85 (Their sum is -5 that is closest to 0(zero)) We can solve this problem by a brute force approach. That involves two… Read More »

## Find two elements in an array such that their sum equals ‘K’. (Method 2)

Question: Given an array of n elements. Find two elements such that their sum is equal to a given element ‘K’ ? Input: arr [] = {42, 23, 15, 16, 8, 4} and element K = 58 Output: 16, 42 We discussed the approach to this problem using 2 loops in this post. But obviously this was not… Read More »

## Find two elements in an array such that their sum equals ‘K’.

Question: Given an array of n elements. Find two elements such that their sum is equal to a given element ‘K’ ? Input: arr [] = {42, 23, 15, 16, 8, 4} and element K = 58 Output: 16, 42 To understand the problem statement a little more, we are given with an array of ‘n’ elements and… Read More »

## Print 2 repeating elements in a given array. (Method 3)

Question: Given an array with n+2 elements, all elements of the array are in range 1 to n and also all elements occur only once except 2 numbers which occur twice. Find those 2 repeating numbers. Input: arr[] = {6, 2, 6, 5, 2, 3, 1} Output: 6 2 We discussed a method to perform this task using… Read More »