Anda di halaman 1dari 7

Decoding Array Puzzles

1. Given an array A[] and a number x, check for pair in A[] with sum as x
Explanation: NA
Category: Drill Down, Binary Map
2. Majority Element
Explanation: A majority element in an array A[] of size n is an element that
appears more than n/2 times (and hence there is at most one such element).
Write a function which takes an array and emits the majority element (if it
exists), otherwise prints NONE as follows:
I/P : 3 3 4 2 4 4 2 4 4
O/P : 4
I/P : 3 3 4 2 4 4 2 4
O/P : NONE
Category:
3. Find the Number Occurring Odd Number of Times
Explanation: Given an array of positive integers. All numbers occur even
number of times except one number which occurs odd number of times. Find
the number in O(n) time & constant space.
Example:
I/P = [1, 2, 3, 2, 3, 1, 3]
O/P = 3
Category: Similarity XOR
4. Largest Sum Contiguous Subarray
Explanation: NA
Category: Keep Track Sum
5. Find the Missing Number
Explanation: You are given a list of n-1 integers and these integers are in the
range of 1 to n. There are no duplicates in list. One of the integers is missing in
the list. Write an efficient code to find the missing integer.
Example:

I/P [1, 2, 4, ,6, 3, 7, 8]


O/P 5
Category: Number Property, Similarity XOR
6. Search an element in a sorted and pivoted array
Explanation: An element in a sorted array can be found in O(log n) time via
binary search. But suppose I rotate the sorted array at some pivot unknown
to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise
a way to find an element in the rotated array in O(log n) time .
Category: Sorted B-Search
7. Merge an array of size n into another array of size m+n
Explanation: There are two sorted arrays. First one is of size m+n containing
only m elements. Another one is of size n and contains n elements. Merge
these two arrays into the first array of size m+n such that the output is
sorted.
Category: Merge
8. Median of two sorted arrays
Explanation: There are 2 sorted arrays A and B of size n each. Write an algorithm
to find the median of the array obtained after merging the above 2 arrays(i.e.
array of length 2n). The complexity should be O(log(n))
Median: In probability theory and statistics, a median is described as the number
separating the higher half of a sample, a population, or a probability distribution,
from the lower half.
The median of a finite list of numbers can be found by arranging all the numbers
from lowest value to highest value and picking the middle one.
For getting the median of input array { 12, 11, 15, 10, 20 }, first sort the array.
We get { 10, 11, 12, 15, 20 } after sorting. Median is the middle element of the
sorted array which is 12.
There are different conventions to take median of an array with even number of
elements, one can take the mean of the two middle values, or first middle value,
or second middle value.
Category: Merge, Sorted B-Search
9. Write a program to reverse an array
Explanation: NA
Category: Swap/Reversal
10.Program for array rotation

Explanation: NA
Category: Swap/Reversal
11.Reversal algorithm for array rotation
Explanation: NA
Category: Swap/Reversal
12.Block swap algorithm for array rotation
Explanation: NA
Category: Swap/Reversal
13.Maximum sum such that no two elements are adjacent
Explanation: Given an array of positive numbers, find the maximum sum of a
subsequence with the constraint that no 2 numbers in the sequence should
be adjacent in the array. So 3 2 7 10 should return 13 (sum of 3 and 10) or 3
2 5 10 7 should return 15 (sum of 3, 5 and 7).
Category:
14.Leaders in an array
Explanation: An element is leader if it is greater than all the elements to its
right side. And the rightmost element is always a leader. For example int the
array {16, 17, 4, 3, 5, 2}, leaders are 17, 5 and 2.
Category: Backward Processing
15.Sort elements by frequency
Explanation: Print the elements of an array in the decreasing frequency if 2
numbers have same frequency then print the one which came 1 st.
E.g. 2 5 2 8 5 6 8 8 output: 8 8 8 2 2 5 5 6.
Category: Binary Map
16.Count Inversions in an array
Explanation: Two elements a[i] and a[j] form an inversion if a[i] > a[j] and i <
j
Category: Merge
17.Two elements whose sum is closest to zero
Explanation: An Array of integers is given, both +ve and -ve. You need to find
the two elements such that their sum is closest to zero.
Category: Sorting, 2-Pointers
18.Find the smallest and second smallest element in an array
Explanation: NA
Category: Simple Scan
19.Check for Majority Element in a sorted array
Explanation: NA
Category: Sorted B-Search
20.Maximum and minimum of an array using minimum number of comparisons
Explanation: NA

Category: Sorted B-Search


21.Segregate 0s and 1s in an array
Explanation: NA
Category: 2-Pointers
22.k largest(or smallest) elements in an array
Explanation: NA
Category: Heap, Sorting
23.Maximum difference between two elements such that larger element appears
after smaller element.
Explanation: NA
Category:
24.Union and Intersection of two sorted arrays
Explanation: NA
Category: Merge
25.Floor and Ceiling in a sorted array
Explanation: Given a sorted array and a value x, the ceiling of x is the smallest
element in array greater than or equal to x, and the floor is the greatest element
smaller than or equal to x. Assume than the array is sorted in non-decreasing
order. Write efficient functions to find floor and ceiling of x.
For
For
For
For
For

example, let the input array be {1, 2, 8, 10, 10, 12, 19}
x = 0: floor doesn't exist in array, ceil = 1
x = 1: floor = 1, ceil = 1
x = 5: floor = 2, ceil = 8
x = 20: floor = 19, ceil doesn't exist in array

Category: Sorted B-Search


26.A Product Array Puzzle
Explanation: Given an array arr[] of n integers, construct a Product Array prod[]
(of same size) such that prod[i] is equal to the product of all the elements of arr[]
except arr[i]. Solve it without division operator and in O(n).
Example:
arr[] = {10, 3, 5, 6, 2}
prod[] = {180, 600, 360, 300, 900}
Category:
27.Segregate Even and Odd numbers

Explanation: NA
Category: 2-Pointers
28.Find the two repeating elements in a given array
Explanation: You are given an array of n+2 elements. All elements of the
array are in range 1 to n. And all elements occur once except two numbers
which occur twice. Find the two repeating numbers.
For example, array = {4, 2, 4, 5, 2, 3, 1} and n = 5
The above array has n + 2 = 7 elements with all elements occurring once
except 2 and 4 which occur twice. So the output should be 4 2.
Category: Number Property
29.Sort an array of 0s, 1s and 2s
Explanation: NA
Category: 2-Pointers
30.Find the Minimum length Unsorted Subarray
Explanation: Given an unsorted array arr[0..n-1] of size n, find the minimum
length subarray arr[s..e] such that sorting this subarray makes the whole
array sorted.
Examples:
1) If the input array is [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60], your
program should be able to find that the subarray lies between the indexes 3
and 8.
2) If the input array is [0, 1, 15, 25, 6, 7, 30, 40, 50], your program should be
able to find that the subarray lies between the indexes 2 and 5.
Category: Scan
31.sorting which makes the complete array sorted
32.Find duplicates in O(n) time and O(1) extra space
33.Equilibrium index of an array
34.Linked List vs Array
35.Which sorting algorithm makes minimum number of memory writes?
36.Turn an image by 90 degree
37.Next Greater Element
38.Check if array elements are consecutive | Added Method 3
39.Find the smallest missing number
40.Count the number of occurrences in a sorted array
41.Interpolation search vs Binary search
42.Given an array arr[], find the maximum j i such that arr[j] > arr[i]

43.Maximum of all subarrays of size k (Added a O(n) method)


44.Find whether an array is subset of another array | Added Method 3
45.Find the minimum distance between two numbers
46.Find the repeating and the missing | Added 3 new methods
47.Median in a stream of integers (running integers)
48.Find a Fixed Point in a given array
49.Maximum Length Bitonic Subarray
50.Find the maximum element in an array which is first increasing and then
decreasing
51.Count smaller elements on right side
52.Minimum number of jumps to reach end
53.Implement two stacks in an array
54.Find subarray with given sum
55.Dynamic Programming | Set 14 (Maximum Sum Increasing Subsequence)
56.Longest Monotonically Increasing Subsequence Size (N log N)
57.Find a triplet that sum to a given value
58.Find the smallest positive number missing from an unsorted array
59.Find the two numbers with odd occurrences in an unsorted array
60.The Celebrity Problem
61.Dynamic Programming | Set 15 (Longest Bitonic Subsequence)
62.Find a sorted subsequence of size 3 in linear time
63.Largest subarray with equal number of 0s and 1s
64.Dynamic Programming | Set 18 (Partition problem)
65.Maximum Product Subarray
66.Find a pair with the given difference
67.Replace every element with the next greatest
68.Dynamic Programming | Set 20 (Maximum Length Chain of Pairs)
69.Find four elements that sum to a given value | Set 1 (n^3 solution)
70.Find four elements that sum to a given value | Set 2 ( O(n^2Logn) Solution)
71.Sort a nearly sorted (or K sorted) array
72.Maximum circular subarray sum
73.Find the row with maximum number of 1s
74.Median of two sorted arrays of different sizes
75.Shuffle a given array
76.Count the number of possible triangles
77.Iterative Quick Sort
78.Find the number of islands
79.Construction of Longest Monotonically Increasing Subsequence (N log N)
80.Find the first circular tour that visits all petrol pumps
81.Arrange given numbers to form the biggest number
82.Pancake sorting
83.A Pancake Sorting Problem
84.Tug of War
85.Divide and Conquer | Set 3 (Maximum Subarray Sum)
86.Counting Sort
87.Merge Overlapping Intervals
88.Find the maximum repeating number in O(n) time and O(1) extra space
89.Stock Buy Sell to Maximize Profit
90.Rearrange positive and negative numbers in O(n) time and O(1) extra space
91.Sort elements by frequency | Set 2

92.Find a peak element


93.Print all possible combinations of r elements in a given array of size n
94.Given an array of of size n and a number k
95.find all elements that appear more than n/k times
96.Find the point where a monotonically increasing function becomes positive
first time
97.Find the Increasing subsequence of length three with maximum product
98.Find the minimum element in a sorted and rotated array
99.Stable Marriage Problem
100.
Merge k sorted arrays | Set 1
101.
Radix Sort
102.
Move all zeroes to end of array
103.
Find number of pairs such that x^y > y^x
104.
Count all distinct pairs with difference equal to k
105.
Find if there is a subarray with 0 sum
106.
Smallest subarray with sum greater than a given value
107.
Sort an array according to the order defined by another array
108.
Maximum Sum Path in Two Arrays
109.
Check if a given array contains duplicate elements within k distance
from each other
110.
Sort an array in wave form
111.
Kth Smallest/Largest Element in Unsorted Array
112.
Kth Smallest/Largest Element in Unsorted Array in Expected Linear
Time
113.
Kth Smallest/Largest Element in Unsorted Array in Worst Case Linear
Time
114.
Find Index of 0 to be replaced with 1 to get longest continuous
sequence of 1s in a binary array
115.
Find the closest pair from two sorted arrays
116.
Given a sorted array and a number x, find the pair in array whose sum
is closest to x
117.
Count 1s in a sorted binary array
118.
Print All Distinct Elements of a given integer array Construct an array
from its pair-sum array
119.
Find common elements in three sorted arrays
120.
Find the first repeating element in an array of integers
121.
Find the smallest positive integer value that cannot be represented as
sum of any subset of a given array

Anda mungkin juga menyukai