Arrays
Arrays
Arrays
1 Given an array A[] and a number x, check for pair in A[] with sum as x 11
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2 Majority Element 18
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1
12 Block swap algorithm for array rotation 58
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
14 Leaders in an array 65
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
23 Maximum difference between two elements such that larger element appears after the
smaller number 100
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
2
26 A Product Array Puzzle 112
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
30 Find the Minimum length Unsorted Subarray, sorting which makes the complete array
sorted 126
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
3
40 Interpolation search vs Binary search 155
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
41 Given an array arr[], find the maximum j - i such that arr[j] > arr[i] 156
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
45 Find the repeating and the missing | Added 3 new methods 172
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
49 Find the maximum element in an array which is first increasing and then decreasing 190
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
4
54 Dynamic Programming | Set 14 (Maximum Sum Increasing Subsequence) 212
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
57 Find the smallest positive number missing from an unsorted array 225
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
58 Find the two numbers with odd occurrences in an unsorted array 228
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
66 Replace every element with the greatest element on right side 256
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
5
68 Find four elements that sum to a given value | Set 1 (nˆ3 solution) 260
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
69 Find four elements that sum to a given value | Set 2 ( O(nˆ2Logn) Solution) 263
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
79 Find the first circular tour that visits all petrol pumps 299
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
6
82 A Pancake Sorting Problem 308
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
87 Find the maximum repeating number in O(n) time and O(1) extra space 326
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
89 Rearrange positive and negative numbers in O(n) time and O(1) extra space 332
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
93 Given an array of of size n and a number k, find all elements that appear more than n/k
times 346
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
94 Unbounded Binary Search Example (Find the point where a monotonically increasing
function becomes positive first time) 352
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
95 Find the Increasing subsequence of length three with maximum product 355
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
7
96 Find the minimum element in a sorted and rotated array 357
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
107Check if a given array contains duplicate elements within k distance from each other 403
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
8
110K’th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time) 418
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
111K’th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time) 421
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
116Find the smallest positive integer value that cannot be represented as sum of any subset
of a given array 437
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
117Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ 440
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
9
124Maximum profit by buying and selling a share at most twice 468
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
10
Chapter 1
Write a C program that, given an array A[] of n numbers and another number x, determines whether or not
there exist two elements in S whose sum is exactly x.
METHOD 1 (Use Sorting)
Algorithm:
Auxiliary Space : Again, depends on sorting algorithm. For example auxiliary space is O(n) for merge sort and
Example:
11
Implementation:
# include <stdio.h>
# define bool int
getchar();
return 0;
}
12
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
Python
13
while l<r:
if (A[l] + A[r] == sum):
return 1
elif (A[l] + A[r] < sum):
l += 1
else:
r -= 1
return 0
return i+1
Output:
14
1) Initialize Binary Hash Map M[] = {0, 0, …}
2) Do following for each element A[i] in A[]
(a) If M[x - A[i]] is set then print the pair (A[i], x – A[i])
(b) Set M[A[i]]
Implementation:
C/C++
#include <stdio.h>
#define MAX 100000
getchar();
return 0;
}
Java
class PairSum
{
private static final int MAX = 100000; // Max size of Hashmap
15
static void printpairs(int arr[],int sum)
{
//Declares and initializes the whole array as false
boolean[] binmap = new boolean[MAX];
Python
# Python program to find if there are two elements wtih given sum
CONST_MAX = 100000
for i in range(0,arr_size):
temp = sum-arr[i]
if (temp>=0 and binmap[temp]==1):
print "Pair with the given sum is", arr[i], "and", temp
binmap[arr[i]]=1
16
printPairs(A, len(A), n)
Source
http://www.geeksforgeeks.org/write-a-c-program-that-given-a-set-a-of-n-numbers-and-another-number-x-determines-wheth
Category: Arrays Tags: Hashing
Post navigation
← Next Power of 2 Majority Element →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
17
Chapter 2
Majority Element
Majority Element: 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
METHOD 1 (Basic)
The basic solution is to have two loops and keep track of maximum count for all different elements. If
maximum count becomes greater than n/2 then break the loops and return the element having maximum
count. If maximum count doesn’t become more than n/2 then majority element doesn’t exist.
Time Complexity: O(n*n).
Auxiliary Space : O(1).
METHOD 2 (Using Binary Search Tree)
Thanks to Sachin Midha for suggesting this solution.
Node of the Binary Search Tree (used in this approach) will be as follows.
struct tree
{
int element;
int count;
}BST;
Insert elements in BST one by one and if an element is already present then increment the count of the
node. At any stage, if count of a node becomes more than n/2 then return.
The method works well for the cases where n/2+1 occurrences of the majority element is present in the
starting of the array, for example {1, 1, 1, 1, 1, 2, 3, 4}.
18
Time Complexity: If a binary search tree is used then time complexity will be O(nˆ2). If a self-
balancing-binary-search tree is used then O(nlogn)
Auxiliary Space: O(n)
METHOD 3 (Using Moore’s Voting Algorithm)
This is a two step process.
1. Get an element occurring most of the time in the array. This phase will make sure that if there is a
majority element then it will return that only.
2. Check if the element obtained from above step is majority element.
1. Finding a Candidate:
The algorithm for first phase that works in O(n) is known as Moore’s Voting Algorithm. Basic idea of the
algorithm is if we cancel out each occurrence of an element e with all the other elements that are different
from e then e will exist till end if it is a majority element.
findCandidate(a[], size)
1. Initialize index and count of majority element
maj_index = 0, count = 1
2. Loop for i = 1 to size – 1
(a)If a[maj_index] == a[i]
count++
(b)Else
count--;
(c)If count == 0
maj_index = i;
count = 1
3. Return a[maj_index]
Above algorithm loops through each element and maintains a count of a[maj_index], If next element is same
then increments the count, if next element is not same then decrements the count, and if the count reaches
0 then changes the maj_index to the current element and sets count to 1.
First Phase algorithm gives us a candidate element. In second phase we need to check if the candidate is
really a majority element. Second phase is simple and can be easily done in O(n). We just need to check if
count of the candidate element is greater than n/2.
Example:
A[] = 2, 2, 3, 5, 2, 2, 6
Initialize:
maj_index = 0, count = 1 –> candidate ‘2?
2, 2, 3, 5, 2, 2, 6
Same as a[maj_index] => count = 2
2, 2, 3, 5, 2, 2, 6
Different from a[maj_index] => count = 1
2, 2, 3, 5, 2, 2, 6
Different from a[maj_index] => count = 0
Since count = 0, change candidate for majority element to 5 => maj_index = 3, count = 1
2, 2, 3, 5, 2, 2, 6
Different from a[maj_index] => count = 0
Since count = 0, change candidate for majority element to 2 => maj_index = 4
2, 2, 3, 5, 2, 2, 6
19
Same as a[maj_index] => count = 2
2, 2, 3, 5, 2, 2, 6
Different from a[maj_index] => count = 1
Finally candidate for majority element is 2.
First step uses Moore’s Voting Algorithm to get a candidate for majority element.
2. Check if the element obtained in step 1 is majority
Implementation of method 3:
20
{
maj_index = i;
count = 1;
}
}
return a[maj_index];
}
Source
http://www.geeksforgeeks.org/majority-element/
21
Chapter 3
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
A Simple Solution is to run two nested loops. The outer loop picks all elements one by one and inner
loop counts number of occurrences of the element picked by outer loop. Time complexity of this solution is
O(n2 ).
A Better Solution is to use Hashing. Use array elements as key and their counts as value. Create an empty
hash table. One by one traverse the given array elements and store counts. Time complexity of this solution
is O(n). But it requires extra space for hashing.
The Best Solution is to do bitwise XOR of all the elements. XOR of all elements gives us odd occurring
element. Please note that XOR of two elements is 0 if both elements are same and XOR of a number x with
0 is x.
Below are implementations of this best approach.
Program:
C/C++
#include <stdio.h>
return res;
}
22
/* Diver function to test above function */
int main()
{
int ar[] = {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
int n = sizeof(ar)/sizeof(ar[0]);
printf("%d", getOddOccurrence(ar, n));
return 0;
}
Python
def getOddOccurrence(arr):
# Initialize result
res = 0
return res
# Test array
arr = [ 2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2]
Output:
Source
http://www.geeksforgeeks.org/find-the-number-occurring-odd-number-of-times/
23
Chapter 4
Write an efficient C program to find the sum of contiguous subarray within a one-dimensional array of
numbers which has the largest sum.
Kadane’s Algorithm:
Initialize:
max_so_far = 0
max_ending_here = 0
Simple idea of the Kadane's algorithm is to look for all positive contiguous segments of the array (max_endin
max_so_far = max_ending_here = 0
C++
24
int max_so_far = 0, max_ending_here = 0;
Python
max_so_far = 0
max_ending_here = 0
return max_so_far
Output:
25
Maximum contiguous sum is 7
Notes:
Algorithm doesn’t work for all negative numbers. It simply returns 0 if all numbers are negative. For
handling this we can add an extra phase before actual implementation. The phase will look if all numbers
are negative, if they are it will return maximum of them (or smallest in terms of absolute value). There may
be other ways to handle it though.
Above program can be optimized further, if we compare max_so_far with max_ending_here only if
max_ending_here is greater than 0.
C++
Python
def maxSubArraySum(a,size):
max_so_far = 0
max_ending_here = 0
return max_so_far
26
Time Complexity: O(n)
Algorithmic Paradigm: Dynamic Programming
Following is another simple implementation suggested by Mohit Kumar. The implementation handles the
case when all numbers in array are negative.
C++
#include<iostream>
using namespace std;
Python
def maxSubArraySum(a,size):
max_so_far =a[0]
curr_max = a[0]
for i in range(1,size):
curr_max = max(a[i], curr_max + a[i])
max_so_far = max(max_so_far,curr_max)
return max_so_far
27
a = [-2, -3, 4, -1, -2, 1, 5, -3]
print"Maximum contiguous sum is" , maxSubArraySum(a,len(a))
Output:
Source
http://www.geeksforgeeks.org/largest-sum-contiguous-subarray/
28
Chapter 5
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
Program:
#include<stdio.h>
29
/*program to test above function */
int main()
{
int a[] = {1,2,4,5,6};
int miss = getMissingNo(a,5);
printf("%d", miss);
getchar();
}
1) XOR all the array elements, let the result of XOR be X1.
2) XOR all numbers from 1 to n, let XOR be X2.
3) XOR of X1 and X2 gives the missing number.
#include<stdio.h>
return (x1^x2);
}
30
Source
http://www.geeksforgeeks.org/find-the-missing-number/
Category: Arrays
31
Chapter 6
An element in a sorted array can be found in O(log n) time via binary search. But suppose we rotate an
ascending order 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.
All solutions provided here assume that all elements in array are distinct.
The idea is to find the pivot point, divide the array in two sub-arrays and call binary search.
The main idea for finding pivot is – for a sorted (in increasing order) and pivoted array, pivot element is the
only only element for which next element to it is smaller than it.
Using above criteria and binary search methodology we can get pivot element in O(logn) time
Implementation:
32
/* Program to search an element in a sorted and pivoted array*/
#include <stdio.h>
33
return binarySearch(arr, low, (mid -1), key);
}
Output:
34
/* If arr[l...mid] is sorted */
if (arr[l] <= arr[mid])
{
/* As this subarray is sorted, we can quickly
check if key lies in half or other half */
if (key >= arr[l] && key <= arr[mid])
return search(arr, l, mid-1, key);
// Driver program
int main()
{
int arr[] = {4, 5, 6, 7, 8, 9, 1, 2, 3};
int n = sizeof(arr)/sizeof(arr[0]);
int key = 6
int i = search(arr, 0, n-1, key);
if (i != -1) cout << "Index: " << i << endl;
else cout << "Key not found\n";
}
Output:
Index: 2
Given a sorted and rotated array, find if there is a pair with a given sum.
Please write comments if you find any bug in above codes/algorithms, or find other ways to solve the same
problem.
35
Source
http://www.geeksforgeeks.org/search-an-element-in-a-sorted-and-pivoted-array/
Category: Arrays Tags: array, Binary-Search
Post navigation
← Function to check if a singly linked list is palindrome The Great Tree-List Recursion Problem. →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
36
Chapter 7
Asked by Binod
Question:
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.
Algorithm:
37
Let first array be mPlusN[] and other array be N[]
1) Move m elements of mPlusN[] to end.
2) Start from nth element of mPlusN[] and 0th element of N[] and merge them
into mPlusN[].
Implementation:
#include <stdio.h>
38
/* Utility that prints out an array on a line */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
return 0;
}
Output:
2 5 7 8 9 13 15 20 25
Source
http://www.geeksforgeeks.org/merge-one-array-of-size-n-into-another-one-of-size-mn/
Category: Arrays Tags: array
39
Chapter 8
Question: 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.
Let us see different methods to get the median of two sorted arrays of size n each. Since size of the set for
which we are looking for median is even (2n), we are taking average of middle two numbers in all below
solutions.
Method 1 (Simply count while Merging)
Use merge procedure of merge sort. Keep track of count while comparing elements of two arrays. If count
becomes n(For 2n elements), we have reached the median. Take the average of the elements at indexes n-1
and n in the merged array. See the below implementation.
Implementation:
#include <stdio.h>
40
of elements at index n-1 and n in the array obtained after
merging ar1 and ar2 */
for (count = 0; count <= n; count++)
{
/*Below is to handle case where all elements of ar1[] are
smaller than smallest(or first) element of ar2[]*/
if (i == n)
{
m1 = m2;
m2 = ar2[0];
break;
}
int n1 = sizeof(ar1)/sizeof(ar1[0]);
int n2 = sizeof(ar2)/sizeof(ar2[0]);
if (n1 == n2)
printf("Median is %d", getMedian(ar1, ar2, n1));
else
printf("Doesn't work for arrays of unequal size");
getchar();
return 0;
}
41
Time Complexity: O(n)
Example:
m1 = 26 m2 = 13.
42
m1 is greater than m2. So the subarrays become
Implementation:
#include<stdio.h>
if (n == 1)
return (ar1[0] + ar2[0])/2;
if (n == 2)
return (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1])) / 2;
43
/* if m1 > m2 then median must exist in ar1[....m1] and ar2[m2...] */
else
{
if (n % 2 == 0)
return getMedian(ar2 + n/2 - 1, ar1, n - n/2 + 1);
else
return getMedian(ar2 + n/2, ar1, n - n/2);
}
}
getchar();
return 0;
}
/* Utility functions */
int max(int x, int y)
{
return x > y? x : y;
}
44
The basic idea is that if you are given two arrays ar1[] and ar2[] and know the length of each, you can check
whether an element ar1[i] is the median in constant time. Suppose that the median is ar1[i]. Since the array
is sorted, it is greater than exactly i values in array ar1[]. Then if it is the median, it is also greater than
exactly j = n – i – 1 elements in ar2[].
It requires constant time to check if ar2[j] 1) Get the middle element of ar1[] using array indexes left and
right. Let index of the middle element be i. 2) Calculate the corresponding index j of ar2[] j = n – i – 1 3)
If ar1[i] >= ar2[j] and ar1[i]
Example:
Middle element of ar1[] is 7. Let us compare 7 with 23 and 30, since 7 smaller than both 23 and 30, move to
right in ar1[]. Do binary search in {10, 13}, this step will pick 10. Now compare 10 with 15 and 23. Since 10
is smaller than both 15 and 23, again move to right. Only 13 is there in right side now. Since 13 is greater
than 11 and smaller than 15, terminate here. We have got the median as 12 (average of 11 and 13)
Implementation:
#include<stdio.h>
int getMedianRec(int ar1[], int ar2[], int left, int right, int n);
i = (left + right)/2;
j = n - i - 1; /* Index of ar2[] */
45
if (i == 0 || ar2[j] > ar1[i-1])
return (ar1[i] + ar2[j])/2;
else
return (ar1[i] + ar1[i-1])/2;
}
getchar();
return 0;
}
46
// last element of ar2
if (ar2[n-1] < ar1[0])
return (ar2[n-1]+ar1[0])/2;
References:
http://en.wikipedia.org/wiki/Median
http://ocw.alfaisal.edu/NR/rdonlyres/Electrical-Engineering-and-Computer-Science/6-046JFall-2005/30C68118-
E436-4FE3-8C79-6BAFBB07D935/0/ps9sol.pdf ds3etph5wn
Asked by Snehal
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same
problem.
Source
http://www.geeksforgeeks.org/median-of-two-sorted-arrays/
Category: Arrays Tags: Divide and Conquer
Post navigation
← Database Management Systems | Set 1 C Language | Set 1 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
47
Chapter 9
Iterative way:
1) Initialize start and end indexes.
start = 0, end = n-1
2) In a loop, swap arr[start] with arr[end] and change start and end as follows.
start = start +1; end = end – 1
printf("\n");
}
48
printArray(arr, 6);
getchar();
return 0;
}
printf("\n");
}
49
Source
http://www.geeksforgeeks.org/write-a-program-to-reverse-an-array/
Category: Arrays Tags: array
50
Chapter 10
leftRotate(arr[], d, n)
start
For i = 0 to i
To rotate by one, store arr[0] in a temporary variable temp, move arr[1] to arr[0], arr[2] to arr[1] …and fin
Let us take the same example arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2
51
We get [2, 3, 4, 5, 6, 7, 1] after first rotation and [ 3, 4, 5, 6, 7, 1, 2] after second rotation.
52
a) Elements are first moved in first set – (See below diagram for this movement)
/*UTILITY FUNCTIONS*/
/* function to print an array */
void printArray(int arr[], int size)
{
int i;
for(i = 0; i < size; i++)
printf("%d ", arr[i]);
}
53
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b, a%b);
}
Source
http://www.geeksforgeeks.org/array-rotation/
Category: Arrays Tags: array
Post navigation
← Program to count leaf nodes in a binary tree Reversal algorithm for array rotation →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
54
Chapter 11
rotate(arr[], d, n)
reverse(arr[], 1, d) ;
reverse(arr[], d + 1, n);
reverse(arr[], l, n);
Let AB are the two parts of the input array where A = arr[0..d-1] and B = arr[d..n-1]. The idea of the
algorithm is:
Reverse A to get ArB. /* Ar is reverse of A */
Reverse B to get ArBr. /* Br is reverse of B */
Reverse all to get (ArBr) r = BA.
For arr[] = [1, 2, 3, 4, 5, 6, 7], d =2 and n = 7
A = [1, 2] and B = [3, 4, 5, 6, 7]
Reverse A, we get ArB = [2, 1, 3, 4, 5, 6, 7]
Reverse B, we get ArBr = [2, 1, 7, 6, 5, 4, 3]
Reverse all, we get (ArBr)r = [3, 4, 5, 6, 7, 1, 2]
Implementation:
55
/*Utility function to print an array */
void printArray(int arr[], int size);
/*UTILITY FUNCTIONS*/
/* function to print an array */
void printArray(int arr[], int size)
{
int i;
for(i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("%\n ");
}
56
Source
http://www.geeksforgeeks.org/program-for-array-rotation-continued-reversal-algorithm/
57
Chapter 12
Algorithm:
Recursive Implementation:
58
#include<stdio.h>
/* If A is shorter*/
if(d < n-d)
{
swap(arr, 0, n-d, d);
leftRotate(arr, d, n-d);
}
else /* If B is shorter*/
{
swap(arr, 0, d, n-d);
leftRotate(arr+n-d, 2*d-n, d); /*This is tricky*/
}
}
/*UTILITY FUNCTIONS*/
/* function to print an array */
void printArray(int arr[], int size)
{
int i;
for(i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("%\n ");
}
59
arr[si + i] = temp;
}
}
Iterative Implementation:
Here is iterative implementation of the same algorithm. Same utility function swap() is used here.
60
Source
http://www.geeksforgeeks.org/block-swap-algorithm-for-array-rotation/
Category: Arrays Tags: array
Post navigation
← Reversal algorithm for array rotation Data Structures and Algorithms | Set 3 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
61
Chapter 13
Question: 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).Answer the question in most efficient way.
Algorithm:
Loop for all elements in arr[] and maintain two sums incl and excl where incl = Max sum including the
previous element and excl = Max sum excluding the previous element.
Max sum excluding the current element will be max(incl, excl) and max sum including the current element
will be excl + current element (Note that only excl is considered because elements cannot be adjacent).
At the end of the loop return max of incl and excl.
Example:
inc = 5
exc = 0
62
For i = 5 (current element is 35)
incl = (excl + arr[i]) = 80
excl = max(5, 15) = 65
#include<stdio.h>
63
Source
http://www.geeksforgeeks.org/maximum-sum-such-that-no-two-elements-are-adjacent/
Category: Arrays Tags: array
64
Chapter 14
Leaders in an array
Write a program to print all the LEADERS in the array. 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.
Let the input array be arr[] and size of the array be size.
Method 1 (Simple)
Use two loops. The outer loop runs from 0 to size – 1 and one by one picks all elements from left to right.
The inner loop compares the picked element to all the elements to its right side. If the picked element is
greater than all the elements to its right side, then the picked element is the leader.
C++
#include<iostream>
using namespace std;
65
printLeaders(arr, n);
return 0;
}
Python
def printLeaders(arr,size):
# Driver function
arr=[16, 17, 4, 3, 5, 2]
printLeaders(arr, len(arr))
Output:
17 5 2
#include <iostream>
using namespace std;
66
{
max_from_right = arr[i];
cout << max_from_right << " ";
}
}
}
Python
max_from_right = arr[size-1]
print max_from_right,
for i in range( size-2, 0, -1):
if max_from_right < arr[i]:
print arr[i],
max_from_right = arr[i]
# Driver function
arr = [16, 17, 4, 3, 5, 2]
printLeaders(arr, len(arr))
Output
2 5 17
Source
http://www.geeksforgeeks.org/leaders-in-an-array/
67
Chapter 15
Print the elements of an array in the decreasing frequency if 2 numbers have same frequency then print the
one which came first.
Examples:
Example:
Input 2 5 2 8 5 6 8 8
Sort by count
68
8, 3
2, 2
5, 2
6, 1
Input 5 2 2 8 5 6 8 8
69
// Used for sorting by frequency. And if frequency is same,
// then by appearance
bool mycomp2(struct ele a, struct ele b) {
if (a.count != b.count) return (a.count < b.count);
else return a.index > b.index;
}
/* Now we have counts and first index for each element so now
sort on the basis of count and in case of tie use index
to sort.*/
stable_sort(element, element+n, mycomp2);
for (int i = n-1, index=0; i >= 0; i--)
70
if (element[i].count != -1)
for (int j=0; j<element[i].count; j++)
arr[index++] = element[i].val;
}
// Driver program
int main()
{
int arr[] = {2, 5, 2, 6, -1, 9999999, 5, 8, 8, 8};
int n = sizeof(arr)/sizeof(arr[0]);
sortByFrequency(arr, n);
Output:
8 8 8 2 2 5 5 6 -1 9999999
struct tree
{
int element;
int first_index /*To handle ties in counts*/
int count;
}BST;
71
Source
http://www.geeksforgeeks.org/sort-elements-by-frequency/
Category: Arrays Tags: array
Post navigation
← Data Structures and Algorithms | Set 8 Count Inversions in an array →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
72
Chapter 16
Inversion Count for an array indicates – how far (or close) the array is from being sorted. If array is already
sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.
Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i Example:
The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3).
METHOD 1 (Simple)
For each element, count number of elements which are on right side of it and are smaller than it.
return inv_count;
}
73
How to get number of inversions in merge()?
In merge process, let i is used for indexing left sub-array and j for right sub-array. At any step in merge(),
if a[i] is greater than a[j], then there are (mid – i) inversions. because left and right subarrays are sorted, so
all the remaining elements in left-subarray (a[i+1], a[i+2] … a[mid]) will be greater than a[j]
74
Implementation:
#include <stdio.h>
#include <stdlib.h>
75
if (right > left)
{
/* Divide the array into two parts and call _mergeSortAndCountInv()
for each of the parts */
mid = (right + left)/2;
/* This funt merges two sorted arrays and returns inversion count in
the arrays.*/
int merge(int arr[], int temp[], int left, int mid, int right)
{
int i, j, k;
int inv_count = 0;
76
arr[i] = temp[i];
return inv_count;
}
Note that above code modifies (or sorts) the input array. If we want to count only inversions then we need
to create a copy of original array and call mergeSort() on copy.
References:
http://www.cs.umd.edu/class/fall2009/cmsc451/lectures/Lec08-inversions.pdf
http://www.cp.eng.chula.ac.th/~piak/teaching/algo/algo2008/count-inv.htm
Please write comments if you find any bug in the above program/algorithm or other ways to solve the same
problem.
Source
http://www.geeksforgeeks.org/counting-inversions/
Category: Arrays Tags: Divide and Conquer
Post navigation
← Sort elements by frequency | Set 1 Operating Systems | Set 4 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
77
Chapter 17
Question: 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.
For the below array, program should print -80 and 85.
METHOD 1 (Simple)
For each element, find the sum of it with every other element in the array and compare sums. Finally, return
the minimum sum.
Implementation
# include <stdio.h>
# include <stdlib.h> /* for abs() */
# include <math.h>
void minAbsSumPair(int arr[], int arr_size)
{
int inv_count = 0;
int l, r, min_sum, sum, min_l, min_r;
/* Initialization of values */
min_l = 0;
min_r = 1;
min_sum = arr[0] + arr[1];
78
{
for(r = l+1; r < arr_size; r++)
{
sum = arr[l] + arr[r];
if(abs(min_sum) > abs(sum))
{
min_sum = sum;
min_l = l;
min_r = r;
}
}
}
printf(" The two elements whose sum is minimum are %d and %d",
arr[min_l], arr[min_r]);
}
# include <stdio.h>
# include <math.h>
# include <limits.h>
79
// left and right index variables
int l = 0, r = n-1;
// variable to keep track of the left and right pair for min_sum
int min_l = l, min_r = n-1;
while(l < r)
{
sum = arr[l] + arr[r];
printf(" The two elements whose sum is minimum are %d and %d",
arr[min_l], arr[min_r]);
}
80
*a = *b;
*b = temp;
}
Time Complexity: complexity to sort + complexity of finding the optimum pair = O(nlogn) + O(n) =
O(nlogn)
Asked by Vineet
Please write comments if you find any bug in the above program/algorithm or other ways to solve the same
problem.
Source
http://www.geeksforgeeks.org/two-elements-whose-sum-is-closest-to-zero/
Category: Arrays Tags: array
81
Post navigation
← Why C treats array parameters as pointers? C Language | Set 4 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
82
Chapter 18
Question: Write an efficient C program to find smallest and second smallest element in an array.
Difficulty Level: Rookie
Algorithm:
Implementation:
#include <stdio.h>
#include <limits.h> /* For INT_MAX */
83
first and second */
if (arr[i] < first)
{
second = first;
first = arr[i];
}
Output:
The same approach can be used to find the largest and second largest elements in an array.
Time Complexity: O(n)
Please write comments if you find any bug in the above program/algorithm or other ways to solve the same
problem.
Source
http://www.geeksforgeeks.org/to-find-smallest-and-second-smallest-element-in-an-array/
Category: Arrays Tags: array
84
Chapter 19
Question: Write a C function to find if a given integer x appears more than n/2 times in a sorted array of
n integers.
Basically, we need to write a function say isMajority() that takes an array (arr[] ), array’s size (n) and a
number to be searched (x) as parameters and returns true if x is a majority element(present more than n/2
times).
Examples:
85
/* search for first occurrence of x in arr[]*/
for (i = 0; i < last_index; i++)
{
/* check if x is present and is present more than n/2 times */
if (arr[i] == x && arr[i+n/2] == x)
return 1;
}
return 0;
}
getchar();
return 0;
}
86
if (((i + n/2) <= (n -1)) && arr[i + n/2] == x)
return true;
else
return false;
}
return -1;
}
return 0;
}
87
Source
http://www.geeksforgeeks.org/check-for-majority-element-in-a-sorted-array/
Category: Arrays Tags: Divide and Conquer
Post navigation
← Data Structures and Algorithms | Set 13 Maximum and minimum of an array using minimum number of
comparisons →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
88
Chapter 20
Write a C function to return minimum and maximum in an array. You program should make
minimum number of comparisons.
First of all, how do we return multiple values from a C function? We can do it either using structures or
pointers.
We have created a structure named pair (which contains min and max) to return multiple values.
struct pair
{
int min;
int max;
};
And the function declaration becomes: struct pair getMinMax(int arr[], int n) where arr[] is the array of size
n whose minimum and maximum are needed.
METHOD 1 (Simple Linear Search)
Initialize values of min and max as minimum and maximum of the first two elements respectively. Starting
from 3rd, compare each element with max and min, and change max and min accordingly (i.e., if the element
is smaller than min then change min, else if the element is greater than max then change max, else ignore
the element)
89
struct pair getMinMax(int arr[], int n)
{
struct pair minmax;
int i;
/*If there is only one element then return it as min and max both*/
if (n == 1)
{
minmax.max = arr[0];
minmax.min = arr[0];
return minmax;
}
return minmax;
}
90
METHOD 2 (Tournament Method)
Divide the array into two parts and compare the maximums and minimums of the the two parts to get the
maximum and the minimum of the the whole array.
Implementation
91
minmax.max = arr[high];
minmax.min = arr[low];
}
return minmax;
}
return minmax;
}
T(n) = 2T(n/2) + 2
92
T(n) = 3/2n -2
Thus, the approach does 3/2n -2 comparisons if n is a power of 2. And it does more than 3/2n -2 comparisons
if n is not a power of 2.
METHOD 3 (Compare in Pairs)
If n is odd then initialize min and max as first element.
If n is even then initialize min and max as minimum and maximum of the first two elements respectively.
For rest of the elements, pick them in pairs and compare their
maximum and minimum with max and min respectively.
#include<stdio.h>
93
/* In the while loop, pick elements in pair and
compare the pair with max and min so far */
while (i < n-1)
{
if (arr[i] > arr[i+1])
{
if(arr[i] > minmax.max)
minmax.max = arr[i];
if(arr[i+1] < minmax.min)
minmax.min = arr[i+1];
}
else
{
if (arr[i+1] > minmax.max)
minmax.max = arr[i+1];
if (arr[i] < minmax.min)
minmax.min = arr[i];
}
i += 2; /* Increment the index by 2 as two
elements are processed in loop */
}
return minmax;
}
If n is odd: 3*(n-1)/2
If n is even: 1 Initial comparison for initializing min and max,
and 3(n-2)/2 comparisons for rest of the elements
= 1 + 3*(n-2)/2 = 3n/2 -2
Second and third approaches make equal number of comparisons when n is a power of 2.
In general, method 3 seems to be the best.
Please write comments if you find any bug in the above programs/algorithms or a better way to solve the
same problem.
94
Source
http://www.geeksforgeeks.org/maximum-and-minimum-in-an-array/
Category: Arrays Tags: Divide and Conquer
Post navigation
← Check for Majority Element in a sorted array Data Structures and Algorithms | Set 14 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
95
Chapter 21
Asked by kapil.
You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the
array. Traverse array only once.
96
Source
http://www.geeksforgeeks.org/segregate-0s-and-1s-in-an-array-by-traversing-array-once/
Category: Arrays
97
Chapter 22
Question: Write an efficient program for printing k largest elements in an array. Elements in array can be
in any order.
For example, if given array is [1, 23, 12, 9, 30, 2, 50] and you are asked for the largest 3 elements i.e., k = 3
then your program should print 50, 30 and 23.
98
Method 5(Use Oder Statistics)
1) Use order statistic algorithm to find the kth largest element. Please see the topic selection in worst-case
linear time O(n)
2) Use QuickSort Partition algorithm to partition around the kth largest number O(n).
3) Sort the k-1 elements (elements greater than the kth largest element) O(kLogk). This step is needed only
if sorted output is required.
Time complexity: O(n) if we don’t need the sorted output, otherwise O(n+kLogk)
Thanks to Shilpifor suggesting the first two approaches.
Method 6 (Use Min Heap)
This method is mainly an optimization of method 1. Instead of using temp[] array, use Min Heap.
Thanks to geek4ufor suggesting this method.
1) Build a Min Heap MH of the first k elements (arr[0] to arr[k-1]) of the given array. O(k)
2) For each element, after the kth element (arr[k] to arr[n-1]), compare it with root of MH.
……a) If the element is greater than the root then make it root and call heapifyfor MH
……b) Else ignore it.
// The step 2 is O((n-k)*logk)
3) Finally, MH has k largest elements and root of the MH is the kth largest element.
Time Complexity: O(k + (n-k)Logk) without sorted output. If sorted output is needed then O(k + (n-k)Logk
+ kLogk)
All of the above methods can also be used to find the kth largest (or smallest) element.
Please write comments if you find any of the above explanations/algorithms incorrect, or find better ways
to solve the same problem.
References:
http://en.wikipedia.org/wiki/Selection_algorithm
Asked by geek4u
Source
http://www.geeksforgeeks.org/k-largestor-smallest-elements-in-an-array/
99
Chapter 23
Given an array arr[] of integers, find out the difference between any two elements such that larger element
appears after the smaller number in arr[].
Examples: If array is [2, 3, 10, 6, 4, 8, 1] then returned value should be 8 (Diff between 10 and 2). If array
is [ 7, 9, 5, 6, 3, 2 ] then returned value should be 2 (Diff between 7 and 9)
Method 1 (Simple)
Use two loops. In the outer loop, pick elements one by one and in the inner loop calculate the difference
of the picked element with every other element in the array and compare the difference with the maximum
difference calculated so far.
#include<stdio.h>
100
/* Driver program to test above function */
int main()
{
int arr[] = {1, 2, 90, 10, 110};
printf("Maximum difference is %d", maxDiff(arr, 5));
getchar();
return 0;
}
#include<stdio.h>
101
Like min element, we can also keep track of max element from right side. See below code suggested by
Katamaran
#include<stdio.h>
102
return max_diff;
}
Output:
98
This method is also O(n) time complexity solution, but it requires O(n) extra space
Time Complexity: O(n)
Auxiliary Space: O(n)
We can modify the above method to work in O(1) extra space. Instead of creating an auxiliary array, we
can calculate diff and max sum in same loop. Following is the space optimized version.
return max_sum;
}
103
Please write comments if you find any bug in above codes/algorithms, or find other ways to solve the same
problem
Source
http://www.geeksforgeeks.org/maximum-difference-between-two-elements/
104
Chapter 24
#include<stdio.h>
105
/* Print remaining elements of the larger array */
while(i < m)
printf(" %d ", arr1[i++]);
while(j < n)
printf(" %d ", arr2[j++]);
}
#include<stdio.h>
106
int main()
{
int arr1[] = {1, 2, 4, 5, 6};
int arr2[] = {2, 3, 5, 7};
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
printIntersection(arr1, arr2, m, n);
getchar();
return 0;
}
Another approach that is useful when difference between sizes of two given arrays is
significant.
The idea is to iterate through the shorter array and do a binary search for every element of short array
in big array (note that arrays are sorted). Time complexity of this solution is O(min(mLogn, nLogm)).
This solution works better than the above approach when ratio of larger length to smaller is more than
logarithmic order.
See following post for unsorted arrays.
Source
http://www.geeksforgeeks.org/union-and-intersection-of-two-sorted-arrays-2/
107
Chapter 25
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 example, let the input array be {1, 2, 8, 10, 10, 12, 19}
For x = 0: floor doesn't exist in array, ceil = 1
For x = 1: floor = 1, ceil = 1
For x = 5: floor = 2, ceil = 8
For x = 20: floor = 19, ceil doesn't exist in array
In below methods, we have implemented only ceiling search functions. Floor search can be implemented in
the same way.
Method 1 (Linear Search)
Algorithm to search ceiling of x:
1) If x is smaller than or equal to the first element in array then return 0(index of first element)
2) Else Linearly search for an index i such that x lies between arr[i] and arr[i+1].
3) If we do not find an index i in step 2, then return -1
#include<stdio.h>
108
return i;
#include<stdio.h>
109
mid = (low + high)/2; /* low + (high - low)/2 */
Source
http://www.geeksforgeeks.org/search-floor-and-ceil-in-a-sorted-array/
Category: Arrays
Post navigation
110
← G-Fact 7 Practice Questions for Recursion | Set 1 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
111
Chapter 26
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}
Algorithm:
1) Construct a temporary array left[] such that left[i] contains product of all elements on left of arr[i] excluding
arr[i].
2) Construct another temporary array right[] such that right[i] contains product of all elements on on right
of arr[i] excluding arr[i].
3) To get prod[], multiply left[] and right[].
Implementation:
#include<stdio.h>
#include<stdlib.h>
int i, j;
112
/* Construct the left array */
for(i = 1; i < n; i++)
left[i] = arr[i-1]*left[i-1];
return;
}
113
temp *= arr[i];
}
return;
}
Source
http://www.geeksforgeeks.org/a-product-array-puzzle/
Category: Arrays
114
Chapter 27
Given an array A[], write a function that segregates even and odd numbers. The functions should put all
even numbers first, and then odd numbers.
Example
Input = {12, 34, 45, 9, 8, 90, 3}
Output = {12, 34, 8, 90, 45, 9, 3}
In the output, order of numbers can be changed, i.e., in the above example 34 can come before 12 and 3 can
come before 9.
The problem is very similar to our old post Segregate 0s and 1s in an array, and both of these problems are
variation of famous Dutch national flag problem.
Algorithm: segregateEvenOdd()
1) Initialize two index variables left and right:
left = 0, right = size -1
2) Keep incrementing left index until we see an odd number.
3) Keep decrementing right index until we see an even number.
4) If lef
Implementation:
#include<stdio.h>
115
right--;
/* UTILITY FUNCTIONS */
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
segregateEvenOdd(arr, arr_size);
getchar();
return 0;
}
Source
http://www.geeksforgeeks.org/segregate-even-and-odd-numbers/
Category: Arrays
116
Chapter 28
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.
Method 1 (Basic)
Use two loops. In the outer loop, pick elements one by one and count the number of occurrences of the
picked element in the inner loop.
This method doesn’t use the other useful data provided in questions like range of numbers is between 1 to
n and there are only two repeating elements.
#include<stdio.h>
#include<stdlib.h>
void printRepeating(int arr[], int size)
{
int i, j;
printf(" Repeating elements are ");
for(i = 0; i < size; i++)
for(j = i+1; j < size; j++)
if(arr[i] == arr[j])
printf(" %d ", arr[i]);
}
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printRepeating(arr, arr_size);
getchar();
return 0;
}
117
Time Complexity: O(n*n)
Auxiliary Space: O(1)
Method 2 (Use Count array)
Traverse the array once. While traversing, keep track of count of all elements in the array using a temp array
count[] of size n, when you see an element whose count is already set, print it as duplicate.
This method uses the range given in the question to restrict the size of count[], but doesn’t use the data that
there are only two repeating elements.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printRepeating(arr, arr_size);
getchar();
return 0;
}
118
X + Y = 21 – 15 = 6
XY = 960/5! = 8
X – Y = sqrt((X+Y)ˆ2 – 4*XY) = sqrt(4) = 2
Using below two equations, we easily get X = (6 + 2)/2 and Y = (6-2)/2
X+Y=6
X–Y=2
Thanks to geek4u for suggesting this method. As pointed by Beginer, there can be addition and multiplication
overflow problem with this approach.
The methods 3 and 4 use all useful information given in the question
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
S = S - n*(n+1)/2; /* S is x + y now */
P = P/fact(n); /* P is x*y now */
x = (D + S)/2;
y = (S - D)/2;
int fact(int n)
{
return (n == 0)? 1 : n*fact(n-1);
119
}
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printRepeating(arr, arr_size);
getchar();
return 0;
}
120
x = x ^ i; /*XOR of first set in arr[] and {1, 2, ...n }*/
else
y = y ^ i; /*XOR of second set in arr[] and {1, 2, ...n } */
}
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printRepeating(arr, arr_size);
getchar();
return 0;
}
i=1;
Check sign of A[abs(A[1])] which is A[1]. A[1] is negative, so A[1] is a repetition.
i=2;
Check sign of A[abs(A[2])] which is A[2]. A[2] is positive, so make it negative. '
Array now becomes {1, -1, -2, 3, 2}
i=3;
Check sign of A[abs(A[3])] which is A[3]. A[3] is positive, so make it negative.
Array now becomes {1, -1, -2, -3, 2}
i=4;
Check sign of A[abs(A[4])] which is A[2]. A[2] is negative, so A[4] is a repetition.
121
Note that this method modifies the original array and may not be a recommended method if we are not
allowed to modify the array.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[] = {1, 3, 2, 2, 1};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printRepeating(arr, arr_size);
getchar();
return 0;
}
The problem can be solved in linear time using other method also, please see this and thiscomments
Please write comments if you find the above codes/algorithms incorrect, or find better ways to solve the
same problem.
Source
http://www.geeksforgeeks.org/find-the-two-repeating-elements-in-a-given-array/
Category: Arrays
Post navigation
← Segregate Even and Odd numbers What is evaluation order of function parameters in C? →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
122
Chapter 29
Given an array A[] consisting 0s, 1s and 2s, write a function that sorts A[]. The functions should put all 0s
first, then all 1s and all 2s in last.
Example
Input = {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1};
Output = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2}
The problem is similar to our old post Segregate 0s and 1s in an array, and both of these problems are
variation of famous Dutch national flag problem.
The problem was posed with three colours, here ‘0�, ‘1� and ‘2�. The array is divided into four sections:
1. Lo := 1; Mid := 1; Hi := N;
2. while Mid <= Hi do
(a) Invariant: a[1..Lo-1]=0 and a[Lo..Mid-1]=1 and a[Hi+1..N]=2; a[Mid..Hi] are unknown.
(b) case a[Mid] in
• 0: swap a[Lo] and a[Mid]; Lo++; Mid++
• 1: Mid++
• 2: swap a[Mid] and a[Hi]; Hi–
123
Case (1) a[Mid] is white, Mid++
/* UTILITY FUNCTIONS */
void swap(int *a, int *b)
{
int temp = *a;
124
*a = *b;
*b = temp;
}
sort012(arr, arr_size);
getchar();
return 0;
}
Source
http://www.geeksforgeeks.org/sort-an-array-of-0s-1s-and-2s/
125
Chapter 30
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.
Solution:
1) Find the candidate unsorted subarray
a) Scan from left to right and find the first element which is greater than the next element. Let s be the
index of such an element. In the above example 1, s is 3 (index of 30).
b) Scan from right to left and find the first element (first in right to left order) which is smaller than the
next element (next in right to left order). Let e be the index of such an element. In the above example 1, e
is 7 (index of 31).
2) Check whether sorting the candidate unsorted subarray makes the complete array sorted
or not. If not, then include more elements in the subarray.
a) Find the minimum and maximum values in arr[s..e]. Let minimum and maximum values be min and max.
min and max for [30, 25, 40, 32, 31] are 25 and 40 respectively.
b) Find the first element (if there is any) in arr[0..s-1] which is greater than min, change s to index of this
element. There is no such element in above example 1.
c) Find the last element (if there is any) in arr[e+1..n-1] which is smaller than max, change e to index of
this element. In the above example 1, e is changed to 8 (index of 35)
3) Print s and e.
Implementation:
126
#include<stdio.h>
127
// step 3 of above algo
printf(" The unsorted subarray which makes the given array "
" sorted lies between the indees %d and %d", s, e);
return;
}
int main()
{
int arr[] = {10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printUnsorted(arr, arr_size);
getchar();
return 0;
}
Source
http://www.geeksforgeeks.org/minimum-length-unsorted-subarray-sorting-which-makes-the-complete-array-sorted/
128
Chapter 31
Given an array of n elements which contains elements from 0 to n-1, with any of these numbers appearing
any number of times. Find these repeating numbers in O(n) and using only constant memory space.
For example, let n be 7 and array be {1, 2, 3, 1, 3, 6, 6}, the answer should be 1, 3 and 6.
This problem is an extended version of following problem.
Find the two repeating elements in a given array
Method 1 and Method 2 of the above link are not applicable as the question says O(n) time complexity and
O(1) constant space. Also, Method 3 and Method 4 cannot be applied here because there can be more than
2 repeating elements in this problem. Method 5 can be extended to work for this problem. Below is the
solution that is similar to the Method 5.
Algorithm:
Implementation:
#include <stdio.h>
#include <stdlib.h>
129
if (arr[abs(arr[i])] >= 0)
arr[abs(arr[i])] = -arr[abs(arr[i])];
else
printf(" %d ", abs(arr[i]));
}
}
int main()
{
int arr[] = {1, 2, 3, 1, 3, 6, 6};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printRepeating(arr, arr_size);
getchar();
return 0;
}
Note: The above program doesn’t handle 0 case (If 0 is present in array). The program can be easily modified
to handle that also. It is not handled to keep the code simple.
Output:
The repeating elements are:
136
Time Complexity: O(n)
Auxiliary Space: O(1)
Please write comments if you find the above codes/algorithms incorrect, or find better ways to solve the
same problem.
Source
http://www.geeksforgeeks.org/find-duplicates-in-on-time-and-constant-extra-space/
Category: Arrays
130
Chapter 32
Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum
of elements at higher indexes. For example, in an arrya A:
A[0] = -7, A[1] = 1, A[2] = 5, A[3] = 2, A[4] = -4, A[5] = 3, A[6]=0
3 is an equilibrium index, because:
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]
6 is also an equilibrium index, because sum of zero elements is zero, i.e., A[0] + A[1] + A[2] + A[3] + A[4]
+ A[5]=0
7 is not an equilibrium index, because it is not a valid index of array A.
Write a function int equilibrium(int[] arr, int n); that given a sequence arr[] of size n, returns an equilibrium
index (if any) or -1 if no equilibrium indexes exist.
Method 1 (Simple but inefficient)
Use two loops. Outer loop iterates through all the element and inner loop finds out whether the current
index picked by the outer loop is equilibrium index or not. Time complexity of this solution is O(nˆ2).
#include <stdio.h>
131
rightsum += arr[j];
int main()
{
int arr[] = {-7, 1, 5, 2, -4, 3, 0};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printf("%d\n", equilibrium(arr, arr_size));
getchar();
return 0;
}
1) Initialize leftsum as 0
2) Get the total sum of the array as sum
3) Iterate through the array and for each index i, do following.
a) Update sum to get the right sum.
sum = sum - arr[i]
// sum is now right sum
b) If leftsum is equal to sum, then return current index.
c) leftsum = leftsum + arr[i] // update leftsum for next iteration.
4) return -1 // If we come out of loop without returning then
// there is no equilibrium index
#include <stdio.h>
132
/* Find sum of the whole array */
for (i = 0; i < n; ++i)
sum += arr[i];
if(leftsum == sum)
return i;
leftsum += arr[i];
}
int main()
{
int arr[] = {-7, 1, 5, 2, -4, 3, 0};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printf("First equilibrium index is %d\n", equilibrium(arr, arr_size));
getchar();
return 0;
}
Source
http://www.geeksforgeeks.org/equilibrium-index-of-an-array/
133
Chapter 33
Source
http://www.geeksforgeeks.org/linked-list-vs-array/
134
Chapter 34
Minimizing the number of writes is useful when making writes to some huge data set is very expensive, such
as with EEPROMs or Flash memory, where each write reduces the lifespan of the memory.
Among the sorting algorithms that we generally study in our data structure and algorithm courses, Selection
Sort makes least number of writes (it makes O(n) swaps). But, Cycle Sort almost always makes less number
of writes compared to Selection Sort. In Cycle Sort, each value is either written zero times, if it’s already
in its correct position, or written one time to its correct position. This matches the minimal number of
overwrites required for a completed in-place sort.
Sources:
http://en.wikipedia.org/wiki/Cycle_sort
http://en.wikipedia.org/wiki/Selection_sort
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Source
http://www.geeksforgeeks.org/which-sorting-algorithm-makes-minimum-number-of-writes/
Category: Arrays
135
Chapter 35
Given an image, how will you turn it by 90 degrees? A vague question. Minimize the browser and try your
solution before going further.
An image can be treated as 2D matrix which can be stored in a buffer. We are provided with matrix
dimensions and it’s base address. How can we turn it?
For example see the below picture,
* * * ^ * * *
* * * | * * *
* * * | * * *
* * * | * * *
* * * *
* * * *
* * * *
-- - - >
* * * *
* * * *
* * * *
The idea is simple. Transform each row of source matrix into required column of final image. We will use
an auxiliary buffer to transform the image.
From the above picture, we can observe that
In pictorial form, we can represent the above transformations of an (m x n) matrix into (n x m) matrix,
136
Transformations
If you have not attempted, atleast try your pseudo code now.
It will be easy to write our pseudo code. In C/C++ we will usually traverse matrix on row major order.
Each row is transformed into different column of final image. We need to construct columns of final image.
See the following algorithm (transformation)
Note that there are various ways to implement the algorithm based on traversal of matrix, row major or
column major order. We have two matrices and two ways (row and column major) to traverse each matrix.
Hence, there can atleast be 4 different ways of transformation of source matrix into final matrix.
Code:
#include <stdio.h>
#include <stdlib.h>
void displayMatrix(unsigned int const *p, unsigned int row, unsigned int col);
void rotate(unsigned int *pS, unsigned int *pD, unsigned int row, unsigned int col);
int main()
{
// declarations
unsigned int image[][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
unsigned int *pSource;
unsigned int *pDestination;
unsigned int m, n;
137
rotate(pSource, pDestination, m, n);
displayMatrix(pDestination, n, m);
free(pDestination);
getchar();
return 0;
}
printf("\n\n");
}
void rotate(unsigned int *pS, unsigned int *pD, unsigned int row, unsigned int col)
{
unsigned int r, c;
for(r = 0; r < row; r++)
{
for(c = 0; c < col; c++)
{
*(pD + c * row + (row - r - 1)) = *(pS + r * col + c);
}
}
}
Compiled by Venki. Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above.
Source
http://www.geeksforgeeks.org/turn-an-image-by-90-degree/
138
Chapter 36
Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element for an
element x is the first greater element on the right side of x in array. Elements for which no greater element
exist, consider next greater element as -1.
Examples:
a) For any array, rightmost element always has next greater element as -1.
b) For an array which is sorted in decreasing order, all elements have next greater element as -1.
c) For the input array [4, 5, 2, 25}, the next greater elements for each element are as follows.
Element NGE
4 --> 5
5 --> 25
2 --> 25
25 --> -1
d) For the input array [13, 7, 6, 12}, the next greater elements for each element are as follows.
Element NGE
13 --> -1
7 --> 12
6 --> 12
12 --> -1
Method 1 (Simple)
Use two loops: The outer loop picks all the elements one by one. The inner loop looks for the first greater
element for the element picked by outer loop. If a greater element is found then that element is printed as
next, otherwise -1 is printed.
Thanks to Sachinfor providing following code.
C/C++
139
// in a given array
#include<stdio.h>
int main()
{
int arr[]= {11, 13, 21, 3};
int n = sizeof(arr)/sizeof(arr[0]);
printNGE(arr, n);
getchar();
return 0;
}
Python
# Function to print element and NGE pair for all elements of list
def printNGE(arr):
next = -1
for j in range(i+1, len(arr), 1):
if arr[i] < arr[j]:
next = arr[j]
break
140
Output:
11 -- 13
13 -- 21
21 -- -1
3 -- -1
Time Complexity: O(nˆ2). The worst case occurs when all elements are sorted in decreasing order.
Method 2 (Using Stack)
Thanks to pchildfor suggesting following approach.
1) Push the first element to stack.
2) Pick rest of the elements one by one and follow following steps in loop.
….a) Mark the current element as next.
….b) If stack is not empty, then pop an element from stack and compare it with next.
….c) If next is greater than the popped element, then next is the next greater element for the popped element.
….d) Keep popping from the stack while the popped element is smaller than next. next becomes the next
greater element for all such popped elements
….g) If next is smaller than the popped element, then push the popped element back.
3) After the loop in step 2 is over, pop all the elements from stack and print -1 as next element for them.
C
// stack structure
struct stack
{
int top;
int items[STACKSIZE];
};
141
}
}
if (isEmpty(&s) == false)
{
// if stack is not empty, then pop an element from stack
element = pop(&s);
142
printf("\n %d --> %d", element, next);
if(isEmpty(&s) == true)
break;
element = pop(&s);
}
Python
def isEmpty(stack):
return len(stack) == 0
143
def pop(stack):
if isEmpty(stack):
print("Error : stack underflow")
else:
return stack.pop()
if isEmpty(s) == False:
144
# Driver program to test above functions
arr = [11, 13, 21, 3]
printNGE(arr)
Output:
11 -- 13
13 -- 21
3 -- -1
21 -- -1
Time Complexity: O(n). The worst case occurs when all elements are sorted in decreasing order. If elements
are sorted in decreasing order, then every element is processed at most 4 times.
a) Initially pushed to the stack.
b) Popped from the stack when next element is being processed.
c) Pushed back to the stack because next element is smaller.
d) Popped from the stack in step 3 of algo.
Source:
http://geeksforgeeks.org/forum/topic/next-greater-element#post-60
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same
problem.
Category: Arrays
Post navigation
← Reverse alternate K nodes in a Singly Linked List Practice Questions for Recursion | Set 5 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
Source
http://www.geeksforgeeks.org/next-greater-element/
145
Chapter 37
Given an unsorted array of numbers, write a function that returns true if array consists of consecutive
numbers.
Examples:
a) If array is {5, 2, 3, 1, 4}, then the function should return true because the array has consecutive numbers
from 1 to 5.
b) If array is {83, 78, 80, 81, 79, 82}, then the function should return true because the array has consecutive
numbers from 78 to 83.
c) If the array is {34, 23, 52, 12, 3 }, then the function should return false because the elements are not
consecutive.
d) If the array is {7, 6, 5, 5, 3, 4}, then the function should return false because 5 and 5 are not consecutive.
Method 1 (Use Sorting)
1) Sort all the elements.
2) Do a linear scan of the sorted array. If the difference between current element and next element is anything
other than 1, then return false. If all differences are 1, then return true.
Time Complexity: O(nLogn)
Method 2 (Use visited array)
The idea is to check for following two conditions. If following two conditions are true, then return true.
1) max – min + 1 = n where max is the maximum element in array, min is minimum element in array and
n is the number of elements in array.
2) All elements are distinct.
To check if all elements are distinct, we can create a visited[] array of size n. We can map the ith element of
input array arr[] to visited array by using arr[i] – min as index in visited[].
#include<stdio.h>
#include<stdlib.h>
146
If elements are consecutive, then returns true, else returns
false */
bool areConsecutive(int arr[], int n)
{
if ( n < 1 )
return false;
/* UTILITY FUNCTIONS */
int getMin(int arr[], int n)
{
int min = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] < min)
min = arr[i];
return min;
}
147
return max;
}
148
Source
http://www.geeksforgeeks.org/check-if-array-elements-are-consecutive/
149
Chapter 38
Given a sorted array of n integers where each integer is in the range from 0 to m-1 and m > n. Find the
smallest number that is missing from the array.
Examples
Input: {0, 1, 2, 6, 9}, n = 5, m = 10
Output: 3
Input: {4, 5, 10, 11}, n = 4, m = 12
Output: 0
Input: {0, 1, 2, 3}, n = 4, m = 5
Output: 4
Input: {0, 1, 2, 3, 4, 5, 6, 7, 10}, n = 9, m = 11
Output: 8
Thanks to Ravichandrafor suggesting following two methods.
Method 1 (Use Binary Search)
For i = 0 to m-1, do binary search for i in the array. If i is not present in the array then return i.
Time Complexity: O(m log n)
Method 2 (Linear Search)
If arr[0] is not 0, return 0. Otherwise traverse the input array starting from index 1, and for each pair of
elements a[i] and a[i+1], find the difference between them. if the difference is greater than 1 then a[i]+1 is
the missing number.
Time Complexity: O(n)
Method 3 (Use Modified Binary Search)
Thanks to yasein and Jams for suggesting this method.
In the standard Binary Search process, the element to be searched is compared with the middle element and
on the basis of comparison result, we decide whether to search is over or to go to left half or right half.
In this method, we modify the standard Binary Search algorithm to compare the middle element with its
index and make decision on the basis of this comparison.
…1) If the first element is not same as its index then return first index
…2) Else get the middle index say mid
…………a) If arr[mid] greater than mid then the required element lies in left half.
…………b) Else the required element lies in right half.
150
#include<stdio.h>
if (start != array[start])
return start;
Note: This method doesn’t work if there are duplicate elements in the array.
Time Complexity: O(Logn)
Source: http://geeksforgeeks.org/forum/topic/commvault-interview-question-for-software-engineerdeveloper-2-5-years-abou
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same
problem.
Source
http://www.geeksforgeeks.org/find-the-first-missing-number/
151
Chapter 39
Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected
time complexity is O(Logn)
Examples:
152
/* get the index of first occurrence of x */
i = first(arr, 0, n-1, x, n);
/* return count */
return j-i+1;
}
153
int arr[] = {1, 2, 2, 3, 3, 3, 3};
int x = 3; // Element to be counted in arr[]
int n = sizeof(arr)/sizeof(arr[0]);
int c = count(arr, x, n);
printf(" %d occurs %d times ", x, c);
getchar();
return 0;
}
Source
http://www.geeksforgeeks.org/count-number-of-occurrences-in-a-sorted-array/
Category: Arrays Tags: Divide and Conquer
Post navigation
← Output of C Program | Set 20 Check if a given Binary Tree is SumTree →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
154
Chapter 40
Interpolation search works better than Binary Search for a sorted and uniformly distributed array.
On average the interpolation search makes about log(log(n)) comparisons (if the elements are uniformly
distributed), where n is the number of elements to be searched. In the worst case (for instance where the
numerical values of the keys increase exponentially) it can make up to O(n) comparisons.
Sources:
http://en.wikipedia.org/wiki/Interpolation_search
Source
http://www.geeksforgeeks.org/g-fact-84/
Category: Arrays
155
Chapter 41
Given an array arr[], find the maximum j – i such that arr[j] > arr[i].
Examples:
Input: {1, 2, 3, 4, 5, 6}
Output: 5 (j = 5, i = 0)
Input: {6, 5, 4, 3, 2, 1}
Output: -1
#include <stdio.h>
/* For a given array arr[], returns the maximum j – i such that
arr[j] > arr[i] */
int maxIndexDiff(int arr[], int n)
{
int maxDiff = -1;
int i, j;
156
maxDiff = j - i;
}
}
return maxDiff;
}
int main()
{
int arr[] = {9, 2, 3, 4, 5, 6, 7, 8, 18, 0};
int n = sizeof(arr)/sizeof(arr[0]);
int maxDiff = maxIndexDiff(arr, n);
printf("\n %d", maxDiff);
getchar();
return 0;
}
#include <stdio.h>
157
int *RMax = (int *)malloc(sizeof(int)*n);
return maxDiff;
}
Source
http://www.geeksforgeeks.org/given-an-array-arr-find-the-maximum-j-i-such-that-arrj-arri/
Category: Arrays
158
Post navigation
← Searching for Patterns | Set 3 (Rabin-Karp Algorithm) XOR Linked List – A Memory Efficient Doubly
Linked List | Set 1 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
159
Chapter 42
Given an array and an integer k, find the maximum for each and every contiguous subarray of size k.
Examples:
Input :
arr[] = {1, 2, 3, 1, 4, 5, 2, 3, 6}
k=3
Output :
3345556
Input :
arr[] = {8, 5, 10, 7, 9, 4, 15, 12, 90, 13}
k=4
Output :
10 10 10 15 15 90 90
Method 1 (Simple)
Run two loops. In the outer loop, take all subarrays of size k. In the inner loop, get the maximum of the
current subarray.
#include<stdio.h>
160
}
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr)/sizeof(arr[0]);
int k = 3;
printKMax(arr, n, k);
return 0;
}
Time Complexity: The outer loop runs n-k+1 times and the inner loop runs k times for every iteration of
outer loop. So time complexity is O((n-k+1)*k) which can also be written as O(nk).
Method 2 (Use Self-Balancing BST)
1) Pick first k elements and create a Self-Balancing Binary Search Tree (BST) of size k.
2) Run a loop for i = 0 to n – k
…..a) Get the maximum element from the BST, and print it.
…..b) Search for arr[i] in the BST and delete it from the BST.
…..c) Insert arr[i+k] into the BST.
Time Complexity: Time Complexity of step 1 is O(kLogk). Time Complexity of steps 2(a), 2(b) and 2(c)
is O(Logk). Since steps 2(a), 2(b) and 2(c) are in a loop that runs n-k+1 times, time complexity of the
complete algorithm is O(kLogk + (n-k+1)*Logk) which can also be written as O(nLogk).
Method 3 (A O(n) method: use Dequeue)
We create a Dequeue, Qi of capacity k, that stores only useful elements of current window of k elements.
An element is useful if it is in current window and is greater than all other elements on left side of it in
current window. We process all array elements one by one and maintain Qi to contain useful elements of
current window and these useful elements are maintained in sorted order. The element at front of the Qi is
the largest and element at rear of Qi is the smallest of current window. Thanks to Aashishfor suggesting
this method.
Following is C++ implementation of this method.
#include <iostream>
#include <deque>
// A Dequeue (Double ended queue) based method for printing maixmum element of
// all subarrays of size k
void printKMax(int arr[], int n, int k)
{
// Create a Double Ended Queue, Qi that will store indexes of array elements
// The queue will store indexes of useful elements in every window and it will
// maintain decreasing order of values from front to rear in Qi, i.e.,
// arr[Qi.front[]] to arr[Qi.rear()] are sorted in decreasing order
std::deque<int> Qi(k);
161
{
// For very element, the previous smaller elements are useless so
// remove them from Qi
while ( (!Qi.empty()) && arr[i] >= arr[Qi.back()])
Qi.pop_back(); // Remove from rear
Output:
78 90 90 90 89
Time Complexity: O(n). It seems more than O(n) at first look. If we take a closer look, we can observe that
every element of array is added and removed at most once. So there are total 2n operations.
Auxiliary Space: O(k)
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same
problem.
162
Source
http://www.geeksforgeeks.org/maximum-of-all-subarrays-of-size-k/
Category: Arrays Tags: Queue, Stack-Queue, StackAndQueue
Post navigation
← XOR Linked List – A Memory Efficient Doubly Linked List | Set 1 Time Complexity of building a heap
→
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
163
Chapter 43
Given two arrays: arr1[0..m-1] and arr2[0..n-1]. Find whether arr2[] is a subset of arr1[] or not. Both the
arrays are not in sorted order. It may be assumed that elements in both array are distinct.
Examples:
Input: arr1[] = {11, 1, 13, 21, 3, 7}, arr2[] = {11, 3, 7, 1}
Output: arr2[] is a subset of arr1[]
Input: arr1[] = {1, 2, 3, 4, 5, 6}, arr2[] = {1, 2, 4}
Output: arr2[] is a subset of arr1[]
Input: arr1[] = {10, 5, 2, 23, 19}, arr2[] = {19, 5, 3}
Output: arr2[] is not a subset of arr1[]
Method 1 (Simple)
Use two loops: The outer loop picks all the elements of arr2[] one by one. The inner loop linearly searches
for the element picked by outer loop. If all elements are found then return 1, else return 0.
#include<stdio.h>
164
}
int main()
{
int arr1[] = {11, 1, 13, 21, 3, 7};
int arr2[] = {11, 3, 7, 1};
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
getchar();
return 0;
}
#include<stdio.h>
/* Fucntion prototypes */
void quickSort(int *arr, int si, int ei);
int binarySearch(int arr[], int low, int high, int x);
quickSort(arr1, 0, m-1);
for (i=0; i<n; i++)
{
if (binarySearch(arr1, 0, m-1, arr2[i]) == -1)
return 0;
}
165
/* If we reach here then all elements of arr2[]
are present in arr1[] */
return 1;
}
166
/* Implementation of Quick Sort
A[] --> Array to be sorted
si --> Starting index
ei --> Ending index
*/
void quickSort(int A[], int si, int ei)
{
int pi; /* Partitioning index */
if(si < ei)
{
pi = partition(A, si, ei);
quickSort(A, si, pi - 1);
quickSort(A, pi + 1, ei);
}
}
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
getchar();
return 0;
}
Time Complexity: O(mLogm + nLogm). Please note that this will be the complexity if an mLogm algorithm
is used for sorting which is not the case in above code. In above code Quick Sort is sued and worst case time
complexity of Quick Sort is O(mˆ2)
Method 3 (Use Sorting and Merging )
1) Sort both arrays: arr1[] and arr2[] O(mLogm + nLogn)
2) Use Merge type of process to see if all elements of sorted arr2[] are present in sorted arr1[].
Thanks to Parthsarthifor suggesting this method.
if(m < n)
return 0;
quickSort(arr1, 0, m-1);
167
quickSort(arr2, 0, n-1);
while( i < n && j < m )
{
if( arr1[j] <arr2[i] )
j++;
else if( arr1[j] == arr2[i] )
{
j++;
i++;
}
else if( arr1[j] > arr2[i] )
return 0;
}
if( i < n )
return 0;
else
return 1;
}
Time Complexity: O(mLogm + nLogn) which is better than method 2. Please note that this will be the
complexity if an nLogn algorithm is used for sorting both arrays which is not the case in above code. In
above code Quick Sort is sued and worst case time complexity of Quick Sort is O(nˆ2)
Method 4 (Use Hashing)
1) Create a Hash Table for all the elements of arr1[].
2) Traverse arr2[] and search for each element of arr2[] in the Hash Table. If element is not found then return
0.
3) If all elements are found then return 1.
Note that method 1, method 2 and method 4 don’t handle the cases when we have duplicates in arr2[]. For
example, {1, 4, 4, 2} is not a subset of {1, 4, 2}, but these methods will print it as a subset.
Source: http://geeksforgeeks.org/forum/topic/if-an-array-is-subset-of-another
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same
problem.
Source
http://www.geeksforgeeks.org/find-whether-an-array-is-subset-of-another-array-set-1/
Category: Arrays
Post navigation
← A Boolean Array Puzzle Dynamic Programming | Set 4 (Longest Common Subsequence) →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
168
Chapter 44
Given an unsorted array arr[] and two numbers x and y, find the minimum distance between x and y in arr[].
The array might also contain duplicates. You may assume that both x and y are different and present in
arr[].
Examples:
Input: arr[] = {1, 2}, x = 1, y = 2
Output: Minimum distance between 1 and 2 is 1.
Input: arr[] = {3, 4, 5}, x = 3, y = 5
Output: Minimum distance between 3 and 5 is 2.
Input: arr[] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}, x = 3, y = 6
Output: Minimum distance between 3 and 6 is 4.
Input: arr[] = {2, 5, 3, 5, 4, 4, 2, 3}, x = 3, y = 2
Output: Minimum distance between 3 and 2 is 1.
Method 1 (Simple)
Use two loops: The outer loop picks all the elements of arr[] one by one. The inner loop picks all the elements
after the element picked by outer loop. If the elements picked by outer and inner loops have same values as
x or y then if needed update the minimum distance calculated so far.
#include <stdio.h>
#include <stdlib.h> // for abs()
#include <limits.h> // for INT_MAX
169
min_dist = abs(i-j);
}
}
}
return min_dist;
}
#include <stdio.h>
#include <limits.h> // For INT_MAX
170
// Traverse after the first occurence
for ( ; i < n; i++)
{
if (arr[i] == x || arr[i] == y)
{
// If the current element matches with any of the two then
// check if current element and prev element are different
// Also check if this value is smaller than minimm distance so far
if ( arr[prev] != arr[i] && (i - prev) < min_dist )
{
min_dist = i - prev;
prev = i;
}
else
prev = i;
}
}
return min_dist;
}
Source
http://www.geeksforgeeks.org/find-the-minimum-distance-between-two-numbers/
Category: Arrays
Post navigation
← A Time Complexity Question Spaghetti Stack →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
171
Chapter 45
Given an unsorted array of size n. Array elements are in range from 1 to n. One number from set {1, 2, …n}
is missing and one number occurs twice in array. Find these two numbers.
Examples:
arr[] = {3, 1, 3}
Output: 2, 3 // 2 is missing and 3 occurs twice
arr[] = {4, 3, 6, 2, 1, 1}
Output: 1, 5 // 5 is missing and 1 occurs twice
#include<stdio.h>
#include<stdlib.h>
172
void printTwoElements(int arr[], int size)
{
int i;
printf("\n The repeating element is");
xor1 = arr[0]^arr[1]^arr[2].....arr[n-1]
173
XOR the result with all numbers from 1 to n
xor1 = xor1^1^2^.....^n
In the result xor1, all elements would nullify each other except x and y. All the bits that are set in xor1 will
be set in either x or y. So if we take any set bit (We have chosen the rightmost set bit in code) of xor1 and
divide the elements of the array in two sets – one set of elements with same bit set and other set with same
bit not set. By doing so, we will get x in one set and y in another set. Now if we do XOR of all the elements
in first set, we will get x, and by doing same in other set we will get y.
#include <stdio.h>
#include <stdlib.h>
xor1 = arr[0];
174
}
Source
http://www.geeksforgeeks.org/find-a-repeating-and-a-missing-number/
175
Chapter 46
Given that integers are read from a data stream. Find median of elements read so for in efficient way. For
simplicity assume there are no duplicates. For example, let us consider the stream 5, 15, 1, 3 …
Making it clear, when the input size is odd, we take the middle element of sorted data. If the input size is
even, we pick average of middle two elements in sorted stream.
Note that output is effective median of integers read from the stream so far. Such an algorithm is called
online algorithm. Any algorithm that can guarantee output of i-elements after processing i-th element, is
said to be online algorithm. Let us discuss three solutions for the above problem.
Method 1: Insertion Sort
If we can sort the data as it appears, we can easily locate median element. Insertion Sort is one such online
algorithm that sorts the data appeared so far. At any instance of sorting, say after sorting i-th element, the
first i elements of array are sorted. The insertion sort doesn’t depend on future data to sort data input till
that point. In other words, insertion sort considers data sorted so far while inserting next element. This is
the key part of insertion sort that makes it an online algorithm.
However, insertion sort takes O(n2 ) time to sort n elements. Perhaps we can use binary search on insertion
sort to find location of next element in O(log n) time. Yet, we can’t do data movement in O(log n) time. No
matter how efficient the implementation is, it takes polynomial time in case of insertion sort.
Interested reader can try implementation of Method 1.
Method 2: Augmented self balanced binary search tree (AVL, RB, etc…)
At every node of BST, maintain number of elements in the subtree rooted at that node. We can use a node
as root of simple binary tree, whose left child is self balancing BST with elements less than root and right
child is self balancing BST with elements greater than root. The root element always holds effective median.
If left and right subtrees contain same number of elements, root node holds average of left and right subtree
root data. Otherwise, root contains same data as the root of subtree which is having more elements. After
processing an incoming element, the left and right subtrees (BST) are differed utmost by 1.
176
Self balancing BST is costly in managing balancing factor of BST. However, they provide sorted data which
we don’t need. We need median only. The next method make use of Heaps to trace median.
Method 3: Heaps
Similar to balancing BST in Method 2 above, we can use a max heap on left side to represent elements that
are less than effective median, and a min heap on right side to represent elements that are greater than
effective median.
After processing an incoming element, the number of elements in heaps differ utmost by 1 element. When
both heaps contain same number of elements, we pick average of heaps root data as effective median. When
the heaps are not balanced, we select effective median from the root of heap containing more elements.
Given below is implementation of above method. For algorithm to build these heaps, please read the high-
lighted code.
#include <iostream>
using namespace std;
// Heap capacity
#define MAX_HEAP_SIZE (128)
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
// exchange a and b
inline
void Exch(int &a, int &b)
{
int aux = a;
a = b;
b = aux;
}
// Signum function
// = 0 if a == b - heaps are balanced
// = -1 if a < b - left contains less elements than right
// = 1 if a > b - left contains more elements than right
int Signum(int a, int b)
177
{
if( a == b )
return 0;
return a < b ? -1 : 1;
}
// Heap implementation
// The functionality is embedded into
// Heap abstract class to avoid code duplication
class Heap
{
public:
// Initializes heap array and comparator required
// in heapification
Heap(int *b, bool (*c)(int, int)) : A(b), comp(c)
{
heapSize = -1;
}
protected:
int right(int i)
{
return 2 * (i + 1);
}
int parent(int i)
{
if( i <= 0 )
{
return -1;
}
178
return (i - 1)/2;
}
// Heap array
int *A;
// Comparator
bool (*comp)(int, int);
// Heap size
int heapSize;
return max;
}
// Heapification
// Note that, for the current median tracing problem
// we need to heapify only towards root, always
void heapify(int i)
{
int p = parent(i);
179
Exch(A[0], A[heapSize]);
heapSize--;
heapify(parent(heapSize+1));
}
return del;
}
return ret;
}
};
public:
MaxHeap() : Heap(new int[MAX_HEAP_SIZE], &Greater) { }
~MaxHeap() { }
180
// Wrapper to insert into Max Heap
bool Insert(int key)
{
return insertHelper(key);
}
};
public:
~MinHeap() { }
181
// Remore top element from left heap and
// insert into right heap
r.Insert(l.ExtractTop());
break;
case 0: // The left and right heaps contain same number of elements
break;
break;
182
}
return 0;
}
Time Complexity: If we omit the way how stream was read, complexity of median finding is O(N log
N), as we need to read the stream, and due to heap insertions/deletions.
At first glance the above code may look complex. If you read the code carefully, it is simple algorithm.
— Venki. Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above.
Source
http://www.geeksforgeeks.org/median-of-stream-of-integers-running-integers/
Category: Arrays
Post navigation
← Advanced C++ | Virtual Constructor Advanced C++ | Virtual Copy Constructor →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
183
Chapter 47
Given an array of n distinct integers sorted in ascending order, write a function that returns a Fixed Point
in the array, if there is any Fixed Point present in array, else returns -1. Fixed Point in an array is an index
i such that arr[i] is equal to i. Note that integers in array can be negative.
Examples:
Asked by rajk
Method 1 (Linear Search)
Linearly search for an index i such that arr[i] == i. Return the first such index found. Thanks to pmfor
suggesting this solution.
184
/* Driver program to check above functions */
int main()
{
int arr[] = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Fixed Point is %d", linearSearch(arr, n));
getchar();
return 0;
}
185
Source
http://www.geeksforgeeks.org/find-a-fixed-point-in-a-given-array/
Category: Arrays Tags: Divide and Conquer
Post navigation
← Trie | (Delete) Length of the longest substring without repeating characters →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
186
Chapter 48
Given an array A[0 … n-1] containing n positive integers, a subarray A[i … j] is bitonic if there is a k with
i = A[k + 1] >= .. A[j – 1] > = A[j]. Write a function that takes an array as argument and returns the
length of the maximum length bitonic subarray.
Expected time complexity of the solution is O(n)
Simple Examples
1) A[] = {12, 4, 78, 90, 45, 23}, the maximum length bitonic subarray is {4, 78, 90, 45, 23} which is of
length 5.
2) A[] = {20, 4, 1, 2, 3, 4, 2, 10}, the maximum length bitonic subarray is {1, 2, 3, 4, 2} which is of length
5.
Extreme Examples
1) A[] = {10}, the single element is bitnoic, so output is 1.
2) A[] = {10, 20, 30, 40}, the complete array itself is bitonic, so output is 4.
3) A[] = {40, 30, 20, 10}, the complete array itself is bitonic, so output is 4.
Solution
Let us consider the array {12, 4, 78, 90, 45, 23} to understand the soultion.
1) Construct an auxiliary array inc[] from left to right such that inc[i] contains length of the nondecreaing
subarray ending at arr[i].
For for A[] = {12, 4, 78, 90, 45, 23}, inc[] is {1, 1, 2, 3, 1, 1}
2) Construct another array dec[] from right to left such that dec[i] contains length of nonincreasing subarray
starting at arr[i].
For A[] = {12, 4, 78, 90, 45, 23}, dec[] is {2, 1, 1, 3, 2, 1}.
3) Once we have the inc[] and dec[] arrays, all we need to do is find the maximum value of (inc[i] + dec[i] –
1).
For {12, 4, 78, 90, 45, 23}, the max value of (inc[i] + dec[i] – 1) is 5 for i = 3.
#include<stdio.h>
#include<stdlib.h>
187
int max;
inc[0] = 1; // The length of increasing sequence ending at first index is 1
dec[n-1] = 1; // The length of increasing sequence starting at first index is 1
return max;
}
188
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Source
http://www.geeksforgeeks.org/maximum-length-bitonic-subarray/
189
Chapter 49
Given an array of integers which is initially increasing and then decreasing, find the maximum value in the
array.
Input: arr[] = {8, 10, 20, 80, 100, 200, 400, 500, 3, 2, 1}
Output: 500
#include <stdio.h>
190
if (arr[i] > max)
max = arr[i];
}
return max;
}
#include <stdio.h>
191
/* If arr[mid] is greater than the next element and smaller than the previous
element then maximum lies on left side of mid */
if (arr[mid] > arr[mid + 1] && arr[mid] < arr[mid - 1])
return findMaximum(arr, low, mid-1);
else // when arr[mid] is greater than arr[mid-1] and smaller than arr[mid+1]
return findMaximum(arr, mid + 1, high);
}
Source
http://www.geeksforgeeks.org/find-the-maximum-element-in-an-array-which-is-first-increasing-and-then-decreasing/
Category: Arrays Tags: Divide and Conquer
Post navigation
← Connect nodes at same level using constant extra space Sorted Linked List to Balanced BST →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
192
Chapter 50
Write a function to count number of smaller elements on right of each element in an array. Given an
unsorted array arr[] of distinct integers, construct another array countSmaller[] such that countSmaller[i]
contains count of smaller elements on right side of each element arr[i] in array.
Examples:
(Corner Cases)
Input: arr[] = {5, 4, 3, 2, 1}
Output: countSmaller[] = {4, 3, 2, 1, 0}
Method 1 (Simple)
Use two loops. The outer loop picks all elements from left to right. The inner loop iterates through all the
elements on right side of the picked element and updates countSmaller[].
193
}
}
printf("\n");
}
#include<stdio.h>
#include<stdlib.h>
194
int max(int a, int b);
/* Helper function that allocates a new node with the given key and
NULL left and right pointers. */
struct node* newNode(int key)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // new node is initially added at leaf
node->size = 1;
return(node);
}
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;
// Update sizes
y->size = size(y->left) + size(y->right) + 1;
195
x->size = size(x->left) + size(x->right) + 1;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right))+1;
y->height = max(height(y->left), height(y->right))+1;
// Update sizes
x->size = size(x->left) + size(x->right) + 1;
y->size = size(y->left) + size(y->right) + 1;
// Inserts a new key to the tree rotted with node. Also, updates *count
// to contain count of smaller elements for the new key
struct node* insert(struct node* node, int key, int *count)
{
/* 1. Perform the normal BST rotation */
if (node == NULL)
return(newNode(key));
196
/* 2. Update height and size of this ancestor node */
node->height = max(height(node->left), height(node->right)) + 1;
node->size = size(node->left) + size(node->right) + 1;
197
}
Output:
Source
http://www.geeksforgeeks.org/count-smaller-elements-on-right-side/
Category: Arrays
Post navigation
← Average of a stream of numbers Minimum number of jumps to reach end →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
198
Chapter 51
Given an array of integers where each element represents the max number of steps that can be made forward
from that element. Write a function to return the minimum number of jumps to reach the end of the array
(starting from the first element). If an element is 0, then cannot move through that element.
Example:
First element is 1, so can only go to 3. Second element is 3, so can make at most 3 steps eg to 5 or 8 or 9.
Method 1 (Naive Recursive Approach)
A naive approach is to start from the first element and recursively call for all the elements reachable from
first element. The minimum number of jumps to reach end from first can be calculated using minimum
number of jumps needed to reach end from the elements reachable from first.
minJumps(start, end) = Min ( minJumps(k, end) ) for all k reachable from start
#include <stdio.h>
#include <limits.h>
199
// get the minimum number of jumps needed to reach arr[h] from these
// reachable points.
int min = INT_MAX;
for (int i = l+1; i <= h && i <= l + arr[l]; i++)
{
int jumps = minJumps(arr, i, h);
if(jumps != INT_MAX && jumps + 1 < min)
min = jumps + 1;
}
return min;
}
If we trace the execution of this method, we can see that there will be overlapping subproblems. For example,
minJumps(3, 9) will be called two times as arr[3] is reachable from arr[1] and arr[2]. So this problem has
both properties (optimal substructure and overlapping subproblems) of Dynamic Programming.
Method 2 (Dynamic Programming)
In this method, we build a jumps[] array from left to right such that jumps[i] indicates the minimum number
of jumps needed to reach arr[i] from arr[0]. Finally, we return jumps[n-1].
#include <stdio.h>
#include <limits.h>
if (n == 0 || arr[0] == 0)
return INT_MAX;
jumps[0] = 0;
200
{
if (i <= j + arr[j] && jumps[j] != INT_MAX)
{
jumps[i] = min(jumps[i], jumps[j] + 1);
break;
}
}
}
return jumps[n-1];
}
Output:
int i, j;
201
// If we can direcly reach to the end point from here then
// jumps[i] is 1
else if (arr[i] >= n - i - 1)
jumps[i] = 1;
// Handle overflow
if (min != INT_MAX)
jumps[i] = min + 1;
else
jumps[i] = min; // or INT_MAX
}
}
return jumps[0];
}
Source
http://www.geeksforgeeks.org/minimum-number-of-jumps-to-reach-end-of-a-given-array/
Category: Arrays Tags: Dynamic Programming
Post navigation
← Count smaller elements on right side Populate Inorder Successor for all nodes →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
202
Chapter 52
Create a data structure twoStacks that represents two stacks. Implementation of twoStacks should use only
one array, i.e., both stacks should use the same array for storing elements. Following functions must be
supported by twoStacks.
push1(int x) –> pushes x to first stack
push2(int x) –> pushes x to second stack
pop1() –> pops an element from first stack and return the popped element
pop2() –> pops an element from second stack and return the popped element
Implementation of twoStack should be space efficient.
Method 1 (Divide the space in two halves)
A simple way to implement two stacks is to divide the array in two halves and assign the half half space to
two stacks, i.e., use arr[0] to arr[n/2] for stack1, and arr[n/2+1] to arr[n-1] for stack2 where arr[] is the array
to be used to implement two stacks and size of array be n.
The problem with this method is inefficient use of array space. A stack push operation may result in stack
overflow even if there is space available in arr[]. For example, say the array size is 6 and we push 3 elements
to stack1 and do not push anything to second stack2. When we push 4th element to stack1, there will be
overflow even if we have space for 3 more elements in array.
Method 2 (A space efficient implementation)
This method efficiently utilizes the available space. It doesn’t cause an overflow if there is space available
in arr[]. The idea is to start two stacks from two extreme corners of arr[]. stack1 starts from the leftmost
element, the first element in stack1 is pushed at index 0. The stack2 starts from the rightmost corner, the
first element in stack2 is pushed at index (n-1). Both stacks grow (or shrink) in opposite direction. To check
for overflow, all we need to check is for space between top elements of both stacks. This check is highlighted
in the below code.
C++
#include<iostream>
#include<stdlib.h>
class twoStacks
{
203
int *arr;
int size;
int top1, top2;
public:
twoStacks(int n) // constructor
{
size = n;
arr = new int[n];
top1 = -1;
top2 = size;
}
204
{
cout << "Stack UnderFlow";
exit(1);
}
}
Python
class twoStacks:
arr = [None]
size,top1,top2 = 0,0,0
205
def push1(self, x):
206
Output:
Source
http://www.geeksforgeeks.org/implement-two-stacks-in-an-array/
207
Chapter 53
Given an unsorted array of nonnegative integers, find a continous subarray which adds to a given number.
Examples:
There may be more than one subarrays with sum as the given sum. The following solutions print first such
subarray.
Source: Google Interview Question
Method 1 (Simple)
A simple solution is to consider all subarrays one by one and check the sum of every subarray. Following
program implements the simple solution. We run two loops: the outer loop picks a starting point i and the
inner loop tries all subarrays starting from i.
/* Returns true if the there is a subarray of arr[] with sum equal to 'sum'
otherwise returns false. Also, prints the result */
int subArraySum(int arr[], int n, int sum)
{
int curr_sum, i, j;
208
// try all subarrays starting with 'i'
for (j = i+1; j <= n; j++)
{
if (curr_sum == sum)
{
printf ("Sum found between indexes %d and %d", i, j-1);
return 1;
}
if (curr_sum > sum || j == n)
break;
curr_sum = curr_sum + arr[j];
}
}
Output:
/* Returns true if the there is a subarray of arr[] with sum equal to 'sum'
otherwise returns false. Also, prints the result */
int subArraySum(int arr[], int n, int sum)
{
209
/* Initialize curr_sum as value of first element
and starting point as 0 */
int curr_sum = arr[0], start = 0, i;
/* Add elements one by one to curr_sum and if the curr_sum exceeds the
sum, then remove starting element */
for (i = 1; i <= n; i++)
{
// If curr_sum exceeds the sum, then remove the starting elements
while (curr_sum > sum && start < i-1)
{
curr_sum = curr_sum - arr[start];
start++;
}
Output:
Time complexity of method 2 looks more than O(n), but if we take a closer look at the program, then we
can figure out the time complexity is O(n). We can prove it by counting the number of operations performed
on every element of arr[] in worst case. There are at most 2 operations performed on every element: (a) the
element is added to the curr_sum (b) the element is subtracted from curr_sum. So the upper bound on
number of operations is 2n which is O(n).
210
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Source
http://www.geeksforgeeks.org/find-subarray-with-given-sum/
Category: Arrays
Post navigation
← Dynamic Programming | Set 13 (Cutting a Rod) Dynamic Programming | Set 14 (Maximum Sum In-
creasing Subsequence) →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
211
Chapter 54
Given an array of n positive integers. Write a program to find the sum of maximum sum subsequence of the
given array such that the intgers in the subsequence are sorted in increasing order. For example, if input is
{1, 101, 2, 3, 100, 4, 5}, then output should be 106 (1 + 2 + 3 + 100), if the input array is {3, 4, 5, 10},
then output should be 22 (3 + 4 + 5 + 10) and if the input array is {10, 5, 4, 3}, then output should be 10
Solution
This problem is a variation of standard Longest Increasing Subsequence (LIS) problem. We need a slight
change in the Dynamic Programming solution of LIS problem. All we need to change is to use sum as a
criteria instead of length of increasing subsequence.
Following are C/C++ and Python implementations for Dynamic Programming solution of the problem.
C/C++
212
msis[i] = msis[j] + arr[i];
return max;
}
Python
return max
213
str(maxSumIS(arr, n)))
Output:
Source
http://www.geeksforgeeks.org/dynamic-programming-set-14-maximum-sum-increasing-subsequence/
214
Chapter 55
After few months of gap posting an algo. The current post is pending from long time, and many readers
(e.g. here, here, here may be few more, I am not keeping track of all) are posting requests for explanation of
the below problem.
Given an array of random numbers. Find longest monotonically increasing subsequence (LIS)
in the array. I know many of you might have read recursive and dynamic programming (DP)
solutions. There are few requests for O(N log N) algo in the forum posts.
For the time being, forget about recursive and DP solutions. Let us take small samples and extend the
solution to large instances. Even though it may look complex at first time, once if we understood the logic,
coding is simple.
Consider an input array A = {2, 5, 3}. I will extend the array during explanation.
By observation we know that the LIS is either {2, 3} or {2, 5}. Note that I am considering only strictly
increasing monotone sequences.
Let us add two more elements, say 7, 11 to the array. These elements will extend the existing sequences.
Now the increasing sequences are {2, 3, 7, 11} and {2, 5, 7, 11} for the input array {2, 5, 3, 7, 11}.
Further, we add one more element, say 8 to the array i.e. input array becomes {2, 5, 3, 7, 11, 8}. Note that
the latest element 8 is greater than smallest element of any active sequence (will discuss shortly about active
sequences). How can we extend the existing sequences with 8? First of all, can 8 be part of LIS? If yes, how?
If we want to add 8, it should come after 7 (by replacing 11).
Since the approach is offline (what we mean by offline?), we are not sure whether adding 8 will extend the
series or not. Assume there is 9 in the input array, say {2, 5, 3, 7, 11, 8, 7, 9 …}. We can replace 11 with 8,
as there is potentially best candidate (9) that can extend the new series {2, 3, 7, 8} or {2, 5, 7, 8}.
Our observation is, assume that the end element of largest sequence is E. We can add (replace) current
element A[i] to the existing sequence if there is an element A[j] (j > i) such that E < A[i] < A[j] or (E >
A[i] < A[j] – for replace). In the above example, E = 11, A[i] = 8 and A[j] = 9.
In case of our original array {2, 5, 3}, note that we face same situation when we are adding 3 to increasing
sequence {2, 5}. I just created two increasing sequences to make explanation simple. Instead of two sequences,
3 can replace 5 in the sequence {2, 5}.
I know it will be confusing, I will clear it shortly!
The question is, when will it be safe to add or replace an element in the existing sequence?
215
Let us consider another sample A = {2, 5, 3}. Say, the next element is 1. How can it extend the current
sequences {2,3} or {2, 5}. Obviously, it can’t extend either. Yet, there is a potential that the new smallest
element can be start of an LIS. To make it clear, consider the array is {2, 5, 3, 1, 2, 3, 4, 5, 6}. Making 1 as
new sequence will create new sequence which is largest.
The observation is, when we encounter new smallest element in the array, it can be a potential candidate to
start new sequence.
From the observations, we need to maintain lists of increasing sequences.
In general, we have set of active lists of varying length. We are adding an element A[i] to these lists. We
scan the lists (for end elements) in decreasing order of their length. We will verify the end elements of all
the lists to find a list whose end element is smaller than A[i] (floor value).
Our strategy determined by the following conditions,
1. If A[i] is smallest among all end candidates of active lists, we will start new active list of
length 1.
2. If A[i] is largest among all end candidates of active lists, we will clone the largest active
list, and extend it by A[i].
3. If A[i] is in between, we will find a list with largest end element that is smaller than A[i].
Clone and extend this list by A[i]. We will discard all other lists of same length as that of this
modified list.
Note that at any instance during our construction of active lists, the following condition is maintained.
“end element of smaller list is smaller than end elements of larger lists”.
It will be clear with an example, let us take example from wiki {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7,
15}.
216
0, 2, 10.
0, 4, 12. Discarded.
-----------------------------------------------------------------------------
A[6] = 6. Case 3. Clone, extend and discard.
0.
0, 2.
0, 2, 6.
0, 2, 10. Discarded.
-----------------------------------------------------------------------------
A[7] = 14. Case 2. Clone and extend.
0.
0, 2.
0, 2, 6.
0, 2, 6, 14.
-----------------------------------------------------------------------------
A[8] = 1. Case 3. Clone, extend and discard.
0.
0, 1.
0, 2. Discarded.
0, 2, 6.
0, 2, 6, 14.
-----------------------------------------------------------------------------
A[9] = 9. Case 3. Clone, extend and discard.
0.
0, 1.
0, 2, 6.
0, 2, 6, 9.
0, 2, 6, 14. Discarded.
-----------------------------------------------------------------------------
A[10] = 5. Case 3. Clone, extend and discard.
0.
0, 1.
0, 1, 5.
0, 2, 6. Discarded.
0, 2, 6, 9.
-----------------------------------------------------------------------------
A[11] = 13. Case 2. Clone and extend.
0.
0, 1.
0, 1, 5.
0, 2, 6, 9.
0, 2, 6, 9, 13.
-----------------------------------------------------------------------------
A[12] = 3. Case 3. Clone, extend and discard.
0.
0, 1.
0, 1, 3.
0, 1, 5. Discarded.
0, 2, 6, 9.
0, 2, 6, 9, 13.
-----------------------------------------------------------------------------
A[13] = 11. Case 3. Clone, extend and discard.
0.
0, 1.
217
0, 1, 3.
0, 2, 6, 9.
0, 2, 6, 9, 11.
0, 2, 6, 9, 13. Discarded.
-----------------------------------------------------------------------------
A[14] = 7. Case 3. Clone, extend and discard.
0.
0, 1.
0, 1, 3.
0, 1, 3, 7.
0, 2, 6, 9. Discarded.
0, 2, 6, 9, 11.
----------------------------------------------------------------------------
A[15] = 15. Case 2. Clone and extend.
0.
0, 1.
0, 1, 3.
0, 1, 3, 7.
0, 2, 6, 9, 11.
0, 2, 6, 9, 11, 15. <-- LIS List
----------------------------------------------------------------------------
It is required to understand above strategy to devise an algorithm. Also, ensure we have maintained the
condition, “end element of smaller list is smaller than end elements of larger lists“. Try with few other
examples, before reading further. It is important to understand what happening to end elements.
Algorithm:
Querying length of longest is fairly easy. Note that we are dealing with end elements only. We need not to
maintain all the lists. We can store the end elements in an array. Discarding operation can be simulated
with replacement, and extending a list is analogous to adding more elements to array.
We will use an auxiliary array to keep end elements. The maximum length of this array is that of input. In
the worst case the array divided into N lists of size one (note that it does’t lead to worst case complexity).
To discard an element, we will trace ceil value of A[i] in auxiliary array (again observe the end elements in
your rough work), and replace ceil value with A[i]. We extend a list by adding element to auxiliary array.
We also maintain a counter to keep track of auxiliary array length.
Bonus: You have learnt Patience Sorting technique partially :).
Here is a proverb, “Tell me and I will forget. Show me and I will remember. Involve me and I will understand.”
So, pick a suit from deck of cards. Find the longest increasing sub-sequence of cards from the shuffled suit.
#include <iostream>
#include <string.h>
#include <stdio.h>
218
using namespace std;
while( r - l > 1 ) {
m = l + (r - l)/2;
(A[m] >= key ? r : l) = m; // ternary expression returns an l-value
}
return r;
}
memset(tailTable, 0, sizeof(tailTable[0])*size);
tailTable[0] = A[0];
len = 1;
for( int i = 1; i < size; i++ ) {
if( A[i] < tailTable[0] )
// new smallest value
tailTable[0] = A[i];
else if( A[i] > tailTable[len-1] )
// A[i] wants to extend largest subsequence
tailTable[len++] = A[i];
else
// A[i] wants to be current end candidate of an existing subsequence
// It will replace ceil value in tailTable
tailTable[CeilIndex(tailTable, -1, len-1, A[i])] = A[i];
}
delete[] tailTable;
return len;
}
int main() {
int A[] = { 2, 5, 3, 7, 11, 8, 10, 13, 6 };
int n = ARRAY_SIZE(A);
return 0;
}
219
Complexity:
The loop runs for N elements. In the worst case (what is worst case input?), we may end up querying ceil
value using binary search (log i) for many A[i].
Therefore, T(n) < O( log N! ) = O(N log N). Analyse to ensure that the upper and lower bounds are also
O( N log N ). The complexity is THETA (N log N).
Exercises:
1. Design an algorithm to construct the longest increasing list. Also, model your solution using DAGs.
2. Design an algorithm to construct all monotonically increasing lists of equal longest size.
3. Is the above algorithm an online algorithm?
4. Design an algorithm to construct the longest decreasing list..
— Venki. Please write comments if you find anything incorrect, or you want to share more information about
the topic discussed above.
Source
http://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/
Category: Arrays
Post navigation
← Dynamic Programming | Set 14 (Maximum Sum Increasing Subsequence) Find a triplet that sum to a
given value →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
220
Chapter 56
Given an array and a value, find if there is a triplet in array whose sum is equal to the given value. If there
is such a triplet present in array, then print the triplet and return true. Else return false. For example, if
the given array is {12, 3, 4, 1, 6, 9} and given sum is 24, then there is a triplet (12, 3 and 9) present in array
whose sum is 24.
Method 1 (Naive)
A simple method is to generate all possible triplets and compare the sum of every triplet with the given
value. The following code implements this simple method using three nested loops.
# include <stdio.h>
221
// If we reach here, then no triplet was found
return false;
}
getchar();
return 0;
}
Output:
Triplet is 4, 10, 8
# include <stdio.h>
/* Now fix the first element one by one and find the
other two elements */
for (int i = 0; i < arr_size - 2; i++)
{
222
// from two corners of the array and move them toward each
// other
l = i + 1; // index of the first element in the remaining elements
r = arr_size-1; // index of the last element
while (l < r)
{
if( A[i] + A[l] + A[r] == sum)
{
printf("Triplet is %d, %d, %d", A[i], A[l], A[r]);
return true;
}
else if (A[i] + A[l] + A[r] < sum)
l++;
else // A[i] + A[l] + A[r] > sum
r--;
}
}
223
*/
void quickSort(int A[], int si, int ei)
{
int pi; /* Partitioning index */
if(si < ei)
{
pi = partition(A, si, ei);
quickSort(A, si, pi - 1);
quickSort(A, pi + 1, ei);
}
}
getchar();
return 0;
}
Output:
Triplet is 4, 8, 10
Source
http://www.geeksforgeeks.org/find-a-triplet-that-sum-to-a-given-value/
Category: Arrays
Post navigation
← Longest Monotonically Increasing Subsequence Size (N log N) Find the smallest positive number missing
from an unsorted array →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
224
Chapter 57
You are given an unsorted array with both positive and negative elements. You have to find the smallest
positive number missing from the array in O(n) time using constant extra space. You can modify the original
array.
Examples
225
code, segregate() function does this part.
2) Now we can ignore non-positive elements and consider only the part of array which contains all positive
elements. We traverse the array containing all positive numbers and to mark presence of an element x,
we change the sign of value at index x to negative. We traverse the array again and print the first index
which has positive value. In the following code, findMissingPositive() function does this part. Note that in
findMissingPositive, we have subtracted 1 from the values as indexes start from 0 in C.
/* Utility function that puts all non-positive (0 and negative) numbers on left
side of arr[] and return count of such numbers */
int segregate (int arr[], int size)
{
int j = 0, i;
for(i = 0; i < size; i++)
{
if (arr[i] <= 0)
{
swap(&arr[i], &arr[j]);
j++; // increment count of non-positive integers
}
}
return j;
}
226
if (arr[i] > 0)
return i+1; // 1 is added becuase indexes start from 0
return size+1;
}
int main()
{
int arr[] = {0, 10, 2, -10, -20};
int arr_size = sizeof(arr)/sizeof(arr[0]);
int missing = findMissing(arr, arr_size);
printf("The smallest positive missing number is %d ", missing);
getchar();
return 0;
}
Output:
Note that this method modifies the original array. We can change the sign of elements in the segregated
array to get the same set of elements back. But we still loose the order of elements. If we want to keep the
original array as it was, then we can create a copy of the array and run this approach on the temp array.
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Source
http://www.geeksforgeeks.org/find-the-smallest-positive-number-missing-from-an-unsorted-array/
227
Chapter 58
Given an unsorted array that contains even number of occurrences for all numbers except two numbers. Find
the two numbers which have odd occurrences in O(n) time complexity and O(1) extra space.
Examples:
A naive method to solve this problem is to run two nested loops. The outer loop picks an element and the
inner loop counts the number of occurrences of the picked element. If the count of occurrences is odd then
print the number. The time complexity of this method is O(nˆ2).
We can use sorting to get the odd occurring numbers in O(nLogn) time. First sort the numbers using an
O(nLogn) sorting algorithm like Merge Sort, Heap Sort.. etc. Once the array is sorted, all we need to do is
a linear scan of the array and print the odd occurring number.
We can also use hashing. Create an empty hash table which will have elements and their counts. Pick all
elements of input array one by one. Look for the picked element in hash table. If the element is found in
hash table, increment its count in table. If the element is not found, then enter it in hash table with count
as 1. After all elements are entered in hash table, scan the hash table and print elements with odd count.
This approach may take O(n) time on average, but it requires O(n) extra space.
A O(n) time and O(1) extra space solution:
The idea is similar to method 2 of thispost. Let the two odd occurring numbers be x and y. We use bitwise
XOR to get x and y. The first step is to do XOR of all elements present in array. XOR of all elements gives
us XOR of x and y because of the following properties of XOR operation.
1) XOR of any number n with itself gives us 0, i.e., n ˆ n = 0
2) XOR of any number n with 0 gives us n, i.e., n ˆ 0 = n
3) XOR is cumulative and associative.
228
So we have XOR of x and y after the first step. Let the value of XOR be xor2. Every set bit in xor2 indicates
that the corresponding bits in x and y have values different from each other. For example, if x = 6 (0110)
and y is 15 (1111), then xor2 will be (1001), the two set bits in xor2 indicate that the corresponding bits in
x and y are different. In the second step, we pick a set bit of xor2 and divide array elements in two groups.
Both x and y will go to different groups. In the following code, the rightmost set bit of xor2 is picked as it
is easy to get rightmost set bit of a number. If we do XOR of all those elements of array which have the
corresponding bit set (or 1), then we get the first odd number. And if we do XOR of all those elements
which have the corresponding bit 0, then we get the other odd occurring number. This step works because
of the same properties of XOR. All the occurrences of a number will go in same set. XOR of all occurrences
of a number which occur even number number of times will result in 0 in its set. And the xor of a set will
be one of the odd occurring elements.
/* Get the xor of all elements in arr[]. The xor will basically
be xor of two odd occurring elements */
for(i = 1; i < size; i++)
xor2 = xor2 ^ arr[i];
/* Get one set bit in the xor2. We get rightmost set bit
in the following line as it is easy to get */
set_bit_no = xor2 & ~(xor2-1);
229
/* Driver program to test above function */
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 3, 1};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printTwoOdd(arr, arr_size);
getchar();
return 0;
}
Output:
Source
http://www.geeksforgeeks.org/find-the-two-numbers-with-odd-occurences-in-an-unsorted-array/
Category: Arrays
Post navigation
← Find the smallest positive number missing from an unsorted array Pattern Searching | Set 7 (Boyer Moore
Algorithm – Bad Character Heuristic) →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
230
Chapter 59
231
• If A doesn’t know B, then B can’t be celebrity. Discard B, and A may be celebrity.
• Repeat above two steps till we left with only one person.
• Ensure the remained person is celebrity. (Why do we need this step?)
We will discard N elements utmost (Why?). If the celebrity is present in the party, we will call HaveAc-
quaintance() 3(N-1) times. Here is code using stack.
#include <iostream>
#include <list>
using namespace std;
i = 0;
while( i < size )
{
stack.push_back(i);
i = i + 1;
}
int A = stack.back();
stack.pop_back();
int B = stack.back();
stack.pop_back();
while( stack.size() != 1 )
232
{
if( HaveAcquiantance(A, B) )
{
A = stack.back();
stack.pop_back();
}
else
{
B = stack.back();
stack.pop_back();
}
}
// Potential candidate?
C = stack.back();
stack.pop_back();
// Last candidate was not examined, it leads one excess comparison (optimise)
if( HaveAcquiantance(C, B) )
C = B;
if( HaveAcquiantance(C, A) )
C = A;
while( !stack.empty() )
{
i = stack.back();
stack.pop_back();
// i must know C
if( !HaveAcquiantance(i, C) )
return -1;
}
return C;
}
int main()
{
int id = CelebrityUsingStack(size);
233
id == -1 ? cout << "No celebrity" : cout << "Celebrity ID " << id;
return 0;
}
Output
Celebrity ID 2
Complexity O(N). Total comparisons 3(N-1). Try the above code for successful MATRIX {{0, 0, 0, 1}, {0,
0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}}.
A Note:
You may think that why do we need a new graph as we already have access to input matrix. Note that the
matrix MATRIX used to help the hypothetical function HaveAcquaintance(A, B), but never accessed via
usual notation MATRIX[i, j]. We have access to the input only through the function HaveAcquiantance(A,
B). Matrix is just a way to code the solution. We can assume the cost of hypothetical function as O(1).
If still not clear, assume that the function HaveAcquiantance accessing information stored in a set of linked
lists arranged in levels. List node will have next and nextLevel pointers. Every level will have N nodes i.e.
an N element list, next points to next node in the current level list and the nextLevel pointer in last node
of every list will point to head of next level list. For example the linked list representation of above matrix
looks like,
L0 0->0->1->0
|
L1 0->0->1->0
|
L2 0->0->1->0
|
L3 0->0->1->0
The function HaveAcquanintance(i, j) will search in the list for j-th node in the i-th level. Out goal is to
minimize calls to HaveAcquanintance function.
Exercises:
1. Write code to find celebrity. Don’t use any data structures like graphs, stack, etc… you have access to N
and HaveAcquaintance(int, int) only.
2. Implement the algorithm using Queues. What is your observation? Compare your solution with Finding
Maximum and Minimum in an array and Tournament Tree. What are minimum number of comparisons do
we need (optimal number of calls to HaveAcquaintance())?
— Venki. Please write comments if you find anything incorrect, or you want to share more information about
the topic discussed above.
Source
http://www.geeksforgeeks.org/the-celebrity-problem/
234
Chapter 60
Given an array arr[0 … n-1] containing n positive integers, a subsequenceof arr[] is called Bitonic if it is first
increasing, then decreasing. Write a function that takes an array as argument and returns the length of the
longest bitonic subsequence.
A sequence, sorted in increasing order is considered Bitonic with the decreasing part as empty. Similarly,
decreasing order sequence is considered Bitonic with the increasing part as empty.
Examples:
235
arr[] of size n. The function mainly creates two temporary arrays
lis[] and lds[] and returns the maximum lis[i] + lds[i] - 1.
getchar();
return 0;
}
236
Output:
Length of LBS is 7
Source
http://www.geeksforgeeks.org/dynamic-programming-set-15-longest-bitonic-subsequence/
Category: Arrays Tags: Dynamic Programming
Post navigation
← Use of explicit keyword in C++ Dynamic Programming | Set 16 (Floyd Warshall Algorithm) →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
237
Chapter 61
#include<stdio.h>
238
// Create an array that will store index of a smaller
// element on left side. If there is no smaller element
// on left side, then smaller[i] will be -1.
int *smaller = new int[n];
smaller[0] = -1; // first entry will always be -1
for (i = 1; i < n; i++)
{
if (arr[i] <= arr[min])
{
min = i;
smaller[i] = -1;
}
else
smaller[i] = min;
}
return;
}
239
// Driver program to test above function
int main()
{
int arr[] = {12, 11, 10, 5, 6, 2, 30};
int n = sizeof(arr)/sizeof(arr[0]);
find3Numbers(arr, n);
return 0;
}
Output:
5 6 30
Source
http://www.geeksforgeeks.org/find-a-sorted-subsequence-of-size-3-in-linear-time/
Category: Arrays
Post navigation
← Dynamic Programming | Set 16 (Floyd Warshall Algorithm) Operating Systems | Set 6 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
240
Chapter 62
Given an array containing only 0s and 1s, find the largest subarray which contain equal no of 0s and 1s.
Expected time complexity is O(n).
Examples:
// A simple program to find the largest subarray with equal number of 0s and 1s
#include <stdio.h>
// This function Prints the starting and ending indexes of the largest subarray
// with equal number of 0s and 1s. Also returns the size of such subarray.
int findSubArray(int arr[], int n)
{
int sum = 0;
int maxsize = -1, startindex;
241
// Pick a starting point as i
for (int i = 0; i < n-1; i++)
{
sum = (arr[i] == 0)? -1 : 1;
return maxsize;
}
findSubArray(arr, size);
return 0;
}
Output:
0 to 5
242
We will return the max of the values obtained by two cases.
4) To find the maximum length subarray starting from 0th index, scan the sumleft[] and find the maximum
i where sumleft[i] = 0.
5) Now, we need to find the subarray where subarray sum is 0 and start index is not 0. This problem is
equivalent to finding two indexes i & j in sumleft[] such that sumleft[i] = sumleft[j] and j-i is maximum.
To solve this, we can create a hash table with size = max-min+1 where min is the minimum value in the
sumleft[] and max is the maximum value in the sumleft[]. The idea is to hash the leftmost occurrences of
all different values in sumleft[]. The size of hash is chosen as max-min+1 because there can be these many
different possible values in sumleft[]. Initialize all values in hash as -1
6) To fill and use hash[], traverse sumleft[] from 0 to n-1. If a value is not present in hash[], then store its
index in hash. If the value is present, then calculate the difference of current index of sumleft[] and previously
stored value in hash[]. If this difference is more than maxsize, then update the maxsize.
7) To handle corner cases (all 1s and all 0s), we initialize maxsize as -1. If the maxsize remains -1, then
print there is no such subarray.
// A O(n) program to find the largest subarray with equal number of 0s and 1s
#include <stdio.h>
#include <stdlib.h>
// This function Prints the starting and ending indexes of the largest subarray
// with equal number of 0s and 1s. Also returns the size of such subarray.
int findSubArray(int arr[], int n)
{
int maxsize = -1, startindex; // variables to store result values
// Fill sumleft array and get min and max values in it.
// Consider 0 values in arr[] as -1
sumleft[0] = ((arr[0] == 0)? -1: 1);
min = arr[0]; max = arr[0];
for (i=1; i<n; i++)
{
sumleft[i] = sumleft[i-1] + ((arr[i] == 0)? -1: 1);
if (sumleft[i] < min)
min = sumleft[i];
if (sumleft[i] > max)
max = sumleft[i];
}
243
// all values in sumleft[] by min before using them as an index in hash[].
int hash[max-min+1];
return maxsize;
}
findSubArray(arr, size);
return 0;
}
Output:
0 to 5
244
Thanks to Aashish Barnwal for suggesting this solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above
Source
http://www.geeksforgeeks.org/largest-subarray-with-equal-number-of-0s-and-1s/
Category: Arrays
Post navigation
← Computer Networks | Set 6 Computer Networks | Set 7 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
245
Chapter 63
Partition problem is to determine whether a given set can be partitioned into two subsets such that the sum
of elements in both subsets is same.
Examples
arr[] = {1, 5, 3}
Output: false
The array cannot be partitioned into equal sum sets.
246
isSubsetSum (arr, n-1, sum/2 - arr[n-1])
247
getchar();
return 0;
}
Output:
Time Complexity: O(2ˆn) In worst case, this solution tries two possibilities (whether to include or exclude)
for every element.
Dynamic Programming Solution
The problem can be solved using dynamic programming when the sum of the elements is not too big. We can
create a 2D array part[][] of size (sum/2)*(n+1). And we can construct the solution in bottom up manner
such that every filled entry has following property
if (sum%2 != 0)
return false;
bool part[sum/2+1][n+1];
248
for (j = 1; j <= n; j++)
{
part[i][j] = part[i][j-1];
if (i >= arr[j-1])
part[i][j] = part[i][j] || part[i - arr[j-1]][j-1];
}
}
return part[sum/2][n];
}
Output:
Following diagram shows the values in partition table. The diagram is taken form the wiki page of partition
problem.
249
Time Complexity: O(sum*n)
Auxiliary Space: O(sum*n)
Please note that this solution will not be feasible for arrays with big sum.
References:
http://en.wikipedia.org/wiki/Partition_problem
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Source
http://www.geeksforgeeks.org/dynamic-programming-set-18-partition-problem/
Category: Arrays Tags: Dynamic Programming
Post navigation
← Automata Theory | Set 5 Database Management Systems | Set 11 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
250
Chapter 64
Given an array that contains both positive and negative integers, find the product of the maximum product
subarray. Expected Time complexity is O(n) and only O(1) extra space can be used.
Examples:
The following solution assumes that the given input array always has a positive ouput. The solution works
for all cases mentioned above. It doesn’t work for arrays like {0, 0, -20, 0}, {0, 0, 0}.. etc. The solution can
be easily modified to handle this case.
It is similar to Largest Sum Contiguous Subarray problem. The only thing to note here is, maximum product
can also be obtained by minimum (negative) product ending with the previous element multiplied by this
element. For example, in array {12, 2, -3, -5, -6, -2}, when we are at element -2, the maximum product is
multiplication of, minimum product ending with -6 and -2.
#include <stdio.h>
251
int max_ending_here = 1;
/* Traverse throught the array. Following values are maintained after the ith iteration:
max_ending_here is always 1 or some positive product ending with arr[i]
min_ending_here is always 1 or some negative product ending with arr[i] */
for (int i = 0; i < n; i++)
{
/* If this element is positive, update max_ending_here. Update
min_ending_here only if min_ending_here is negative */
if (arr[i] > 0)
{
max_ending_here = max_ending_here*arr[i];
min_ending_here = min (min_ending_here * arr[i], 1);
}
return max_so_far;
}
252
int arr[] = {1, -2, -3, 0, 7, -8, -2};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Maximum Sub array product is %d", maxSubarrayProduct(arr, n));
return 0;
}
Output:
Source
http://www.geeksforgeeks.org/maximum-product-subarray/
Category: Arrays
Post navigation
← Count numbers that don’t contain 3 Magic Square →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
253
Chapter 65
Given an unsorted array and a number n, find if there exists a pair of elements in the array whose difference
is n.
Examples:
Input: arr[] = {5, 20, 3, 2, 50, 80}, n = 78
Output: Pair Found: (2, 80)
#include <stdio.h>
254
// Search for a pair
while (i<size && j<size)
{
if (i != j && arr[j]-arr[i] == n)
{
printf("Pair Found: (%d, %d)", arr[i], arr[j]);
return true;
}
else if (arr[j]-arr[i] < n)
j++;
else
i++;
}
Output:
Hashing can also be used to solve this problem. Create an empty hash table HT. Traverse the array, use
array elements as hash keys and enter them in HT. Traverse the array again look for value n + arr[i] in HT.
Please write comments if you find any of the above codes/algorithms incorrect, or find other ways to solve
the same problem.
Source
http://www.geeksforgeeks.org/find-a-pair-with-the-given-difference/
Category: Arrays
255
Chapter 66
Given an array of integers, replace every element with the next greatest element (greatest element on the
right side) in the array. Since there is no element next to the last element, replace it with -1. For example,
if the array is {16, 17, 4, 3, 5, 2}, then it should be modified to {17, 5, 5, 5, 2, -1}.
The question is very similar to this post and solutions are also similar.
A naive method is to run two loops. The outer loop will one by one pick array elements from left to
right. The inner loop will find the greatest element present after the picked element. Finally the outer loop
will replace the picked element with the greatest element found by inner loop. The time complexity of this
method will be O(n*n).
A tricky method is to replace all elements using one traversal of the array. The idea is to start from the
rightmost element, move to the left side one by one, and keep track of the maximum element. Replace every
element with the maximum element.
#include <stdio.h>
256
arr[i] = max_from_right;
Output:
Source
http://www.geeksforgeeks.org/replace-every-element-with-the-greatest-on-right-side/
Category: Arrays
257
Chapter 67
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.
A pair (c, d) can follow another pair (a, b) if b Amazon Interview | Set 2
For example, if the given pairs are {{5, 24}, {39, 60}, {15, 28}, {27, 40}, {50, 90} }, then the longest chain
that can be formed is of length 3, and the chain is {{5, 24}, {27, 40}, {50, 90}}
This problem is a variation of standard Longest Increasing Subsequence problem. Following is a simple two
step process.
1) Sort given pairs in increasing order of first (or smaller) element.
2) Now run a modified LIS process where we compare the second element of already finalized LIS with the
first element of new LIS being constructed.
The following code is a slight modification of method 2 of this post.
#include<stdio.h>
#include<stdlib.h>
258
for ( i = 1; i < n; i++ )
for ( j = 0; j < i; j++ )
if ( arr[i].a > arr[j].b && mcl[i] < mcl[j] + 1)
mcl[i] = mcl[j] + 1;
// mcl[i] now stores the maximum chain length ending with pair i
return max;
}
Output:
Source
http://www.geeksforgeeks.org/dynamic-programming-set-20-maximum-length-chain-of-pairs/
259
Chapter 68
Given an array of integers, find all combination of four elements in the array whose sum is equal to a given
value X.
For example, if the given array is {10, 2, 3, 4, 5, 9, 7, 8} and X = 23, then your function should print “3 5 7
8� (3 + 5 + 7 + 8 = 23).
Sources: Find Specific Sum and Amazon Interview Question
A Naive Solution is to generate all possible quadruples and compare the sum of every quadruple with X.
The following code implements this simple method using four nested loops
#include <stdio.h>
260
int main()
{
int A[] = {10, 20, 30, 40, 1, 2};
int n = sizeof(A) / sizeof(A[0]);
int X = 91;
findFourElements (A, n, X);
return 0;
}
Output:
# include <stdio.h>
# include <stdlib.h>
261
r = n-1;
Output:
1, 4, 6, 10
Source
http://www.geeksforgeeks.org/find-four-numbers-with-sum-equal-to-given-sum/
Category: Arrays
Post navigation
← Detect if two integers have opposite signs Find four elements that sum to a given value | Set 2 ( O(nˆ2Logn)
Solution) →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
262
Chapter 69
Given an array of integers, find all combination of four elements in the array whose sum is equal to a given
value X.
For example, if the given array is {10, 2, 3, 4, 5, 9, 7, 8} and X = 23, then your function should print “3 5 7
8� (3 + 5 + 7 + 8 = 23).
Sources: Find Specific Sum and Amazon Interview Question
We have discussed a O(nˆ3) algorithm in the previous post on this topic. The problem can be solved in
O(nˆ2Logn) time with the help of auxiliary space.
Thanks to itsnimishfor suggesting this method. Following is the detailed process.
Let the input array be A[].
1) Create an auxiliary array aux[] and store sum of all possible pairs in aux[]. The size of aux[] will be
n*(n-1)/2 where n is the size of A[].
2) Sort the auxiliary array aux[].
3) Now the problem reduces to find two elements in aux[] with sum equal to X. We can use method 1 ofthis
postto find the two elements efficiently. There is following important point to note though. An element of
aux[] represents a pair from A[]. While picking two elements from aux[], we must check whether the two
elements have an element of A[] in common. For example, if first element sum of A[1] and A[2], and second
element is sum of A[2] and A[4], then these two elements of aux[] don’t represent four distinct elements of
input array A[].
Following is C implementation of this method.
#include <stdio.h>
#include <stdlib.h>
263
int sum; // sum of the pair
};
// Function to check if two given pairs have any common element or not
bool noCommon(struct pairSum a, struct pairSum b)
{
if (a.first == b.first || a.first == b.sec ||
a.sec == b.first || a.sec == b.sec)
return false;
return true;
}
264
arr[aux[j].first], arr[aux[j].sec]);
return;
}
else if (aux[i].sum + aux[j].sum < X)
i++;
else
j--;
}
}
Output:
20, 1, 30, 40
Please note that the above code prints only one quadruple. If we remove the return statement and add
statements “i++; j–;”, then it prints same quadruple five times. The code can modified to print all quadruples
only once. It has been kept this way to keep it simple.
Time complexity: The step 1 takes O(nˆ2) time. The second step is sorting an array of size O(nˆ2). Sorting
can be done in O(nˆ2Logn) time using merge sort or heap sort or any other O(nLogn) algorithm. The third
step takes O(nˆ2) time. So overall complexity is O(nˆ2Logn).
Auxiliary Space: O(nˆ2). The big size of auxiliary array can be a concern in this method.
Please write comments if you find any of the above codes/algorithms incorrect, or find other ways to solve
the same problem.
Source
http://www.geeksforgeeks.org/find-four-elements-that-sum-to-a-given-value-set-2/
265
Chapter 70
Given an array of n elements, where each element is at most k away from its target position, devise an
algorithm that sorts in O(n log k) time.
For example, let us consider k is 2, an element at index 7 in the sorted array, can be at indexes 5, 6, 7, 8, 9
in the given array.
Source: Nearly sorted algorithm
We can use Insertion Sort to sort the elements efficiently. Following is the C code for standard Insertion
Sort.
The inner loop will run at most k times. To move every element to its correct place, at most k elements
need to be moved. So overall complexity will be O(nk)
266
We can sort such arrays more efficiently with the help of Heap data structure. Following is the
detailed process that uses Heap.
1) Create a Min Heap of size k+1 with first k+1 elements. This will take O(k) time (See this GFact)
2) One by one remove min element from heap, put it in result array, and add a new element to heap from
remaining elements.
Removing an element and adding a new element to min heap will take Logk time. So overall complexity will
be O(k) + O((n-k)*logK)
#include<iostream>
using namespace std;
// to remove min (or root), add a new value x, and return old root
int replaceMin(int x);
// Given an array of size n, where every element is k away from its target
// position, sorts the array in O(nLogk) time.
int sortK(int arr[], int n, int k)
{
// Create a Min Heap of first (k+1) elements from
// input array
int *harr = new int[k+1];
for (int i = 0; i<=k && i<n; i++) // i < n condition is needed when k > n
harr[i] = arr[i];
MinHeap hp(harr, k+1);
267
for(int i = k+1, ti = 0; ti < n; i++, ti++)
{
// If there are remaining elements, then place
// root of heap at target index and add arr[i]
// to Min Heap
if (i < n)
arr[ti] = hp.replaceMin(arr[i]);
// FOLLOWING ARE IMPLEMENTATIONS OF STANDARD MIN HEAP METHODS FROM CORMEN BOOK
// Constructor: Builds a heap from a given array a[] of given size
MinHeap::MinHeap(int a[], int size)
{
heap_size = size;
harr = a; // store address of array
int i = (heap_size - 1)/2;
while (i >= 0)
{
MinHeapify(i);
i--;
}
}
// Method to change root with given value x, and return the old root
int MinHeap::replaceMin(int x)
{
int root = harr[0];
harr[0] = x;
if (root < x)
MinHeapify(0);
return root;
}
268
void MinHeap::MinHeapify(int i)
{
int l = left(i);
int r = right(i);
int smallest = i;
if (l < heap_size && harr[l] < harr[i])
smallest = l;
if (r < heap_size && harr[r] < harr[smallest])
smallest = r;
if (smallest != i)
{
swap(&harr[i], &harr[smallest]);
MinHeapify(smallest);
}
}
return 0;
}
Output:
The Min Heap based method takes O(nLogk) time and uses O(k) auxiliary space.
269
We can also use a Balanced Binary Search Tree instead of Heap to store K+1 elements. The insertand
deleteoperations on Balanced BST also take O(Logk) time. So Balanced BST based method will also take
O(nLogk) time, but the Heap bassed method seems to be more efficient as the minimum element will always
be at root. Also, Heap doesn’t need extra space for left and right pointers.
Please write comments if you find any of the above codes/algorithms incorrect, or find other ways to solve
the same problem.
Source
http://www.geeksforgeeks.org/nearly-sorted-algorithm/
Category: Arrays
Post navigation
← Adobe Interview | Set 1 Microsoft Interview | Set 4 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
270
Chapter 71
Given n numbers (both +ve and -ve), arranged in a circle, fnd the maximum sum of consecutive number.
Examples:
271
{
// Case 1: get the maximum sum using standard kadane's algorithm
int max_kadane = kadane(a, n);
// Case 2: Now find the maximum sum that includes corner elements.
int max_wrap = 0, i;
for(i=0; i<n; i++)
{
max_wrap += a[i]; // Calculate array-sum
a[i] = -a[i]; // invert the array (change sign)
}
Output:
272
Note that the above algorithm doesn’t work if all numbers are negative e.g., {-1, -2, -3}. It returns 0 in this
case. This case can be handled by adding a pre-check to see if all the numbers are negative before running
the above algorithm.
Please write comments if you find any of the above codes/algorithms incorrect, or find other ways to solve
the same problem.
Source
http://www.geeksforgeeks.org/maximum-contiguous-circular-sum/
Category: Arrays
Post navigation
← What happens when a function is called before its declaration in C? Amazon Interview | Set 7 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
273
Chapter 72
Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s.
Example
Input matrix
0 1 1 1
0 0 1 1
1 1 1 1 // this row has maximum 1s
0 0 0 0
Output: 2
A simple method is to do a row wise traversal of the matrix, count the number of 1s in each row and
compare the count with max. Finally, return the index of row with maximum 1s. The time complexity of
this method is O(m*n) where m is number of rows and n is number of columns in matrix.
We can do better. Since each row is sorted, we can use Binary Search to count of 1s in each row. We find
the index of first instance of 1 in each row. The count of 1s will be equal to total number of columns minus
the index of first 1.
See the following code for implementation of the above approach.
#include <stdio.h>
#define R 4
#define C 4
274
// check if the element at middle index is first 1
if ( ( mid == 0 || arr[mid-1] == 0) && arr[mid] == 1)
return mid;
// The main function that returns index of row with maximum number of 1s.
int rowWithMax1s(bool mat[R][C])
{
int max_row_index = 0, max = -1; // Initialize max values
// Traverse for each row and count number of 1s by finding the index
// of first 1
int i, index;
for (i = 0; i < R; i++)
{
index = first (mat[i], 0, C-1);
if (index != -1 && C-index > max)
{
max = C - index;
max_row_index = i;
}
}
return max_row_index;
}
return 0;
}
Output:
275
Time Complexity: O(mLogn) where m is number of rows and n is number of columns in matrix.
The above solution can be optimized further. Instead of doing binary search in every row, we first check
whether the row has more 1s than max so far. If the row has more 1s, then only count 1s in the row. Also,
to count 1s in a row, we don’t do binary search in complete row, we do search in before the index of last
max.
Following is an optimized version of the above solution.
// The main function that returns index of row with maximum number of 1s.
int rowWithMax1s(bool mat[R][C])
{
int i, index;
// Traverse for each row and count number of 1s by finding the index
// of first 1
for (i = 1; i < R; i++)
{
// Count 1s in this row only if this row has more 1s than
// max so far
if (mat[i][C-max-1] == 1)
{
// Note the optimization here also
index = first (mat[i], 0, C-max);
The worst case time complexity of the above optimized version is also O(mLogn), the will solution work
better on average. Thanks to Naveen Kumar Singh for suggesting the above solution.
Sources: this and this
The worst case of the above solution occurs for a matrix like following.
000…01
0 0 0 ..0 1 1
0…0111
….0 1 1 1 1
Following method works in O(m+n) time complexity in worst case.
Step1: Get the index of first (or leftmost) 1 in the first row.
Step2: Do following for every row after the first row
…IF the element on left of previous leftmost 1 is 0, ignore this row.
276
…ELSE Move left until a 0 is found. Update the leftmost index to this index and max_row_index to be the
current row.
The time complexity is O(m+n) because we can possibly go as far left as we came ahead in the first step.
Following is C++ implementation of this method.
// The main function that returns index of row with maximum number of 1s.
int rowWithMax1s(bool mat[R][C])
{
// Initialize first row as row with max 1s
int max_row_index = 0;
Source
http://www.geeksforgeeks.org/find-the-row-with-maximum-number-1s/
277
Chapter 73
This is an extension of median of two sorted arrays of equal size problem. Here we handle arrays of unequal
size also.
The approach discussed in this post is similar to method 2 of equal size post. The basic idea is same, we find
the median of two arrays and compare the medians to discard almost half of the elements in both arrays.
Since the number of elements may differ here, there are many base cases that need to be handled separately.
Before we proceed to complete solution, let us first talk about all base cases.
Let the two arrays be A[N] and B[M]. In the following explanation, it is assumed that N is smaller than or
equal to M.
Base cases:
The smaller array has only one element
Case 1: N = 1, M = 1.
Case 2: N = 1, M is odd
Case 3: N = 1, M is even
The smaller array has only two elements
Case 4: N = 2, M = 2
Case 5: N = 2, M is odd
Case 6: N = 2, M is even
Case 1: There is only one element in both arrays, so output the average of A[0] and B[0].
Case 2: N = 1, M is odd
Let B[5] = {5, 10, 12, 15, 20}
First find the middle element of B[], which is 12 for above array. There are following 4 sub-cases.
…2.1 If A[0] is smaller than 10, the median is average of 10 and 12.
…2.2 If A[0] lies between 10 and 12, the median is average of A[0] and 12.
…2.3 If A[0] lies between 12 and 15, the median is average of 12 and A[0].
…2.4 If A[0] is greater than 15, the median is average of 12 and 15.
In all the sub-cases, we find that 12 is fixed. So, we need to find the median of B[ M / 2 – 1 ], B[ M / 2 +
1], A[ 0 ] and take its average with B[ M / 2 ].
Case 3: N = 1, M is even
Let B[4] = {5, 10, 12, 15}
First find the middle items in B[], which are 10 and 12 in above example. There are following 3 sub-cases.
…3.1 If A[0] is smaller than 10, the median is 10.
…3.2 If A[0] lies between 10 and 12, the median is A[0].
278
…3.3 If A[0] is greater than 10, the median is 12.
So, in this case, find the median of three elements B[ M / 2 – 1 ], B[ M / 2] and A[ 0 ].
Case 4: N = 2, M = 2
There are four elements in total. So we find the median of 4 elements.
Case 5: N = 2, M is odd
Let B[5] = {5, 10, 12, 15, 20}
The median is given by median of following three elements: B[M/2], max(A[0], B[M/2 – 1]), min(A[1], B[M/2
+ 1]).
Case 6: N = 2, M is even
Let B[4] = {5, 10, 12, 15}
The median is given by median of following four elements: B[M/2], B[M/2 – 1], max(A[0], B[M/2 – 2]),
min(A[1], B[M/2 + 1])
Remaining Cases:
Once we have handled the above base cases, following is the remaining process.
1) Find the middle item of A[] and middle item of B[].
…..1.1) If the middle item of A[] is greater than middle item of B[], ignore the last half of A[], let length of
ignored part is idx. Also, cut down B[] by idx from the start.
…..1.2) else, ignore the first half of A[], let length of ignored part is idx. Also, cut down B[] by idx from the
last.
Following is C implementation of the above approach.
279
}
// Case 2: If the larger array has odd number of elements, then consider
// the middle 3 elements of larger array and the only element of
// smaller array. Take few examples like following
// A = {9}, B[] = {5, 8, 10, 20, 30} and
// A[] = {1}, B[] = {5, 8, 10, 20, 30}
if( M & 1 )
return MO2( B[M/2], MO3(A[0], B[M/2 - 1], B[M/2 + 1]) );
// Case 3: If the larger array has even number of element, then median
// will be one of the following 3 elements
// ... The middle two elements of larger array
// ... The only element of smaller array
return MO3( B[M/2], B[M/2 - 1], A[0] );
}
// Case 5: If the larger array has odd number of elements, then median
// will be one of the following 3 elements
// 1. Middle element of larger array
// 2. Max of first element of smaller array and element just
// before the middle in bigger array
// 3. Min of second element of smaller array and element just
// after the middle in bigger array
if( M & 1 )
return MO3 ( B[M/2],
max( A[0], B[M/2 - 1] ),
min( A[1], B[M/2 + 1] )
);
280
return MO4 ( B[M/2],
B[M/2 - 1],
max( A[0], B[M/2 - 2] ),
min( A[1], B[M/2 + 1] )
);
}
int idxA = ( N - 1 ) / 2;
int idxB = ( M - 1 ) / 2;
return findMedianUtil( A, N, B, M );
}
Output:
10
281
Source
http://www.geeksforgeeks.org/median-of-two-sorted-arrays-of-different-sizes/
Category: Arrays Tags: Divide and Conquer
Post navigation
← Implement LRU Cache Print unique rows in a given boolean matrix →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
282
Chapter 74
Given an array, write a program to generate a random permutation of array elements. This question is also
asked as “shuffle a deck of cards” or “randomize a given array”.
Let the given array be arr[]. A simple solution is to create an auxiliary array temp[] which is initially a copy
of arr[]. Randomly select an element from temp[], copy the randomly selected element to arr[0] and remove
the selected element from temp[]. Repeat the same process n times and keep copying elements to arr[1],
arr[2], … . The time complexity of this solution will be O(nˆ2).
Fisher–Yates shuffle Algorithm works in O(n) time complexity. The assumption here is, we are given a
function rand() that generates random number in O(1) time.
The idea is to start from the last element, swap it with a randomly selected element from the whole array
(including last). Now consider the array from 0 to n-2 (size reduced by 1), and repeat the process till we hit
the first element.
Following is the detailed algorithm
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
283
// A utility function to swap to integers
void swap (int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Start from the last element and swap one by one. We don't
// need to run for the first element that's why i > 0
for (int i = n-1; i > 0; i--)
{
// Pick a random index from 0 to i
int j = rand() % (i+1);
return 0;
}
Output:
7 8 4 6 3 1 2 5
284
How does this work?
The probability that ith element (including the last one) goes to last position is 1/n, because we randomly
pick an element in first iteration.
The probability that ith element goes to second last position can be proved to be 1/n by dividing it in two
cases.
Case 1: i = n-1 (index of last element):
The probability of last element going to second last position is = (probability that last element doesn’t stay
at its original position) x (probability that the index picked in previous step is picked again so that the last
element is swapped)
So the probability = ((n-1)/n) x (1/(n-1)) = 1/n
Case 2: 0 :
The probability of ith element going to second position = (probability that ith element is not picked in previous
iteration) x (probability that ith element is picked in this iteration)
So the probability = ((n-1)/n) x (1/(n-1)) = 1/n
We can easily generalize above proof for any other position.
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Source
http://www.geeksforgeeks.org/shuffle-a-given-array/
285
Chapter 75
Given an unsorted array of positive integers. Find the number of triangles that can be formed with three
different array elements as three sides of triangles. For a triangle to be possible from 3 values, the sum of
any two values (or sides) must be greater than the third value (or third side).
For example, if the input array is {4, 6, 3, 7}, the output should be 3. There are three triangles possible {3,
4, 6}, {4, 6, 7} and {3, 6, 7}. Note that {3, 4, 7} is not a possible triangle.
As another example, consider the array {10, 21, 22, 100, 101, 200, 300}. There can be 6 possible triangles:
{10, 21, 22}, {21, 100, 101}, {22, 100, 101}, {10, 100, 101}, {100, 101, 200} and {101, 200, 300}
Method 1 (Brute force)
The brute force method is to run three loops and keep track of the number of triangles possible so far. The
three loops select three different values from array, the innermost loop checks for the triangle property ( the
sum of any two sides must be greater than the value of third side).
Time Complexity: O(Nˆ3) where N is the size of input array.
Method 2 (Tricky and Efficient)
Let a, b and c be three sides. The below condition must hold for a triangle (Sum of two sides is greater than
the third side)
i) a + b > c
ii) b + c > a
iii) a + c > b
Following are steps to count triangle.
1. Sort the array in non-decreasing order.
2. Initialize two pointers ‘i’ and ‘j’ to first and second elements respectively, and initialize count of triangles
as 0.
3. Fix ‘i’ and ‘j’ and find the rightmost index ‘k’ (or largest ‘arr[k]’) such that ‘arr[i] + arr[j] > arr[k]’. The
number of triangles that can be formed with ‘arr[i]’ and ‘arr[j]’ as two sides is ‘k – j’. Add ‘k – j’ to count of
triangles.
Let us consider ‘arr[i]’ as ‘a’, ‘arr[j]’ as b and all elements between ‘arr[j+1]’ and ‘arr[k]’ as ‘c’. The above
mentioned conditions (ii) and (iii) are satisfied because ‘arr[i] 4. Increment ‘j’ to fix the second element
again.
Note that in step 3, we can use the previous value of ‘k’. The reason is simple, if we know that the value
of ‘arr[i] + arr[j-1]’ is greater than ‘arr[k]’, then we can say ‘arr[i] + arr[j]’ will also be greater than ‘arr[k]’,
because the array is sorted in increasing order.
286
5. If ‘j’ has reached end, then increment ‘i’. Initialize ‘j’ as ‘i + 1�, ‘k’ as ‘i+2� and repeat the steps 3 and 4.
Following is implementation of the above approach.
// Program to count number of triangles that can be formed from given array
#include <stdio.h>
#include <stdlib.h>
// Fix the first element. We need to run till n-3 as the other two elements are
// selected from arr[i+1...n-1]
for (int i = 0; i < n-2; ++i)
{
// Initialize index of the rightmost third element
int k = i+2;
287
return count;
}
return 0;
}
Output:
Time Complexity: O(nˆ2). The time complexity looks more because of 3 nested loops. If we take a closer
look at the algorithm, we observe that k is initialized only once in the outermost loop. The innermost loop
executes at most O(n) time for every iteration of outer most loop, because k starts from i+2 and goes upto
n for all values of j. Therefore, the time complexity is O(nˆ2).
Source: http://stackoverflow.com/questions/8110538/total-number-of-possible-triangles-from-n-numbers
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Source
http://www.geeksforgeeks.org/find-number-of-triangles-possible/
288
Chapter 76
Following is a typical recursive implementation of Quick Sort that uses last element as pivot.
/* This function takes last element as pivot, places the pivot element at its
correct position in sorted array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right of pivot */
int partition (int arr[], int l, int h)
{
int x = arr[h];
int i = (l - 1);
/* A[] --> Array to be sorted, l --> Starting index, h --> Ending index */
void quickSort(int A[], int l, int h)
{
if (l < h)
{
int p = partition(A, l, h); /* Partitioning index */
quickSort(A, l, p - 1);
quickSort(A, p + 1, h);
}
}
289
1) The above implementation uses last index as pivot. This causes worst-case behavior on already sorted
arrays, which is a commonly occurring case. The problem can be solved by choosing either a random index
for the pivot, or choosing the middle index of the partition or choosing the median of the first, middle and
last element of the partition for the pivot. (See thisfor details)
2) To reduce the recursion depth, recur first for the smaller half of the array, and use a tail call to recurse
into the other.
3) Insertion sort works better for small subarrays. Insertion sort can be used for invocations on such small
arrays (i.e. where the length is less than a threshold t determined experimentally). For example, this library
implementation of qsort uses insertion sort below size 7.
Despite above optimizations, the function remains recursive and uses function call stack to store intermediate
values of l and h. The function call stack stores other bookkeeping information together with parameters.
Also, function calls involve overheads like storing activation record of the caller function and then resuming
execution.
The above function can be easily converted to iterative version with the help of an auxiliary stack. Following
is an iterative implementation of the above recursive code.
/* A[] --> Array to be sorted, l --> Starting index, h --> Ending index */
void quickSortIterative (int arr[], int l, int h)
{
// Create an auxiliary stack
int stack[ h - l + 1 ];
290
// initialize top of stack
int top = -1;
291
Output:
1 2 2 3 3 3 4 5
The above mentioned optimizations for recursive quick sort can also be applied to iterative version.
1) Partition process is same in both recursive and iterative. The same techniques to choose optimal pivot
can also be applied to iterative version.
2) To reduce the stack size, first push the indexes of smaller half.
3) Use insertion sort when the size reduces below a experimentally calculated threshold.
References:
http://en.wikipedia.org/wiki/Quicksort
This article is compiled by Aashish Barnwal and reviewed by GeeksforGeeks team. Please write comments
if you find anything incorrect, or you want to share more information about the topic discussed above.
Source
http://www.geeksforgeeks.org/iterative-quick-sort/
Category: Arrays
Post navigation
← Output of C++ Program | Set 17 Longest Palindromic Substring | Set 1 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
292
Chapter 77
A graph where all vertices are connected with each other, has exactly one connected component, consisting
of the whole graph. Such graph with only one connected component is called as Strongly Connected Graph.
The problem can be easily solved by applying DFS() on each component. In each DFS() call, a component
or a sub-graph is visited. We will call DFS on the next un-visited component. The number of calls to DFS()
gives the number of connected components. BFS can also be used.
What is an island?
A group of connected 1s forms an island. For example, the below matrix contains 5 islands
{1, 1, 0, 0, 0},
293
{0, 1, 0, 0, 1},
{1, 0, 0, 1, 1},
{0, 0, 0, 0, 0},
{1, 0, 1, 0, 1}
A cell in 2D matrix can be connected to 8 neighbors. So, unlike standard DFS(), where we recursively call
for all adjacent vertices, here we can recursive call for 8 neighbors only. We keep track of the visited 1s so
that they are not visited again.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define ROW 5
#define COL 5
294
// Initialize count as 0 and travese through the all cells of
// given matrix
int count = 0;
for (int i = 0; i < ROW; ++i)
for (int j = 0; j < COL; ++j)
if (M[i][j] && !visited[i][j]) // If a cell with value 1 is not
{ // visited yet, then new island found
DFS(M, i, j, visited); // Visit all cells in this island.
++count; // and increment island count
}
return count;
}
return 0;
}
Output:
http://en.wikipedia.org/wiki/Connected_component_%28graph_theory%29
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Source
http://www.geeksforgeeks.org/find-number-of-islands/
Category: Arrays Graph Tags: Graph
Post navigation
← Floor and Ceil from a BST Reservoir Sampling →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
295
Chapter 78
Construction of Longest
Monotonically Increasing
Subsequence (N log N)
In my previous post, I have explained about longest monotonically increasing sub-sequence (LIS) problem
in detail. However, the post only covered code related to querying size of LIS, but not the construction of
LIS. I left it as an exercise. If you have solved, cheers. If not, you are not alone, here is code.
If you have not read my previous post, read here. Note that the below code prints LIS in reverse order. We
can modify print order using a stack (explicit or system stack). I am leaving explanation as an exercise
(easy).
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
// Binary search
int GetCeilIndex(int A[], int T[], int l, int r, int key) {
int m;
while( r - l > 1 ) {
m = l + (r - l)/2;
if( A[T[m]] >= key )
r = m;
else
l = m;
}
return r;
}
296
int *tailIndices = new int[size];
int *prevIndices = new int[size];
int len;
memset(tailIndices, 0, sizeof(tailIndices[0])*size);
memset(prevIndices, 0xFF, sizeof(prevIndices[0])*size);
tailIndices[0] = 0;
prevIndices[0] = -1;
len = 1; // it will always point to empty location
for( int i = 1; i < size; i++ ) {
if( A[i] < A[tailIndices[0]] ) {
// new smallest value
tailIndices[0] = i;
} else if( A[i] > A[tailIndices[len-1]] ) {
// A[i] wants to extend largest subsequence
prevIndices[i] = tailIndices[len-1];
tailIndices[len++] = i;
} else {
// A[i] wants to be a potential condidate of future subsequence
// It will replace ceil value in tailIndices
int pos = GetCeilIndex(A, tailIndices, -1, len-1, A[i]);
prevIndices[i] = tailIndices[pos-1];
tailIndices[pos] = i;
}
}
cout << "LIS of given input" << endl;
for( int i = tailIndices[len-1]; i >= 0; i = prevIndices[i] )
cout << A[i] << " ";
cout << endl;
delete[] tailIndices;
delete[] prevIndices;
return len;
}
int main() {
int A[] = { 2, 5, 3, 7, 11, 8, 10, 13, 6 };
int size = sizeof(A)/sizeof(A[0]);
return 0;
}
Exercises:
1. You know Kadane‘s algorithm to find maximum sum sub-array. Modify Kadane’s algorithm to trace
starting and ending location of maximum sum sub-array.
2. Modify Kadane‘s algorithm to find maximum sum sub-array in a circular array. Refer GFG forum for
many comments on the question.
297
3. Given two integers A and B as input. Find number of Fibonacci numbers existing in between these two
numbers (including A and B). For example, A = 3 and B = 18, there are 4 Fibonacci numbers in between
{3, 5, 8, 13}. Do it in O(log K) time, where K is max(A, B). What is your observation?
— Venki. Please write comments if you find anything incorrect, or you want to share more information about
the topic discussed above.
Source
http://www.geeksforgeeks.org/construction-of-longest-monotonically-increasing-subsequence-n-log-n/
Category: Arrays
298
Chapter 79
Suppose there is a circle. There are n petrol pumps on that circle. You are given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Calculate the first point from where a truck will be able to complete the circle (The truck will stop at each
petrol pump and it has infinite capacity). Expected time complexity is O(n). Assume for 1 litre petrol, the
truck can go 1 unit of distance.
For example, let there be 4 petrol pumps with amount of petrol and distance to next petrol pump value
pairs as {4, 6}, {6, 5}, {7, 3} and {4, 5}. The first point from where truck can make a circular tour is 2nd
petrol pump. Output should be “start = 1� (index of 2nd petrol pump).
A Simple Solution is to consider every petrol pumps as starting point and see if there is a possible tour. If
we find a starting point with feasible solution, we return that starting point. The worst case time complexity
of this solution is O(nˆ2).
We can use a Queue to store the current tour. We first enqueue first petrol pump to the queue, we keep
enqueueing petrol pumps till we either complete the tour, or current amount of petrol becomes negative.
If the amount becomes negative, then we keep dequeueing petrol pumps till the current amount becomes
positive or queue becomes empty.
Instead of creating a separate queue, we use the given array itself as queue. We maintain two index variables
start and end that represent rear and front of queue.
299
int printTour(struct petrolPump arr[], int n)
{
// Consider first petrol pump as a starting point
int start = 0;
int end = 1;
int n = sizeof(arr)/sizeof(arr[0]);
int start = printTour(arr, n);
return 0;
}
Output:
start = 2
300
Time Complexity: Seems to be more than linear at first look. If we consider the items between start and
end as part of a circular queue, we can observe that every item is enqueued at most two times to the queue.
The total number of operations is proportional to total number of enqueue operations. Therefore the time
complexity is O(n).
Auxiliary Space: O(1)
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above
Source
http://www.geeksforgeeks.org/find-a-tour-that-visits-all-stations/
301
Chapter 80
Given an array of numbers, arrange them in a way that yields the largest value. For example, if the given
numbers are {54, 546, 548, 60}, the arrangement 6054854654 gives the largest value. And if the given
numbers are {1, 34, 3, 98, 9, 76, 45, 4}, then the arrangement 998764543431 gives the largest value.
A simple solution that comes to our mind is to sort all numbers in descending order, but simply sorting
doesn’t work. For example, 548 is greater than 60, but in output 60 comes before 548. As a second example,
98 is greater than 9, but 9 comes before 98 in output.
So how do we go about it? The idea is to use any comparison based sorting algorithm. In the used sorting
algorithm, instead of using the default comparison, write a comparison function myCompare() and use it to
sort numbers. Given two numbers X and Y, how should myCompare() decide which number to put first –
we compare two numbers XY (Y appended at the end of X) and YX (X appended at the end of Y). If XY
is larger, then X should come before Y in output, else Y should come before. For example, let X and Y be
542 and 60. To compare X and Y, we compare 54260 and 60542. Since 60542 is greater than 54260, we put
Y first.
Following is C++ implementation of the above approach. To keep the code simple, numbers are considered
as strings, and vectoris used instead of normal array.
302
// Now see which of the two formed numbers is greater
return XY.compare(YX) > 0 ? 1: 0;
}
// The main function that prints the arrangement with the largest value.
// The function accepts a vector of strings
void printLargest(vector<string> arr)
{
// Sort the numbers using library sort funtion. The function uses
// our comparison function myCompare() to compare two strings.
// See http://www.cplusplus.com/reference/algorithm/sort/ for details
sort(arr.begin(), arr.end(), myCompare);
return 0;
}
Output:
6054854654
303
This article is compiled by Ravi Chandra Enaganti. Please write comments if you find anything incorrect,
or you want to share more information about the topic discussed above.
Source
http://www.geeksforgeeks.org/given-an-array-of-numbers-arrange-the-numbers-to-form-the-biggest-number/
304
Chapter 81
Pancake sorting
Given an an unsorted array, sort the given array. You are allowed to do only following operation on array.
Unlike a traditional sorting algorithm, which attempts to sort with the fewest comparisons possible, the goal
is to sort the sequence in as few reversals as possible.
The idea is to do something similar to Selection Sort. We one by one place maximum element at the end
and reduce the size of current array by one.
Following are the detailed steps. Let given array be arr[] and size of array be n.
1) Start from current size equal to n and reduce current size by one while it’s greater than 1. Let the current
size be curr_size. Do following for every curr_size
……a) Find index of the maximum element in arr[0..curr_szie-1]. Let the index be ‘mi’
……b) Call flip(arr, mi)
……c) Call flip(arr, curr_size-1)
See following video for visualization of the above algorithm.
[iframe width=”340� height=”252� src=”http://www.youtube.com/embed/kk-_DDgoXfk” framebor-
der=”0�]
/* Reverses arr[0..i] */
void flip(int arr[], int i)
{
int temp, start = 0;
while (start < i)
{
temp = arr[start];
arr[start] = arr[i];
arr[i] = temp;
start++;
i--;
305
}
}
// The main function that sorts given array using flip operations
int pancakeSort(int *arr, int n)
{
// Start from the complete array and one by one reduce current size by one
for (int curr_size = n; curr_size > 1; --curr_size)
{
// Find index of the maximum element in arr[0..curr_size-1]
int mi = findMax(arr, curr_size);
pancakeSort(arr, n);
return 0;
306
}
Output:
Sorted Array
6 7 10 11 12 20 23
Total O(n) flip operations are performed in above code. The overall time complexity is O(nˆ2).
References:
http://en.wikipedia.org/wiki/Pancake_sorting
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Source
http://www.geeksforgeeks.org/pancake-sorting/
Category: Arrays
307
Chapter 82
We have discussed Pancake Sorting in the previous post. Following is a problem based on Pancake Sorting.
Given an an unsorted array, sort the given array. You are allowed to do only following operation on array.
Imagine a hypothetical machine where flip(i) always takes O(1) time. Write an efficient pro-
gram for sorting a given array in O(nLogn) time on the given machine. If we apply the same
algorithm here, the time taken will be O(nˆ2) because the algorithm calls findMax() in a loop and find
findMax() takes O(n) time even on this hypothetical machine.
We can use insertion sort that uses binary search. The idea is to run a loop from second element to last
element (from i = 1 to n-1), and one by one insert arr[i] in arr[0..i-1] (like standard insertion sort algorithm).
When we insert an element arr[i], we can use binary search to find position of arr[i] in O(Logi) time. Once
we have the position, we can use some flip operations to put arr[i] at its new place. Following are abstract
steps.
Since flip operation takes O(1) on given hypothetical machine, total running time of above algorithm is
O(nlogn). Thanks to Kumar for suggesting above problem and algorithm.
Let us see how does the above algorithm work. ceilSearch() actually returns the index of the smallest element
which is greater than arr[i] in arr[0..i-1]. If there is no such element, it returns -1. Let the returned value
be j. If j is -1, then we don’t need to do anything as arr[i] is already the greatest element among arr[0..i].
Otherwise we need to put arr[i] just before arr[j].
So how to apply flip operations to put arr[i] just before arr[j] using values of i and j. Let us take an example
to understand this. Let i be 6 and current array be {12, 15, 18, 30, 35, 40, 20, 6, 90, 80}. To put 20 at its
308
new place, the array should be changed to {12, 15, 18, 20, 30, 35, 40, 6, 90, 80}. We apply following steps
to put 20 at its new place.
1) Find j using ceilSearch (In the above example j is 3).
2) flip(arr, j-1) (array becomes {18, 15, 12, 30, 35, 40, 20, 6, 90, 80})
3) flip(arr, i-1); (array becomes {40, 35, 30, 12, 15, 18, 20, 6, 90, 80})
4) flip(arr, i); (array becomes {20, 18, 15, 12, 30, 35, 40, 6, 90, 80})
5) flip(arr, j); (array becomes {12, 15, 18, 20, 30, 35, 40, 6, 90, 80})
Following is C implementation of the above algorithm.
#include <stdlib.h>
#include <stdio.h>
309
/* Reverses arr[0..i] */
void flip(int arr[], int i)
{
int temp, start = 0;
while (start < i)
{
temp = arr[start];
arr[start] = arr[i];
arr[i] = temp;
start++;
i--;
}
}
/* Function to sort an array using insertion sort, binary search and flip */
void insertionSort(int arr[], int size)
{
int i, j;
// Start from the second element and one by one insert arr[i]
// in already sorted arr[0..i-1]
for(i = 1; i < size; i++)
{
// Find the smallest element in arr[0..i-1] which is also greater than
// or equal to arr[i]
int j = ceilSearch(arr, 0, i-1, arr[i]);
310
return 0;
}
Output:
6 12 18 20 30 35 35 40 80 90
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Source
http://www.geeksforgeeks.org/a-pancake-sorting-question/
Category: Arrays
Post navigation
← Amazon Interview | Set 22 Swap all odd and even bits →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
311
Chapter 83
Tug of War
Given a set of n integers, divide the set in two subsets of n/2 sizes each such that the difference of the sum
of two subsets is as minimum as possible. If n is even, then sizes of two subsets must be strictly n/2 and if
n is odd, then size of one subset must be (n-1)/2 and size of other subset must be (n+1)/2.
For example, let given set be {3, 4, 5, -3, 100, 1, 89, 54, 23, 20}, the size of set is 10. Output for this set
should be {4, 100, 1, 23, 20} and {3, 5, -3, 89, 54}. Both output subsets are of size 5 and sum of elements
in both subsets is same (148 and 148).
Let us consider another example where n is odd. Let given set be {23, 45, -34, 12, 0, 98, -99, 4, 189, -1, 4}.
The output subsets should be {45, -34, 12, 98, -1} and {23, 0, -99, 4, 189, 4}. The sums of elements in two
subsets are 120 and 121 respectively.
The following solution tries every possible subset of half size. If one subset of half size is formed, the
remaining elements form the other subset. We initialize current set as empty and one by one build it. There
are two possibilities for every element, either it is part of current set, or it is part of the remaining elements
(other subset). We consider both possibilities for every element. When the size of current set becomes n/2,
we check whether this solutions is better than the best solution available so far. If it is, then we update the
best solution.
Following is C++ implementation for Tug of War problem. It prints the required arrays.
#include <iostream>
#include <stdlib.h>
#include <limits.h>
using namespace std;
// checks that the numbers of elements left are not less than the
// number of elements required to form the solution
if ((n/2 - no_of_selected_elements) > (n - curr_position))
return;
312
// consider the cases when current element is not included in the solution
TOWUtil(arr, n, curr_elements, no_of_selected_elements,
soln, min_diff, sum, curr_sum, curr_position+1);
int sum = 0;
for (int i=0; i<n; i++)
{
sum += arr[i];
curr_elements[i] = soln[i] = false;
}
313
cout << "The first subset is: ";
for (int i=0; i<n; i++)
{
if (soln[i] == true)
cout << arr[i] << " ";
}
cout << "\nThe second subset is: ";
for (int i=0; i<n; i++)
{
if (soln[i] == false)
cout << arr[i] << " ";
}
}
Output:
This article is compiled by Ashish Anand and reviewed by GeeksforGeeks team. Please write comments if
you find anything incorrect, or you want to share more information about the topic discussed above.
Source
http://www.geeksforgeeks.org/tug-of-war/
Category: Arrays Tags: Backtracking
Post navigation
← Interview Experiences at D. E. Shaw & Co. Print Matrix Diagonally →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
314
Chapter 84
You are given a one dimensional array that may contain both positive and negative integers, find the sum
of contiguous subarray of numbers which has the largest sum.
For example, if the given array is {-2, -5, 6, -2, -3, 1, 5, -6}, then the maximum subarray sum is 7 (see
highlighted elements).
The naive method is to run two loops. The outer loop picks the beginning element, the inner loop finds
the maximum possible sum with first element picked by outer loop and compares this maximum with the
overall maximum. Finally return the overall maximum. The time complexity of the Naive method is O(nˆ2).
Using Divide and Conquer approach, we can find the maximum subarray sum in O(nLogn) time. Following
is the Divide and Conquer algorithm.
1) Divide the given array in two halves
2) Return the maximum of following three
….a) Maximum subarray sum in left half (Make a recursive call)
….b) Maximum subarray sum in right half (Make a recursive call)
….c) Maximum subarray sum such that the subarray crosses the midpoint
The lines 2.a and 2.b are simple recursive calls. How to find maximum subarray sum such that the subarray
crosses the midpoint? We can easily find the crossing sum in linear time. The idea is simple, find the
maximum sum starting from mid point and ending at some point on left of mid, then find the maximum
sum starting from mid + 1 and ending with sum point on right of mid + 1. Finally, combine the two and
return.
// A Divide and Conquer based program for maximum subarray sum problem
#include <stdio.h>
#include <limits.h>
// Find the maximum possible sum in arr[] auch that arr[m] is part of it
int maxCrossingSum(int arr[], int l, int m, int h)
315
{
// Include elements on left of mid.
int sum = 0;
int left_sum = INT_MIN;
for (int i = m; i >= l; i--)
{
sum = sum + arr[i];
if (sum > left_sum)
left_sum = sum;
}
316
Time Complexity: maxSubArraySum() is a recursive method and time complexity can be expressed as
following recurrence relation.
T(n) = 2T(n/2) + Θ(n)
The above recurrence is similar to Merge Sort and can be solved either using Recurrence Tree method or
Master method. It falls in case II of Master Method and solution of the recurrence is Θ(nLogn).
The Kadane’s Algorithm for this problem takes O(n) time. Therefore the Kadane’s algorithm is better
than the Divide and Conquer approach, but this problem can be considered as a good example to show
power of Divide and Conquer. The above simple approach where we divide the array in two halves, reduces
the time complexity from O(nˆ2) to O(nLogn).
References:
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Source
http://www.geeksforgeeks.org/divide-and-conquer-maximum-sum-subarray/
317
Chapter 85
Counting Sort
Counting sort is a sorting technique based on keys between a specific range. It works by counting the number
of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position
of each object in the output sequence.
Let us understand it with the help of an example.
2) Modify the count array such that each element at each index
stores the sum of previous counts.
Index: 0 1 2 3 4 5 6 7 8 9
Count: 0 2 4 4 5 6 6 7 7 7
// The main function that sort the given string str in alphabatical order
void countSort(char *str)
318
{
// The output character array that will have sorted str
char output[strlen(str)];
countSort(str);
Output:
Time Complexity: O(n+k) where n is the number of elements in input array and k is the range of input.
Auxiliary Space: O(n+k)
Points to be noted:
1. Counting sort is efficient if the range of input data is not significantly greater than the number of objects
to be sorted. Consider the situation where the input sequence is between range 1 to 10K and the data is 10,
5, 10K, 5K.
319
2. It is not a comparison based sorting. It running time complexity is O(n) with space proportional to the
range of data.
3. It is often used as a sub-routine to another sorting algorithm like radix sort.
4. Counting sort uses a partial hashing to count the occurrence of the data object in O(1).
5. Counting sort can be extended to work for negative inputs also.
Exercise:
1. Modify above code to sort the input data in the range from M to N.
2. Modify above code to sort negative input data.
3. Is counting sort stable and online?
4. Thoughts on parallelizing the counting sort algorithm.
This article is compiled byAashish Barnwal. Please write comments if you find anything incorrect, or you
want to share more information about the topic discussed above.
Source
http://www.geeksforgeeks.org/counting-sort/
Category: Arrays
320
Chapter 86
Given a set of time intervals in any order, merge all overlapping intervals into one and output the result
which should have only mutually exclusive intervals. Let the intervals be represented as pairs of integers for
simplicity.
For example, let the given set of intervals be {{1,3}, {2,4}, {5,7}, {6,8} }. The intervals {1,3} and {2,4}
overlap with each other, so they should be merged and become {1, 4}. Similarly {5, 7} and {6, 8} should
be merged and become {5, 8}
Write a function which produces the set of merged intervals for the given set of intervals.
A simple approach is to start from the first interval and compare it with all other intervals for overlapping,
if it overlaps with any other interval, then remove the other interval from list and merge the other into the
first interval. Repeat the same steps for remaining intervals after first. This approach cannot be implemented
in better than O(nˆ2) time.
An efficient approach is to first sort the intervals according to starting time. Once we have the sorted
intervals, we can combine all intervals in a linear traversal. The idea is, in sorted array of intervals, if interval[i]
doesn’t overlap with interval[i-1], then interval[i+1] cannot overlap with interval[i-1] because starting time
of interval[i+1] must be greater than or equal to interval[i]. Following is the detailed step by step algorithm.
321
struct Interval
{
int start, end;
};
322
{
Interval t = s.top();
cout << "[" << t.start << "," << t.end << "] ";
s.pop();
}
return;
}
// Driver program
int main()
{
Interval arr[] = { {6,8}, {1,9}, {2,4}, {4,7} };
int n = sizeof(arr)/sizeof(arr[0]);
mergeIntervals(arr, n);
return 0;
}
Output:
Time complexity of the method is O(nLogn) which is for sorting. Once the array of intervals is sorted,
merging takes linear time.
A O(n Log n) and O(1) Extra Space Solution
The above solution requires O(n) extra space for stack. We can avoid use of extra space by doing merge
operations in-place. Below are detailed steps.
Note that if intervals are sorted by decreasing order of start times, we can quickly check if intervals overlap
or not by comparing start time of previous interval with end time of current interval.
Below is C++ implementation of above algorithm.
// An Interval
struct Interval
{
int s, e;
323
};
// Driver program
int main()
{
Interval arr[] = { {6,8}, {1,9}, {2,4}, {4,7} };
int n = sizeof(arr)/sizeof(arr[0]);
mergeIntervals(arr, n);
return 0;
}
Output:
324
This article is compiled by Ravi Chandra Enaganti. Please write comments if you find anything incorrect,
or you want to share more information about the topic discussed above.
Source
http://www.geeksforgeeks.org/merging-intervals/
Category: Arrays Tags: stack
Post navigation
← Construct Complete Binary Tree from its Linked List Representation Amazon Interview | Set 23 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
325
Chapter 87
Given an array of size n, the array contains numbers in range from 0 to k-1 where k is a positive integer and
k Find the maximum repeating number in this array. For example, let k be 10 the given array be arr[] = {1,
2, 2, 2, 0, 2, 0, 2, 3, 8, 0, 9, 2, 3}, the maximum repeating number would be 2. Expected time complexity is
O(n) and extra space allowed is O(1). Modifications to array are allowed.
The naive approach is to run two loops, the outer loop picks an element one by one, the inner loop
counts number of occurrences of the picked element. Finally return the element with maximum count. Time
complexity of this approach is O(nˆ2).
A better approach is to create a count array of size k and initialize all elements of count[] as 0. Iterate
through all elements of input array, and for every element arr[i], increment count[arr[i]]. Finally, iterate
through count[] and return the index with maximum value. This approach takes O(n) time, but requires
O(k) space.
Following is the O(n) time and O(1) extra space approach. Let us understand the approach with a
simple example where arr[] = {2, 3, 3, 5, 3, 4, 1, 7}, k = 8, n = 8 (number of elements in arr[]).
1) Iterate though input array arr[], for every element arr[i], increment arr[arr[i]%k] by k (arr[] becomes {2,
11, 11, 29, 11, 12, 1, 15 })
2) Find the maximum value in the modified array (maximum value is 29). Index of the maximum value is
the maximum repeating element (index of 29 is 3).
3) If we want to get the original array back, we can iterate through the array one more time and do arr[i]
= arr[i] % k where i varies from 0 to n-1.
How does the above algorithm work? Since we use arr[i]%k as index and add value k at the index arr[i]%k,
the index which is equal to maximum repeating element will have the maximum value in the end. Note
that k is added maximum number of times at the index equal to maximum repeating element and all array
elements are smaller than k.
Following is C++ implementation of the above algorithm.
#include<iostream>
using namespace std;
326
int maxRepeating(int* arr, int n, int k)
{
// Iterate though input array, for every element
// arr[i], increment arr[arr[i]%k] by k
for (int i = 0; i< n; i++)
arr[arr[i]%k] += k;
return 0;
}
Output:
Exercise:
The above solution prints only one repeating element and doesn’t work if we want to print all maximum
repeating elements. For example, if the input array is {2, 3, 2, 3}, the above solution will print only 3. What
if we need to print both of 2 and 3 as both of them occur maximum number of times. Write a O(n) time
and O(1) extra space function that prints all maximum repeating elements. (Hint: We can use maximum
quotient arr[i]/n instead of maximum value in step 2).
Note that the above solutions may cause overflow if adding k repeatedly makes the value more than
INT_MAX.
This article is compiled by Ashish Anand and reviewed by GeeksforGeeks team. Please write comments if
you find anything incorrect, or you want to share more information about the topic discussed above.
327
Source
http://www.geeksforgeeks.org/find-the-maximum-repeating-number-in-ok-time/
328
Chapter 88
The cost of a stock on each day is given in an array, find the max profit that you can make by buying and
selling in those days. For example, if the given array is {100, 180, 260, 310, 40, 535, 695}, the maximum
profit can earned by buying on day 0, selling on day 3. Again buy on day 4 and sell on day 6. If the given
array of prices is sorted in decreasing order, then profit cannot be earned at all.
If we are allowed to buy and sell only once, then we can use following algorithm.Maximum difference between
two elements. Here we are allowed to buy and sell multiple times. Following is algorithm for this problem.
1. Find the local minima and store it as starting index. If not exists, return.
2. Find the local maxima. and store it as ending index. If we reach the end, set the end as ending index.
3. Update the solution (Increment count of buy sell pairs)
4. Repeat the above steps if end is not reached.
// solution structure
struct Interval
{
int buy;
int sell;
};
// This function finds the buy sell schedule for maximum profit
void stockBuySell(int price[], int n)
{
// Prices must be given for at least two days
if (n == 1)
return;
// solution vector
Interval sol[n/2 + 1];
329
{
// Find Local Minima. Note that the limit is (n-2) as we are
// comparing present element to the next element.
while ((i < n-1) && (price[i+1] <= price[i]))
i++;
// print solution
if (count == 0)
printf("There is no day when buying the stock will make profit\n");
else
{
for (int i = 0; i < count; i++)
printf("Buy on day: %d\t Sell on day: %d\n", sol[i].buy, sol[i].sell);
}
return;
}
// fucntion call
stockBuySell(price, n);
return 0;
}
Output:
330
Buy on day : 4 Sell on day: 6
Time Complexity: The outer loop runs till i becomes n-1. The inner two loops increment value of i in
every iteration. So overall time complexity is O(n)
This article is compiled by Ashish Anand and reviewed by GeeksforGeeks team. Please write comments if
you find anything incorrect, or you want to share more information about the topic discussed above.
Source
http://www.geeksforgeeks.org/stock-buy-sell/
Category: Arrays
331
Chapter 89
An array contains both positive and negative numbers in random order. Rearrange the array elements so
that positive and negative numbers are placed alternatively. Number of positive and negative numbers need
not be equal. If there are more positive numbers they appear at the end of the array. If there are more
negative numbers, they too appear in the end of the array.
For example, if the input array is [-1, 2, -3, 4, 5, 6, -7, 8, 9], then the output should be [9, -7, 8, -3, 5, -1, 2,
4, 6]
The solution is to first separate positive and negative numbers using partition process of QuickSort. In the
partition process, consider 0 as value of pivot element so that all negative numbers are placed before positive
numbers. Once negative and positive numbers are separated, we start from the first negative number and
first positive number, and swap every alternate negative number with next positive number.
332
i++;
swap(&arr[i], &arr[j]);
}
}
Output:
4 -3 5 -1 6 -7 2 8 9
333
This article is compiled by Abhay Rathi. Please write comments if you find anything incorrect, or you
want to share more information about the topic discussed above.
If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to
contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other
Geeks.
Source
http://www.geeksforgeeks.org/rearrange-positive-and-negative-numbers-publish/
Category: Arrays
Post navigation
← Amazon Interview | Set 24 HCL Interview | Set 1 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
334
Chapter 90
Given an array of integers, sort the array according to frequency of elements. For example, if the input array
is {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12}, then modify the array to {3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}.
In the previous post, we have discussed all methods for sorting according to frequency. In this post, method
2 is discussed in detail and C++ implementation for the same is provided.
Following is detailed algorithm.
1) Create a BST and while creating BST maintain the count i,e frequency of each coming element in same
BST. This step may take O(nLogn) time if a self balancing BST is used.
2) Do Inorder traversal of BST and store every element and count of each element in an auxiliary array. Let
us call the auxiliary array as ‘count[]’. Note that every element of this array is element and frequency pair.
This step takes O(n) time.
3) Sort ‘count[]’ according to frequency of the elements. This step takes O(nLohn) time if a O(nLogn) sorting
algorithm is used.
4) Traverse through the sorted array ‘count[]’. For each element x, print it ‘freq’ times where ‘freq’ is
frequency of x. This step takes O(n) time.
Overall time complexity of the algorithm can be minimum O(nLogn) if we use a O(nLogn) sorting algorithm
and use a self balancing BST with O(Logn) insert operation.
Following is C++ implementation of the above algorithm.
335
int data;
int freq;
};
/* Helper function that allocates a new node with the given data,
frequency as 1 and NULL left and right pointers.*/
BSTNode* newNode(int data)
{
struct BSTNode* node = new BSTNode;
node->data = data;
node->left = NULL;
node->right = NULL;
node->freq = 1;
return (node);
}
336
}
Output:
3 3 3 3 2 2 2 12 12 5 4
337
Exercise:
The above implementation doesn’t guarantee original order of elements with same frequency (for example,
4 comes before 5 in input, but 4 comes after 5 in output). Extend the implementation to maintain original
order. For example, if two elements have same frequency then print the one which came 1st in input array.
This article is compiled by Chandra Prakash. Please write comments if you find anything incorrect, or
you want to share more information about the topic discussed above
Source
http://www.geeksforgeeks.org/sort-elements-by-frequency-set-2/
Category: Arrays
Post navigation
← Amazon Interview | Set 28 Amazon Interview | Set 29 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
338
Chapter 91
Given an array of integers. Find a peak element in it. An array element is peak if it is NOT smaller than
its neighbors. For corner elements, we need to consider only one neighbor. For example, for input array
{5, 10, 20, 15}, 20 is the only peak element. For input array {10, 20, 15, 2, 23, 90, 67}, there are two peak
elements: 20 and 90. Note that we need to return any one peak element.
Following corner cases give better idea about the problem.
1) If input array is sorted in strictly increasing order, the last element is always a peak element. For example,
50 is peak element in {10, 20, 30, 40, 50}.
2) If input array is sorted in strictly decreasing order, the first element is always a peak element. 100 is the
peak element in {100, 80, 60, 50, 20}.
3) If all elements of input array are same, every element is a peak element.
It is clear from above examples that there is always a peak element in input array in any input array.
A simple solution is to do a linear scan of array and as soon as we find a peak element, we return it. The
worst case time complexity of this method would be O(n).
Can we find a peak element in worst time complexity better than O(n)?
We can use Divide and Conquerto find a peak in O(Logn) time. The idea is Binary Search based, we compare
middle element with its neighbors. If middle element is greater than both of its neighbors, then we return
it. If the middle element is smaller than the its left neighbor, then there is always a peak in left half (Why?
take few examples). If the middle element is smaller than the its right neighbor, then there is always a peak
in right half (due to same reason as left half). Following is C implementation of this approach.
// If middle element is not peak and its left neighbor is greater than it
339
// then left half must have a peak element
else if (mid > 0 && arr[mid-1] > arr[mid])
return findPeakUtil(arr, low, (mid -1), n);
// If middle element is not peak and its right neighbor is greater than it
// then right half must have a peak element
else return findPeakUtil(arr, (mid + 1), high, n);
}
Output:
Source
http://www.geeksforgeeks.org/find-a-peak-in-a-given-array/
340
Chapter 92
Given an array of size n, generate and print all possible combinations of r elements in array. For example, if
input array is {1, 2, 3, 4} and r is 2, then output should be {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4} and {3, 4}.
Following are two methods to do this.
Method 1 (Fix Elements and Recur)
We create a temporary array ‘data[]’ which stores all outputs one by one. The idea is to start from first index
(index = 0) in data[], one by one fix elements at this index and recur for remaining indexes. Let the input
array be {1, 2, 3, 4, 5} and r be 3. We first fix 1 at index 0 in data[], then recur for remaining indexes, then
we fix 2 at index 0 and recur. Finally, we fix 3 and recur for remaining indexes. When number of elements
in data[] becomes equal to r (size of a combination), we print data[].
Following diagram shows recursion tree for same input.
341
// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[r];
Output:
1 2 3
1 2 4
1 2 5
1 3 4
342
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
343
data[] ---> Temporary array to store current combination
i ---> index of current element in arr[] */
void combinationUtil(int arr[], int n, int r, int index, int data[], int i)
{
// Current cobination is ready, print it
if (index == r)
{
for (int j=0; j<r; j++)
printf("%d ",data[j]);
printf("\n");
return;
}
Output:
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
344
// Since the elements are sorted, all occurrences of an element
// must be together
while (arr[i] == arr[i+1])
i++;
Source
http://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/
Category: Arrays Tags: MathematicalAlgo, Recursion
Post navigation
← Fab.com Pune Interview Experience Goldman Sachs Interview Experience →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
345
Chapter 93
Given an array of size n, find all elements in array that appear more than n/k times. For example, if the
input arrays is {3, 1, 2, 2, 1, 2, 3, 3} and k is 4, then the output should be [2, 3]. Note that size of array is 8
(or n = 8), so we need to find all elements that appear more than 2 (or 8/4) times. There are two elements
that appear more than two times, 2 and 3.
A simple method is to pick all elements one by one. For every picked element, count its occurrences by
traversing the array, if count becomes more than n/k, then print the element. Time Complexity of this
method would be O(n2 ).
A better solution is to use sorting. First, sort all elements using a O(nLogn) algorithm. Once the array is
sorted, we can find all required elements in a linear scan of array. So overall time complexity of this method
is O(nLogn) + O(n) which is O(nLogn).
Following is an interesting O(nk) solution:
We can solve the above problem in O(nk) time using O(k-1) extra space. Note that there can never be more
than k-1 elements in output (Why?). There are mainly three steps in this algorithm.
1) Create a temporary array of size (k-1) to store elements and their counts (The output elements are going
to be among these k-1 elements). Following is structure of temporary array elements.
struct eleCount {
int element;
int count;
};
struct eleCount temp[];
346
The main step is step 2, how to maintain (k-1) potential candidates at every point? The steps used in step 2
are like famous game: Tetris. We treat each number as a piece in Tetris, which falls down in our temporary
array temp[]. Our task is to try to keep the same number stacked on the same column (count in temporary
array is incremented).
Consider k = 4, n = 9
Given array: 3 1 2 2 2 1 4 3 3
i = 0
3 _ _
temp[] has one element, 3 with count 1
i = 1
3 1 _
temp[] has two elements, 3 and 1 with
counts 1 and 1 respectively
i = 2
3 1 2
temp[] has three elements, 3, 1 and 2 with
counts as 1, 1 and 1 respectively.
i = 3
- - 2
3 1 2
temp[] has three elements, 3, 1 and 2 with
counts as 1, 1 and 2 respectively.
i = 4
- - 2
- - 2
3 1 2
temp[] has three elements, 3, 1 and 2 with
counts as 1, 1 and 3 respectively.
i = 5
- - 2
- 1 2
3 1 2
temp[] has three elements, 3, 1 and 2 with
counts as 1, 2 and 3 respectively.
Now the question arises, what to do when temp[] is full and we see a new element – we remove the bottom
row from stacks of elements, i.e., we decrease count of every element by 1 in temp[]. We ignore the current
element.
i = 6
- - 2
- 1 2
temp[] has two elements, 1 and 2 with
counts as 1 and 2 respectively.
347
i = 7
- 2
3 1 2
temp[] has three elements, 3, 1 and 2 with
counts as 1, 1 and 2 respectively.
i = 8
3 - 2
3 1 2
temp[] has three elements, 3, 1 and 2 with
counts as 2, 1 and 2 respectively.
Finally, we have at most k-1 numbers in temp[]. The elements in temp are {3, 1, 2}. Note that the counts
in temp[] are useless now, the counts were needed only in step 2. Now we need to check whether the actual
counts of elements in temp[] are more than n/k (9/4) or not. The elements 3 and 2 have counts more than
9/4. So we print 3 and 2.
Note that the algorithm doesn’t miss any output element. There can be two possibilities, many occurrences
are together or spread across the array. If occurrences are together, then count will be high and won’t become
0. If occurrences are spread, then the element would come again in temp[]. Following is C++ implementation
of above algorithm.
348
/* If arr[i] is already present in
the element count array, then increment its count */
for (j=0; j<k-1; j++)
{
if (temp[j].e == arr[i])
{
temp[j].c += 1;
break;
}
}
349
int main()
{
cout << "First Test\n";
int arr1[] = {4, 5, 6, 7, 8, 4, 4};
int size = sizeof(arr1)/sizeof(arr1[0]);
int k = 3;
moreThanNdK(arr1, size, k);
return 0;
}
Output:
First Test
Number:4 Count:3
Second Test
Number:2 Count:2
Third Test
Number:2 Count:2
Fourth Test
Number:2 Count:2
Number:3 Count:2
350
Exercise:
The above problem can be solved in O(nLogk) time with the help of more appropriate data structures than
array for auxiliary storage of k-1 elements. Suggest a O(nLogk) approach.
This article is contributed by Kushagra Jaiswal. Please write comments if you find anything incorrect, or
you want to share more information about the topic discussed above
Source
http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-than-nk-times/
Category: Arrays
Post navigation
← Goldman Sachs Interview Experience Adobe Interview | Set 5 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
351
Chapter 94
Given a function ‘int f(unsigned int x)’ which takes a non-negative integer ‘x’ as input and returns an
integer as output. The function is monotonically increasing with respect to value of x, i.e., the value of
f(x+1) is greater than f(x) for every input x. Find the value ‘n’ where f() becomes positive for the first time.
Since f() is monotonically increasing, values of f(n+1), f(n+2),… must be positive and values of f(n-2), f(n-3),
.. must be negative.
Find n in O(logn) time, you may assume that f(x) can be evaluated in O(1) time for any input x.
A simple solution is to start from i equals to 0 and one by one calculate value of f(i) for 1, 2, 3, 4 .. etc
until we find a positive f(i). This works, but takes O(n) time.
Can we apply Binary Search to find n in O(Logn) time? We can’t directly apply Binary Search as
we don’t have an upper limit or high index. The idea is to do repeated doubling until we find a positive
value, i.e., check values of f() for following values until f(i) becomes positive.
f(0)
f(1)
f(2)
f(4)
f(8)
f(16)
f(32)
....
....
f(high)
Let 'high' be the value of i when f() becomes positive for first time.
Can we apply Binary Search to find n after finding ‘high’? We can apply Binary Search now, we can use
‘high/2� as low and ‘high’ as high indexes in binary search. The result n must lie between ‘high/2� and ‘high’.
Number of steps for finding ‘high’ is O(Logn). So we can find ‘high’ in O(Logn) time. What about time
taken by Binary Search between high/2 and high? The value of ‘high’ must be less than 2*n. The number
352
of elements between high/2 and high must be O(n). Therefore, time complexity of Binary Search is O(Logn)
and overall time complexity is 2*O(Logn) which is O(Logn).
#include <stdio.h>
int binarySearch(int low, int high); // prototype
// Searches first positive value of f(i) where low <= i <= high
int binarySearch(int low, int high)
{
if (high >= low)
{
int mid = low + (high - low)/2; /* mid = (low + high)/2 */
353
int main()
{
printf("The value n where f() becomes positive first is %d",
findFirstPositive());
return 0;
}
Output:
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above
Source
http://www.geeksforgeeks.org/find-the-point-where-a-function-becomes-negative/
354
Chapter 95
Given a sequence of non-negative integers, find the subsequence of length 3 having maximum product with
the numbers of the subsequence being in ascending order.
Examples:
Input:
arr[] = {6, 7, 8, 1, 2, 3, 9, 10}
Output:
8 9 10
Input:
arr[] = {1, 5, 10, 8, 9}
Output: 5 8 9
Since we want to find the maximum product, we need to find following two things for every element in the
given sequence:
LSL: The largest smaller element on left of given element
LGR: The largest greater element on right of given element.
Once we find LSL and LGR for an element, we can find the product of element with its LSL and LGR (if
they both exist). We calculate this product for every element and return maximum of all products.
A simple method is to use nested loops. The outer loop traverses every element in sequence. Inside the
outer loop, run two inner loops (one after other) to find LSL and LGR of current element. Time complexity
of this method is O(n2 ).
We can do this in O(nLogn) time. For simplicity, let us first create two arrays LSL[] and LGR[] of size
n each where n is number of elements in input array arr[]. The main task is to fill two arrays LSL[] and
LGR[]. Once we have these two arrays filled, all we need to find maximum product LSL[i]*arr[i]*LGR[i]
where 0 fill LSL[] in O(nLogn) time. The idea is to use a Balanced Binary Search Tree like AVL. We start
with empty AVL tree, insert the leftmost element in it. Then we traverse the input array starting from the
second element to second last element. For every element currently being traversed, we find the floor of it
in AVL tree. If floor exists, we store the floor in LSL[], otherwise we store NIL. After storing the floor, we
insert the current element in the AVL tree.
355
We can fill LGR[] in O(n) time. The idea is similar to thispost. We traverse from right side and keep track
of the largest element. If the largest element is greater than current element, we store it in LGR[], otherwise
we store NIL.
Finally, we run a O(n) loop and return maximum of LSL[i]*arr[i]*LGR[i]
Overall complexity of this approach is O(nLogn) + O(n) + O(n) which is O(nLogn). Auxiliary space required
is O(n). Note that we can avoid space required for LSL, we can find and use LSL values in final loop.
This article is contributed byAmit Jain. Please write comments if you find anything incorrect, or you want
to share more information about the topic discussed above.
Source
http://www.geeksforgeeks.org/increasing-subsequence-of-length-three-with-maximum-product/
356
Chapter 96
A sorted array is rotated at some unknown point, find the minimum element in it.
Following solution assumes that all elements are distinct.
Examples
Input: {5, 6, 1, 2, 3, 4}
Output: 1
Input: {1, 2, 3, 4}
Output: 1
Input: {2, 1}
Output: 1
A simple solution is to traverse the complete array and find minimum. This solution requires Θ(n) time.
We can do it in O(Logn) using Binary Search. If we take a closer look at above examples, we can easily figure
out following pattern: The minimum element is the only element whose previous element is greater than it.
If there is no such element, then there is no rotation and first element is the minimum element. Therefore,
we do binary search for an element which is smaller than the previous element. We strongly recommend you
to try it yourself before seeing the following C implementation.
357
// Find mid
int mid = low + (high - low)/2; /*(low + high)/2;*/
358
int n9 = sizeof(arr9)/sizeof(arr9[0]);
printf("The minimum element is %d\n", findMin(arr9, 0, n9-1));
return 0;
}
Output:
// Find mid
int mid = low + (high - low)/2; /*(low + high)/2;*/
359
// Check if mid itself is minimum element
if (mid > low && arr[mid] < arr[mid - 1])
return arr[mid];
return 0;
}
Output:
360
This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you
want to share more information about the topic discussed above.
Source
http://www.geeksforgeeks.org/find-minimum-element-in-a-sorted-and-rotated-array/
Category: Arrays Tags: Divide and Conquer
Post navigation
← Print ancestors of a given binary tree node without recursion Ford-Fulkerson Algorithm for Maximum
Flow Problem →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
361
Chapter 97
Given N men and N women, where each person has ranked all members of the opposite sex in order of
preference, marry the men and women together such that there are no two people of opposite sex who would
both rather have each other than their current partners. If there are no such people, all the marriages are
“stable” (Source Wiki).
Consider the following example.
Let there be two men m1 and m2 and two women w1 and w2.
Let m1‘s list of preferences be {w1, w2}
Let m2‘s list of preferences be {w1, w2}
Let w1‘s list of preferences be {m1, m2}
Let w2‘s list of preferences be {m1, m2}
The matching { {m1, w2}, {w1, m2} } is not stable because m1 and w1 would prefer each other over their
assigned partners. The matching {m1, w1} and {m2, w2} is stable because there are no two people of
opposite sex that would prefer each other over their assigned partners.
It is always possible to form stable marriages from lists of preferences (See references for proof). Following
is Gale–Shapley algorithm to find a stable matching:
The idea is to iterate through all free men while there is any free man available. Every free man goes to all
women in his preference list according to the order. For every woman he goes to, he checks if the woman is
free, if yes, they both become engaged. If the woman is not free, then the woman chooses either says no to
him or dumps her current engagement according to her preference list. So an engagement done once can be
broken if a woman gets better option.
Following is complete algorithm from Wiki
362
Input & Output: Input is a 2D matrix of size (2*N)*N where N is number of women or men. Rows from
0 to N-1 represent preference lists of men and rows from N to 2*N – 1 represent preference lists of women.
So men are numbered from 0 to N-1 and women are numbered from N to 2*N – 1. The output is list of
married pairs.
Following is C++ implementation of the above algorithm.
// This function returns true if woman 'w' prefers man 'm1' over man 'm'
bool wPrefersM1OverM(int prefer[2*N][N], int w, int m, int m1)
{
// Check if w prefers m over her current engagment m1
for (int i = 0; i < N; i++)
{
// If m1 comes before m in lisr of w, then w prefers her
// cirrent engagement, don't do anything
if (prefer[w][i] == m1)
return true;
// Prints stable matching for N boys and N girls. Boys are numbered as 0 to
// N-1. Girls are numbereed as N to 2N-1.
void stableMarriage(int prefer[2*N][N])
{
// Stores partner of women. This is our output array that
// stores paing information. The value of wPartner[i]
// indicates the partner assigned to woman N+i. Note that
// the woman numbers between N and 2*N-1. The value -1
// indicates that (N+i)'th woman is free
int wPartner[N];
363
// While there are free men
while (freeCount > 0)
{
// Pick the first free man (we could pick any)
int m;
for (m = 0; m < N; m++)
if (mFree[m] == false)
break;
364
{5, 4, 6, 7},
{4, 5, 6, 7},
{4, 5, 6, 7},
{0, 1, 2, 3},
{0, 1, 2, 3},
{0, 1, 2, 3},
{0, 1, 2, 3},
};
stableMarriage(prefer);
return 0;
}
Output:
Woman Man
4 2
5 1
6 3
7 0
References:
http://www.csee.wvu.edu/~ksmani/courses/fa01/random/lecnotes/lecture5.pdf
http://www.youtube.com/watch?v=5RSMLgy06Ew#t=11m4s
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above
Source
http://www.geeksforgeeks.org/stable-marriage-problem/
Category: Arrays
Post navigation
← Find minimum s-t cut in a flow network Convex Hull | Set 2 (Graham Scan) →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
365
Chapter 98
Given k sorted arrays of size n each, merge them and print the sorted output.
Example:
Input:
k = 3, n = 4
arr[][] = { {1, 3, 5, 7},
{2, 4, 6, 8},
{0, 9, 10, 11}} ;
Output: 0 1 2 3 4 5 6 7 8 9 10 11
A simple solution is to create an output array of size n*k and one by one copy all arrays to it. Finally, sort
the output array using any O(nLogn) sorting algorithm. This approach takes O(nkLognk) time.
We can merge arrays in O(nk*Logk) time using Min Heap. Following is detailed algorithm.
1. Create an output array of size n*k.
2. Create a min heap of size k and insert 1st element in all the arrays into a the heap
3. Repeat following steps n*k times.
a) Get minimum element from heap (minimum is always at root) and store it in output array.
b) Replace heap root with next element from the array from which the element is extracted. If the array
doesn’t have any more elements, then replace root with infinite. After replacing the root, heapify the tree.
Following is C++ implementation of the above algorithm.
#define n 4
366
int i; // index of the array from which the element is taken
int j; // index of the next element to be picked from array
};
367
// Get the minimum element and store it in output
MinHeapNode root = hp.getMin();
output[count] = root.element;
return output;
}
368
// A utility function to swap two elements
void swap(MinHeapNode *x, MinHeapNode *y)
{
MinHeapNode temp = *x; *x = *y; *y = temp;
}
return 0;
}
Output:
Merged array is
1 2 6 9 12 20 23 34 34 90 1000 2000
Time Complexity: The main step is 3rd step, the loop runs n*k times. In every iteration of loop, we call
heapify which takes O(Logk) time. Therefore, the time complexity is O(nk Logk).
There are other interesting methods to merge k sorted arrays in O(nkLogk), we will sonn be discussing them
as separate posts.
Thanks to vigneshfor suggesting this problem and initial solution. Please write comments if you find anything
incorrect, or you want to share more information about the topic discussed above
Source
http://www.geeksforgeeks.org/merge-k-sorted-arrays/
Category: Arrays
Post navigation
369
← Amazon Interview | Set 35 When does compiler create default and copy constructors in C++? →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
370
Chapter 99
Radix Sort
The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort .. etc) is
Ω(nLogn), i.e., they cannot do better than nLogn.
Counting sort is a linear tine sorting algorithm that sort in O(n+k) time when elements are in range from 1
to k.
What if the elements are in range from 1 to n2 ?
We can’t use counting sort because counting sort will take O(n2 ) which is worse than comparison based
sorting algorithms. Can we sort such an array in linear time?
Radix Sort is the answer. The idea of Radix Sort is to do digit by digit sort starting from least significant
digit to most significant digit. Radix sort uses counting sort as a subroutine to sort.
The Radix Sort Algorithm
1) Do following for each digit i where i varies from least significant digit to the most significant digit.
………….a) Sort input array using counting sort (or any stable sort) according to the i’th digit.
Example:
Original, unsorted list:
170, 45, 75, 90, 802, 24, 2, 66
Sorting by least significant digit (1s place) gives: [*Notice that we keep 802 before 2, because 802 occurred
before 2 in the original list, and similarly for pairs 170 & 90 and 45 & 75.]
170, 90, 802, 2, 24, 45, 75, 66
Sorting by next digit (10s place) gives: [*Notice that 802 again comes before 2 as 802 comes before 2 in the
previous list.]
802, 2, 24, 45, 66, 170, 75, 90
Sorting by most significant digit (100s place) gives:
2, 24, 45, 66, 75, 90, 170, 802
What is the running time of Radix Sort?
Let there be d digits in input integers. Radix Sort takes O(d*(n+b)) time where b is the base for representing
numbers, for example, for decimal system, b is 10. What is the value of d? If k is the maximum possible
value, then d would be O(logb (k)). So overall time complexity is O((n+b) * logb (k)). Which looks more than
the time complexity of comparison based sorting algorithms for a large k. Let us first limit k. Let k c where
c is a constant. In that case, the complexity becomes O(nLogb (n)). But it still doesn’t beat comparison
based sorting algorithms.
What if we make value of b larger?. What should be the value of b to make the time complexity linear? If
371
we set b as n, we get the time complexity as O(n). In other words, we can sort an array of integers with
range from 1 to nc if the numbers are represented in base n (or every digit takes log2 (n) bits).
Is Radix Sort preferable to Comparison based sorting algorithms like Quick-Sort?
If we have log2 n bits for every digit, the running time of Radix appears to be better than Quick Sort for a
wide range of input numbers. The constant factors hidden in asymptotic notation are higher for Radix Sort
and Quick-Sort uses hardware caches more effectively. Also, Radix sort uses counting sort as a subroutine
and counting sort takes extra space to sort numbers.
Implementation of Radix Sort
Following is a simple C++ implementation of Radix Sort. For simplicity, the value of d is assumed to be 10.
We recommend you to see Counting Sort for details of countSort() function in below code.
372
}
// The main function to that sorts arr[] of size n using Radix Sort
void radixsort(int arr[], int n)
{
// Find the maximum number to know number of digits
int m = getMax(arr, n);
// Do counting sort for every digit. Note that instead of passing digit
// number, exp is passed. exp is 10^i where i is current digit number
for (int exp = 1; m/exp > 0; exp *= 10)
countSort(arr, n, exp);
}
Output:
2 24 45 66 75 90 170 802
References:
http://en.wikipedia.org/wiki/Radix_sort
http://alg12.wikischolars.columbia.edu/file/view/RADIX.pdf
MIT Video Lecture
Introduction to Algorithms 3rd Edition by Clifford Stein, Thomas H. Cormen, Charles E. Leiserson, Ronald
L. Rivest
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above
Source
http://www.geeksforgeeks.org/radix-sort/
Category: Arrays
Post navigation
373
← [TopTalent.in] Rubal Chadha on playfully working in EA – A Gamer’s Paradise B-Tree | Set 3 (Delete)
→
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
374
Chapter 100
Given an array of random numbers, Push all the zero’s of a given array to the end of the array. For example,
if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}.
The order of all other elements should be same. Expected time complexity is O(n) and extra space is O(1).
There can be many ways to solve this problem. Following is a simple and interesting way to solve this
problem.
Traverse the given array ‘arr’ from left to right. While traversing, maintain count of non-zero elements
in array. Let the count be ‘count’. For every non-zero element arr[i], put the element at ‘arr[count]’ and
increment ‘count’. After complete traversal, all non-zero elements have already been shifted to front end and
‘count’ is set as index of first 0. Now all we need to do is that run a loop which makes all elements zero from
‘count’ till end of the array.
Below is C++ implementation of the above approach.
// Now all non-zero elements have been shifted to front and 'count' is
// set as index of first 0. Make all elements 0 from count to end.
while (count < n)
arr[count++] = 0;
}
375
int arr[] = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9};
int n = sizeof(arr) / sizeof(arr[0]);
pushZerosToEnd(arr, n);
cout << "Array after pushing all zeros to end of array :\n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Output:
Source
http://www.geeksforgeeks.org/move-zeroes-end-array/
376
Chapter 101
Given two arrays X[] and Y[] of positive integers, find number of pairs such that xˆy > yˆx where x is an
element from X[] and y is an element from Y[].
Examples:
The brute force solution is to consider each element of X[] and Y[], and check whether the given condition
satisfies or not. Time Complexity of this solution is O(m*n) where m and n are sizes of given arrays.
Following is C++ code based on brute force solution.
377
Efficient Solution:
The problem can be solved in O(nLogn + mLogn) time. The trick here is, if y > x then xˆy > yˆx with
some exceptions. Following are simple steps based on this trick.
1) Sort array Y[].
2) For every x in X[], find the index idx of smallest number greater than x (also called ceil of x) in Y[] using
binary search or we can use the inbuilt function upper_bound() in algorithm library.
3) All the numbers after idx satisfy the relation so just add (n-idx) to the count.
Base Cases and Exceptions:
Following are exceptions for x from X[] and y from Y[]
If x = 0, then the count of pairs for this x is 0.
If x = 1, then the count of pairs for this x is equal to count of 0s in Y[].
The following cases must be handled separately as they don’t follow the general rule that x smaller than y
means xˆy is greater than yˆx.
a) x = 2, y = 3 or 4
b) x = 3, y = 2
Note that the case where x = 4 and y = 2 is not there
Following diagram shows all exceptions in tabular form. The value 1 indicates that the corresponding (x, y)
form a valid pair.
Following is C++ implementation. In the following implementation, we pre-process the Y array and count
0, 1, 2, 3 and 4 in it, so that we can handle all exceptions in constant time. The array NoOfY[] is used to
store the counts.
#include<iostream>
#include<algorithm>
using namespace std;
378
// zeroes in Y[]
if (x == 1) return NoOfY[0];
return ans;
}
// The main function that returns count of pairs (x, y) such that
// x belongs to X[], y belongs to Y[] and x^y > y^x
int countPairs(int X[], int Y[], int m, int n)
{
// To store counts of 0, 1, 2, 3 and 4 in array Y
int NoOfY[5] = {0};
for (int i = 0; i < n; i++)
if (Y[i] < 5)
NoOfY[Y[i]]++;
return total_pairs;
}
int m = sizeof(X)/sizeof(X[0]);
int n = sizeof(Y)/sizeof(Y[0]);
379
return 0;
}
Output:
Total pairs = 3
Time Complexity : Let m and n be the sizes of arrays X[] and Y[] respectively. The sort step takes
O(nLogn) time. Then every element of X[] is searched in Y[] using binary search. This step takes O(mLogn)
time. Overall time complexity is O(nLogn + mLogn).
This article is contributed by Shubham Mittal. Please write comments if you find anything incorrect, or
you want to share more information about the topic discussed above.
Source
http://www.geeksforgeeks.org/find-number-pairs-xy-yx/
Category: Arrays
Post navigation
← How to count set bits in a floating point number in C? Analysis of Algorithms | Set 3 (Asymptotic
Notations) →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
380
Chapter 102
Given an integer array and a positive integer k, count all distinct pairs with difference equal to k.
Examples:
Method 1 (Simple)
A simple solution is to consider all pairs one by one and check difference between every pair. Following
program implements the simple solution. We run two loops: the outer loop picks the first element of pair,
the inner loop looks for the other element. This solution doesn’t work if there are duplicates in array as the
requirement is to count only distinct pairs.
381
if (arr[i] - arr[j] == k || arr[j] - arr[i] == k )
count++;
}
return count;
}
Output:
1) Initialize count as 0
2) Sort all numbers in increasing order.
3) Remove duplicates from array.
4) Do following for each element arr[i]
a) Binary Search for arr[i] + k in subarray from i+1 to n-1.
b) If arr[i] + k found, increment count.
5) Return count.
382
else
return binarySearch(arr, low, (mid -1), x);
}
return -1;
}
return count;
}
Output:
Time complexity: The first step (sorting) takes O(nLogn) time. The second step runs binary search n times,
so the time complexity of second step is also O(nLogn). Therefore, overall time complexity is O(nLogn).
The second step can be optimized to O(n), see this.
Method 3 (Use Self-balancing BST)
We can also a self-balancing BST like AVL tree or Red Black tree to solve this problem. Following is detailed
algorithm.
1) Initialize count as 0.
2) Insert all elements of arr[] in an AVL tree. While inserting,
ignore an element if already present in AVL tree.
3) Do following for each element arr[i].
a) Search for arr[i] + k in AVL tree, if found then increment count.
b) Search for arr[i] - k in AVL tree, if found then increment count.
c) Remove arr[i] from AVL tree.
Time complexity of above solution is also O(nLogn) as search and delete operations take O(Logn) time for
a self-balancing binary search tree.
Method 4 (Use Hashing)
We can also use hashing to achieve the average time complexity as O(n) for many cases.
1) Initialize count as 0.
2) Insert all distinct elements of arr[] in a hash map. While inserting,
ignore an element if already present in the hash map.
3) Do following for each element arr[i].
383
a) Look for arr[i] + k in the hash map, if found then increment count.
b) Look for arr[i] - k in the hash map, if found then increment count.
c) Remove arr[i] from hash table.
A very simple case where hashing works in O(n) time is the case where range of values is very small. For
example, in the following implementation, range of numbers is assumed to be 0 to 99999. A simple hashing
technique to use values as index can be used.
• If value diff is K, increment count and move both pointers to next element
• if value diff > k, move l to next element
• if value diff < k, move r to next element
return count
384
/* Returns count of pairs with difference k in arr[] of size n. */
int countPairsWithDiffK(int arr[], int n, int k)
{
int count = 0;
sort(arr, arr+n); // Sort array elements
int l = 0;
int r = 0;
while(r < n)
{
if(arr[r] - arr[l] == k)
{
count++;
l++;
r++;
}
else if(arr[r] - arr[l] > k)
l++;
else // arr[r] - arr[l] < sum
r++;
}
return count;
}
Output:
Source
http://www.geeksforgeeks.org/count-pairs-difference-equal-k/
Category: Arrays
Post navigation
← DELL Interview | Set 1 (On-Campus) How to compare two arrays in Java? →
385
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
386
Chapter 103
Given an array of positive and negative numbers, find if there is a subarray (of size at-least one) with 0 sum.
Examples:
Input: {4, 2, 0, 1, 6}
Output: true
There is a subarray with zero sum from index 2 to 2.
Input: {-3, 2, 3, 1, 6}
Output: false
There is no subarray with zero sum.
We strongly recommend to minimize the browser and try this yourself first.
A simple solution is to consider all subarrays one by one and check the sum of every subarray. We can
run two loops: the outer loop picks a starting point i and the inner loop tries all subarrays starting from i
(See thisfor implementation). Time complexity of this method is O(n2 ).
We can also use hashing. The idea is to iterate through the array and for every element arr[i], calculate
sum of elements form 0 to i (this can simply be done as sum += arr[i]). If the current sum has been seen
before, then there is a zero sum array. Hashing is used to store the sum values, so that we can quickly store
sum and find out whether the current sum is seen before or not.
Following is Java implementation of the above approach.
class ZeroSumSubarray {
387
{
// Creates an empty hashMap hM
HashMap<Integer, Integer> hM = new HashMap<Integer, Integer>();
Output:
Time Complexity of this solution can be considered as O(n) under the assumption that we have good hashing
function that allows insertion and retrieval operations in O(1) time.
Exercise:
Extend the above program to print starting and ending indexes of all subarrays with 0 sum.
This article is contributed by Chirag Gupta. Please write comments if you find anything incorrect, or you
want to share more information about the topic discussed above
Source
http://www.geeksforgeeks.org/find-if-there-is-a-subarray-with-0-sum/
388
Category: Arrays Tags: Hashing
389
Chapter 104
Given an array of integers and a number x, find the smallest subarray with sum greater than the given value.
Examples:
arr[] = {1, 4, 45, 6, 0, 19}
x = 51
Output: 3
Minimum length subarray is {4, 45, 6}
A simple solution is to use two nested loops. The outer loop picks a starting element, the inner loop
considers all elements (on right side of current start) as ending element. Whenever sum of elements between
current start and end becomes more than the given number, update the result if current length is smaller
than the smallest length so far.
Following is C++ implementation of simple approach.
# include <iostream>
using namespace std;
390
int min_len = n + 1;
return 0;
}
Output:
3
1
4
391
Time Complexity: Time complexity of the above approach is clearly O(n2 ).
Efficient Solution: This problem can be solved in O(n) time using the idea used in thispost. Thanks to
Ankit and Nitin for suggesting this optimized solution.
392
int n3 = sizeof(arr3)/sizeof(arr3[0]);
x = 280;
cout << smallestSubWithSum(arr3, n3, x);
return 0;
}
Output:
3
1
4
This article is contributed by Rahul Jain. Please write comments if you find anything incorrect, or you
want to share more information about the topic discussed above
Source
http://www.geeksforgeeks.org/minimum-length-subarray-sum-greater-given-value/
Category: Arrays
Post navigation
← Count trailing zeroes in factorial of a number Amazon Interview | Set 83 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
393
Chapter 105
Given two arrays A1[] and A2[], sort A1 in such a way that the relative order among the elements will be
same as those are in A2. For the elements not present in A2, append them at last in sorted order.
The code should handle all cases like number of elements in A2[] may be more or less compared to A1[]. A2[]
may have some elements which may not be there in A1[] and vice versa is also possible.
Source: Amazon Interview | Set 110 (On-Campus)
We strongly recommend to minimize the browser and try this yourself first.
Method 1 (Using Sorting and Binary Search)
Let size of A1[] be m and size of A2[] be n.
1) Create a temporary array temp of size m and copy contents of A1[] to it.
2) Create another array visited[] and initialize all entries in it as false. visited[] is used to mark those elements
in temp[] which are copied to A1[].
3) Sort temp[]
4) Initialize the output index ind as 0.
5) Do following for every element of A2[i] in A2[]
…..a) Binary search for all occurrences of A2[i] in temp[], if present then copy all occurrences to A1[ind] and
increment ind. Also mark the copied elements visited[]
6) Copy all unvisited elements from temp[] to A1[].
Time complexity: The steps 1 and 2 require O(m) time. Step 3 requires O(mLogm) time. Step 5 requires
O(nLogm) time. Therefore overall time complexity is O(m + nLogm).
Thanks to vivekfor suggesting this method. Following is C++ implementation of above algorithm.
394
using namespace std;
// Now copy all items of temp[] which are not present in A2[]
395
for (int i=0; i<m; i++)
if (visited[i] == 0)
A1[ind++] = temp[i];
}
Output:
Sorted array is
2 2 1 1 8 8 3 5 6 7 9
396
Method 4 (By Writing a Customized Compare Method)
We can also customize compare method of a sorting algorithm to solve the above problem. For example
qsort() in C allows us to pass our own customized compare method.
1. If num1 and num2 both are in A2 then number with lower index in A2 will be treated smaller than other.
2. If only one of num1 or num2 present in A2, then that number will be treated smaller than the other which
doesn’t present in A2.
3. If both are not in A2, then natural ordering will be taken.
Time complexity of this method is O(mnLogm) if we use a O(nLogn) time complexity sorting algorithm. We
can improve time complexity to O(mLogm) by using a Hashing instead of doing linear search.
Following is C implementation of this method.
397
// Driver program to test above function
int main(int argc, char *argv[])
{
int A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8, 7, 5, 6, 9, 7, 5};
sortA1ByA2(A1, size1);
Output:
Sorted Array is 2 2 1 1 8 8 3 5 5 5 6 6 7 7 7 9 9
This method is based on comments by readers (Xinuo Chen, Pranay Doshi and javakurious) and compiled
by Anurag Singh.
This article is compiled by Piyush. Please write comments if you find anything incorrect, or you want to
share more information about the topic discussed above
Source
http://www.geeksforgeeks.org/sort-array-according-order-defined-another-array/
Category: Arrays
Post navigation
← Microsoft Interview | Set 32 (On-Campus for Internship) Amazon Interview | Set 115 (On-Campus) →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
398
Chapter 106
Given two sorted arrays such the arrays may have some common elements. Find the sum of the maximum
sum path to reach from beginning of any array to end of any of the two arrays. We can switch from one
array to another array only at common elements.
Expected time complexity is O(m+n) where m is the number of elements in ar1[] and n is the number of
elements in ar2[].
Examples:
We strongly recommend to minimize the browser and try this yourself first.
The idea is to do something similar to merge process of merge sort. We need to calculate sums of elements
between all common points for both arrays. Whenever we see a common point, we compare the two sums
and add the maximum of two to the result. Following are detailed steps.
1) Initialize result as 0. Also initialize two variables sum1 and sum2 as 0. Here sum1 and sum2 are used to
store sum of element in ar1[] and ar2[] respectively. These sums are between two common points.
2) Now run a loop to traverse elements of both arrays. While traversing compare current elements of ar1[]
and ar2[].
399
2.a) If current element of ar1[] is smaller than current element of ar2[], then update sum1, else if current
element of ar2[] is smaller, then update sum2.
2.b) If current element of ar1[] and ar2[] are same, then take the maximum of sum1 and sum2 and add it
to the result. Also add the common element to the result.
Following is C++ implementation of above approach.
C++
#include<iostream>
using namespace std;
400
}
return result;
}
Python
401
sum2 += ar2[j]
j+=1
# Update sum1 and sum2 for elements after this intersection point
sum1, sum2 = 0, 0
return result
# Driver function
ar1 = [2, 3, 7, 10, 12, 15, 30, 34]
ar2 = [1, 5, 7, 8, 10, 15, 16, 19]
m = len(ar1)
n = len(ar2)
print "Maximum sum path is", maxPathSum(ar1, ar2, m, n)
Output:
Time complexity: In every iteration of while loops, we process an element from either of the two arrays.
There are total m + n elements. Therefore, time complexity is O(m+n).
This article is contributed by Piyush Gupta. Please write comments if you find anything incorrect, or you
want to share more information about the topic discussed above
Source
http://www.geeksforgeeks.org/maximum-sum-path-across-two-arrays/
402
Chapter 107
Given an unsorted array that may contain duplicates. Also given a number k which is smaller than size of
array. Write a function that returns true if array contains duplicates within k distance.
Examples:
A Simple Solution is to run two loops. The outer loop picks every element ‘arr[i]’ as a starting element,
the inner loop compares all elements which are within k distance of ‘arr[i]’. The time complexity of this
solution is O(kn).
We can solve this problem in Θ(n) time using Hashing. The idea is to one by add elements to hash.
We also remove elements which are at more than k distance from current element. Following is detailed
algorithm.
1) Create an empty hashtable.
2) Traverse all elements from left from right. Let the current element be ‘arr[i]’
….a) If current element ‘arr[i]’ is present in hashtable, then return true.
….b) Else add arr[i] to hash and remove arr[i-k] from hash if i is greater than or equal to k
403
/* Java program to Check if a given array contains duplicate
elements within k distance from each other */
import java.util.*;
class Main
{
static boolean checkDuplicatesWithinK(int arr[], int k)
{
// Creates an empty hashset
HashSet<Integer> set = new HashSet<>();
Output:
Yes
This article is contributed by Anuj. Please write comments if you find anything incorrect, or you want to
share more information about the topic discussed above.
Source
http://www.geeksforgeeks.org/check-given-array-contains-duplicate-elements-within-k-distance/
Category: Arrays Tags: Hashing
Post navigation
404
← Zoho Interview | Set 4 Amazon Interview Experience | Set 163 (For SDE II) →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
405
Chapter 108
Given an unsorted array of integers, sort the array into a wave like array. An array ‘arr[0..n-1]’ is sorted in
wave form if arr[0] >= arr[1] = arr[3] = …..
Examples:
We strongly recommend to minimize your browser and try this yourself first.
A Simple Solution is to use sorting. First sort the input array, then swap all adjacent elements.
For example, let the input array be {3, 6, 5, 10, 7, 20}. After sorting, we get {3, 5, 6, 7, 10, 20}. After
swapping adjacent elements, we get {5, 3, 7, 6, 20, 10}.
Below are implementations of this simple approach.
C++
406
using namespace std;
Python
# Driver progrM
arr = [10, 90, 49, 2, 1, 5, 23]
sortInWave(arr, len(arr))
for i in range(0,len(arr)):
print arr[i],
407
# This code is contributed by __Devesh Agrawal__
Output:
2 1 10 5 49 23 90
The time complexity of the above solution is O(nLogn) if a O(nLogn) sorting algorithm like Merge Sort,
Heap Sort, .. etc is used.
This can be done in O(n) time by doing a single traversal of given array. The idea is based on the fact
that if we make sure that all even positioned (at index 0, 2, 4, ..) elements are greater than their adjacent
odd elements, we don’t need to worry about odd positioned element. Following are simple steps.
1) Traverse all even positioned elements of input array, and do following.
….a) If current element is smaller than previous odd element, swap previous and current.
….b) If current element is smaller than next odd element, swap next and current.
Below are implementations of above simple algorithm.
C++
408
int n = sizeof(arr)/sizeof(arr[0]);
sortInWave(arr, n);
for (int i=0; i<n; i++)
cout << arr[i] << " ";
return 0;
}
Python
# Driver program
arr = [10, 90, 49, 2, 1, 5, 23]
sortInWave(arr, len(arr))
for i in range(0,len(arr)):
print arr[i],
Output:
90 10 49 1 5 2 23
This article is contributed by Shivam. Please write comments if you find anything incorrect, or you want
to share more information about the topic discussed above.
Source
http://www.geeksforgeeks.org/sort-array-wave-form-2/
409
Chapter 109
Given an array and a number k where k is smaller than size of array, we need to find the k’th smallest
element in the given array. It is given that ll array elements are distinct.
Examples:
410
}
411
MinHeapify(i);
i--;
}
}
// If there are more than 1 items, move the last item to root
// and call heapify.
if (heap_size > 1)
{
harr[0] = harr[heap_size-1];
MinHeapify(0);
}
heap_size--;
return root;
}
412
{
// Build a heap of n elements: O(n) time
MinHeap mh(arr, n);
// Return root
return mh.getMin();
}
Output:
413
int *harr; // pointer to array of elements in heap
int capacity; // maximum possible size of max heap
int heap_size; // Current number of elements in max heap
public:
MaxHeap(int a[], int size); // Constructor
void maxHeapify(int i); //To maxHeapify subtree rooted with index i
int parent(int i) { return (i-1)/2; }
int left(int i) { return (2*i + 1); }
int right(int i) { return (2*i + 2); }
// If there are more than 1 items, move the last item to root
// and call heapify.
if (heap_size > 1)
{
harr[0] = harr[heap_size-1];
maxHeapify(0);
}
heap_size--;
return root;
}
414
int l = left(i);
int r = right(i);
int largest = i;
if (l < heap_size && harr[l] > harr[i])
largest = l;
if (r < heap_size && harr[r] > harr[largest])
largest = r;
if (largest != i)
{
swap(&harr[i], &harr[largest]);
maxHeapify(largest);
}
}
// Return root
return mh.getMax();
}
Output:
Method 4 (QuickSelect)
This is an optimization over method 1 if QuickSortis used as a sorting algorithm in first step. In QuickSort,
415
we pick a pivot element, then move the pivot element to its correct position and partition the array around
it. The idea is, not to do complete quicksort, but stop at the point where pivot itself is k’th smallest element.
Also, not to recur for both left and right sides of pivot, but recur for one of them according to the position
of pivot. The worst case time complexity of this method is O(n2 ), but it works in O(n) on average.
#include<iostream>
#include<climits>
using namespace std;
// If position is same as k
if (pos-l == k-1)
return arr[pos];
if (pos-l > k-1) // If position is more, recur for left subarray
return kthSmallest(arr, l, pos-1, k);
416
swap(&arr[i], &arr[j]);
i++;
}
}
swap(&arr[i], &arr[r]);
return i;
}
Output:
There are two more solutions which are better than above discussed ones: One solution is to do randomized
version of quickSelect() and other solution is worst case linear time algorithm (see the following posts).
K’th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
K’th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time)
References:
http://www.ics.uci.edu/~eppstein/161/960125.html
http://www.cs.rit.edu/~ib/Classes/CS515_Spring12-13/Slides/022-SelectMasterThm.pdf
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above
Source
http://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array/
Category: Arrays
Post navigation
← Time complexity of insertion sort when there are O(n) inversions? Amazon Interview Experience | Set
151 (For SDE) →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
417
Chapter 110
418
// This function returns k'th smallest element in arr[l..r] using
// QuickSort based method. ASSUMPTION: ALL ELEMENTS IN ARR[] ARE DISTINCT
int kthSmallest(int arr[], int l, int r, int k)
{
// If k is smaller than number of elements in array
if (k > 0 && k <= r - l + 1)
{
// Partition the array around a random element and
// get position of pivot element in sorted array
int pos = randomPartition(arr, l, r);
// If position is same as k
if (pos-l == k-1)
return arr[pos];
if (pos-l > k-1) // If position is more, recur for left subarray
return kthSmallest(arr, l, pos-1, k);
419
{
int n = r-l+1;
int pivot = rand() % n;
swap(&arr[l + pivot], &arr[r]);
return partition(arr, l, r);
}
Output:
Time Complexity:
The worst case time complexity of the above solution is still O(n2 ). In worst case, the randomized function
may always pick a corner element. The expected time complexity of above randomized QuickSelect is Θ(n),
see CLRS book or MIT video lecture for proof. The assumption in the analysis is, random number generator
is equally likely to generate any number in the input range.
Sources:
Source
http://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array-set-2-expected-linear-time/
Category: Arrays
Post navigation
← Amazon Interview Experience | Set 151 (For SDE) Amazon Interview Experience | Set 152 →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
420
Chapter 111
In previous post, we discussed an expected linear time algorithm. In this post, a worst case linear time
method is discussed. The idea in this new method is similar to quickSelect(), we get worst case linear time by
selecting a pivot that divides array in a balanced way (there are not very few elements on one side and many
on other side). After the array is divided in a balanced way, we apply the same steps as used in quickSelect()
to decide whether to go left or right of pivot.
Following is complete algorithm.
kthSmallest(arr[0..n-1], k)
1) Divide arr[] into �n/5rceil; groups where size of each group is 5
except possibly the last group which may have less than 5 elements.
421
of all �n/5� groups in this median array.
In above algorithm, last 3 steps are same as algorithm in previous post. The first four steps are used to
obtain a good point for partitioning the array (to make sure that there are not too many elements either
side of pivot).
Following is C++ implementation of above algorithm.
422
}
// If position is same as k
if (pos-l == k-1)
return arr[pos];
if (pos-l > k-1) // If position is more, recur for left
return kthSmallest(arr, l, pos-1, k);
423
swap(&arr[i], &arr[r]);
return i;
}
Output:
Time Complexity:
The worst case time complexity of the above algorithm is O(n). Let us analyze all steps.
The steps 1) and 2) take O(n) time as finding median of an array of size 5 takes O(1) time and there are
n/5 arrays of size 5.
The step 3) takes T(n/5) time. The step 4 is standard partition and takes O(n) time.
The interesting steps are 6) and 7). At most, one of them is executed. These are recursive steps. What
is the worst case size of these recursive calls. The answer is maximum number of elements greater than
medOfMed (obtained in step 3) or maximum number of elements smaller than medOfMed.
How many elements are greater than medOfMed and how many are smaller?
At least half of the medians found in step 2 are greater than or equal to medOfMed. Thus, at
least half of the n/5 groups contribute 3 elements that are greater than medOfMed, except for the one
group that has fewer than 5 elements. Therefore, the number of elements greater than medOfMed is at least.
Similarly, the number of elements that are less than medOfMed is at least 3n/10 – 6. In the worst case, the
function recurs for at most n – (3n/10 – 6) which is 7n/10 + 6 elements.
Note that 7n/10 + 6 20 and that any input of 80 or fewer elements requires O(1) time. We can therefore
obtain the recurrence
We show that the running time is linear by substitution. Assume that T(n) cn for some constant c and all
n > 80. Substituting this inductive hypothesis into the right-hand side of the recurrence yields
T(n)
since we can pick c large enough so that c(n/10 - 7) is larger than the function described by the O(n) term for
Note that the above algorithm is linear in worst case, but the constants are very high for this algorithm. The
Sources:
424
MIT Video Lecture on Order Statistics, Median
http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap10.htm
This article is contributed by Shivam. Please write comments if you find anything incorrect, or you want to sh
Source
http://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array-set-3-worst-case-linear-time/
Category: Arrays
Post navigation
← Check a given sentence for a given set of simple grammer rules [TopTalent.in] Interview With Faraz Who
Got Into MobiKwik →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
425
Chapter 112
Given an array of 0s and 1s, find the position of 0 to be replaced with 1 to get longest continuous sequence
of 1s. Expected time complexity is O(n) and auxiliary space is O(1).
Example:
Input:
arr[] = {1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1}
Output:
Index 9
Assuming array index starts from 0, replacing 0 with 1 at index 9 causes
the maximum continuous sequence of 1s.
Input:
arr[] = {1, 1, 1, 1, 0}
Output:
Index 4
We strongly recommend to minimize the browser and try this yourself first.
A Simple Solution is to traverse the array, for every 0, count the number of 1s on both sides of it. Keep
track of maximum count for any 0. Finally return index of the 0 with maximum number of 1s around it.
The time complexity of this solution is O(n2 ).
Using an Efficient Solution, the problem can solved in O(n) time. The idea is to keep track of three indexes,
current index (curr), previous zero index (prev_zero) and previous to previous zero index (prev_prev_zero).
Traverse the array, if current element is 0, calculate the difference between curr and prev_prev_zero (This
difference minus one is the number of 1s around the prev_zero). If the difference between curr and
prev_prev_zero is more than maximum so far, then update the maximum. Finally return index of the
prev_zero with maximum difference.
Following is C++ implementation of the above algorithm.
426
#include<iostream>
using namespace std;
return max_index;
}
// Driver program
int main()
{
bool arr[] = {1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Index of 0 to be replaced is "
<< maxOnesIndex(arr, n);
return 0;
}
Output:
Index of 0 to be replaced is 9
427
Time Complexity: O(n)
Auxiliary Space: O(1)
This article is contributed by Ankur Singh. Please write comments if you find anything incorrect, or you
want to share more information about the topic discussed above.
Source
http://www.geeksforgeeks.org/find-index-0-replaced-1-get-longest-continuous-sequence-1s-binary-array/
Category: Arrays
Post navigation
← Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
Check a given sentence for a given set of simple grammer rules →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
428
Chapter 113
Given two sorted arrays and a number x, find the pair whose sum is closest to x and the pair has an
element from each array.
We are given two arrays ar1[0…m-1] and ar2[0..n-1] and a number x, we need to find the pair ar1[i] + ar2[j]
such that absolute value of (ar1[i] + ar2[j] – x) is minimum.
Example:
We strongly recommend to minimize your browser and try this yourself first.
A Simple Solution is to run two loops. The outer loop considers every element of first array and inner
loop checks for the pair in second array. We keep track of minimum difference between ar1[i] + ar2[j] and x.
We can do it in O(n) time using following steps.
1) Merge given two arrays into an auxiliary array of size m+n using merge process of merge sort. While
merging keep another boolean array of size m+n to indicate whether the current element in merged array is
from ar1[] or ar2[].
2) Consider the merged array and use the linear time algorithm to find the pair with sum closest to x. One
extra thing we need to consider only those pairs which have one element from ar1[] and other from ar2[], we
use the boolean array for this purpose.
Can we do it in a single pass and O(1) extra space?
The idea is to start from left side of one array and right side of another array, and use the algorithm same
as step 2 of above approach. Following is detailed algorithm.
429
1) Initialize a variable diff as infinite (Diff is used to store the
difference between pair and x). We need to find the minimum diff.
2) Initialize two index variables l and r in the given sorted array.
(a) Initialize first to the leftmost index in ar1: l = 0
(b) Initialize second the rightmost index in ar2: r = n-1
3) Loop while l = 0
(a) If abs(ar1[l] + ar2[r] - sum)
Following is C++ implementation of this approach.
// C++ program to find the pair from two sorted arays such
// that the sum of pair is closest to a given number x
#include <iostream>
#include <climits>
#include <cstdlib>
using namespace std;
// res_l and res_r are result indexes from ar1[] and ar2[]
// respectively
int res_l, res_r;
430
// Driver program to test above functions
int main()
{
int ar1[] = {1, 4, 5, 7};
int ar2[] = {10, 20, 30, 40};
int m = sizeof(ar1)/sizeof(ar1[0]);
int n = sizeof(ar2)/sizeof(ar2[0]);
int x = 38;
printClosest(ar1, ar2, m, n, x);
return 0;
}
Output:
This article is contributed by Harsh. Please write comments if you find anything incorrect, or you want to
share more information about the topic discussed above
Source
http://www.geeksforgeeks.org/given-two-sorted-arrays-number-x-find-pair-whose-sum-closest-x/
Category: Arrays
Post navigation
← Adobe Interview | Set 17 (For MTS-1) Multiply two polynomials →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
431
Chapter 114
Given three arrays sorted in non-decreasing order, print all common elements in these arrays.
Examples:
ar1[] = {1, 5, 5}
ar2[] = {3, 4, 5, 5, 10}
ar3[] = {5, 5, 10, 20}
Outptu: 5, 5
A simple solution is to first find intersection of two arraysand store the intersection in a temporary array,
then find the intersection of third array and temporary array. Time complexity of this solution is O(n1 +
n2 + n3) where n1, n2 and n3 are sizes of ar1[], ar2[] and ar3[] respectively.
The above solution requires extra space and two loops, we can find the common elements using a single loop
and without extra space. The idea is similar to intersection of two arrays. Like two arrays loop, we run a
loop and traverse three arrays.
Let the current element traversed in ar1[] be x, in ar2[] be y and in ar3[] be z. We can have following cases
inside the loop.
1) If x, y and z are same, we can simply print any of them as common element and move ahead in all three
arrays.
2) Else If x y and y > z), we can simply move ahead in ar3[] as z cannot be a common element.
Following are implementations of the above idea.
C++
432
// This function prints common elements in ar1
int findCommon(int ar1[], int ar2[], int ar3[], int n1, int n2, int n3)
{
// Initialize starting indexes for ar1[], ar2[] and ar3[]
int i = 0, j = 0, k = 0;
// x < y
else if (ar1[i] < ar2[j])
i++;
// y < z
else if (ar2[j] < ar3[k])
j++;
Python
433
# Iterate through three arrays while all arrays have elements
while (i < n1 and j < n2 and k< n3):
# x < y
elif ar1[i] < ar2[j]:
i += 1
# y < z
elif ar2[j] < ar3[k]:
j += 1
Output:
Time complexity of the above solution is O(n1 + n2 + n3). In worst case, the largest sized array may have
all small elements and middle sized array has all middle elements.
This article is compiled by Rahul Gupta Please write comments if you find anything incorrect, or you want
to share more information about the topic discussed above.
Source
http://www.geeksforgeeks.org/find-common-elements-three-sorted-arrays/
434
Chapter 115
Given an array of integers, find the first repeating element in it. We need to find the element that occurs
more than once and whose index of first occurrence is smallest.
Examples:
A Simple Solution is to use two nested loops. The outer loop picks an element one by one, the inner loop
checks whether the element is repeated or not. Once we find an element that repeats, we break the loops
and print the element. Time Complexity of this solution is O(n2 )
We can Use Sorting to solve the problem in O(nLogn) time. Following are detailed steps.
1) Copy the given array to an auxiliary array temp[].
2) Sort the temp array using a O(nLogn) time sorting algorithm.
3) Scan the input array from left to right. For every element, count its occurrences in temp[] using binary
search. As soon as we find an element that occurs more than once, we return the element. This step can be
done in O(nLogn) time.
We can Use Hashing to solve this in O(n) time on average. The idea is to traverse the given array from
right to left and update the minimum index whenever we find an element that has been visited on right side.
Thanks to Mohammad Shahid for suggesting this solution.
Following is Java implementation of this idea.
class Main
{
// This function prints the first repeating element in arr[]
435
static void printFirstRepeating(int arr[])
{
// Initialize index of first repeating element
int min = -1;
Output:
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Source
http://www.geeksforgeeks.org/find-first-repeating-element-array-integers/
Category: Arrays Tags: Hashing
436
Chapter 116
Given a sorted array (sorted in non-decreasing order) of positive numbers, find the smallest positive integer
value that cannot be represented as sum of elements of any subset of given set.
Expected time complexity is O(n).
Examples:
We strongly recommend to minimize the browser and try this yourself first.
A Simple Solution is to start from value 1 and check all values one by one if they can sum to values in the
given array. This solution is very inefficient as it reduces to subset sum problem which is a well known NP
Complete Problem.
We can solve this problem in O(n) time using a simple loop. Let the input array be arr[0..n-1]. We initialize
the result as 1 (smallest possible outcome) and traverse the given array. Let the smallest element that cannot
be represented by elements at indexes from 0 to (i-1) be ‘res’, there are following two possibilities when we
consider element at index i:
437
1) We decide that ‘res’ is the final result: If arr[i] is greater than ‘res’, then we found the gap which
is ‘res’ because the elements after arr[i] are also going to be greater than ‘res’.
2) The value of ‘res’ is incremented after considering arr[i]: The value of ‘res’ is incremented by
arr[i] (why? If elements from 0 to (i-1) can represent 1 to ‘res-1�, then elements from 0 to i can represent
from 1 to ‘res + arr[i] – 1� be adding ‘arr[i]’ to all subsets that represent 1 to ‘res’)
Following is C++ implementation of above idea.
return res;
}
return 0;
}
Output:
438
4
5
10
Source
http://www.geeksforgeeks.org/find-smallest-value-represented-sum-subset-given-array/
Category: Arrays
Post navigation
← [TopTalent.in] Exclusive Rapid Fire Interview with Gritika Who Got a job offer from Microsoft Nvidia
Interview | Set 2 (On Campus for R & D Team, Pune) →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
439
Chapter 117
Given an array of size n where all elements are in range from 0 to n-1, change contents of arr[] so that arr[i]
= j is changed to arr[j] = i.
Examples:
Example 1:
Input: arr[] = {1, 3, 0, 2};
Output: arr[] = {2, 0, 3, 1};
Explanation for the above output.
Since arr[0] is 1, arr[1] is changed to 0
Since arr[1] is 3, arr[3] is changed to 1
Since arr[2] is 0, arr[0] is changed to 2
Since arr[3] is 2, arr[2] is changed to 3
Example 2:
Input: arr[] = {2, 0, 1, 4, 5, 3};
Output: arr[] = {1, 2, 0, 5, 3, 4};
Example 3:
Input: arr[] = {0, 1, 2, 3};
Output: arr[] = {0, 1, 2, 3};
Example 4:
Input: arr[] = {3, 2, 1, 0};
Output: arr[] = {3, 2, 1, 0};
A Simple Solution is to create a temporary array and one by one copy ‘i’ to ‘temp[arr[i]]’ where i varies
from 0 to n-1.
Below is C implementation of the above idea.
440
// such that arr[j] becomes j if arr[i] is j
#include<stdio.h>
// Drive program
int main()
{
int arr[] = {1, 3, 0, 2};
int n = sizeof(arr)/sizeof(arr[0]);
rearrangeNaive(arr, n);
Output:
Given array is
1 3 0 2
Modified array is
2 0 3 1
Time complexity of the above solution is O(n) and auxiliary space needed is O(n).
441
Can we solve this in O(n) time and O(1) auxiliary space?
The idea is based on the fact that the modified array is basically a permutation of input array. We can find
the target permutation by storing the next item before updating it.
Let us consider array ‘{1, 3, 0, 2}’ for example. We start with i = 0, arr[i] is 1. So we go to arr[1] and change
it to 0 (because i is 0). Before we make the change, we store old value of arr[1] as the old value is going to
be our new index i. In next iteration, we have i = 3, arr[3] is 2, so we change arr[2] to 3. Before making the
change we store next i as old value of arr[2].
The below code gives idea about this approach.
// Update arr[]
arr[i] = val;
The above function doesn’t work for inputs like {2, 0, 1, 4, 5, 3}; as there are two cycles. One
cycle is (2, 0, 1) and other cycle is (4, 5, 3).
How to handle multiple cycles with the O(1) space constraint?
The idea is to process all cycles one by one. To check whether an element is processed or not, we change the
value of processed items arr[i] as -arr[i]. Since 0 can not be made negative, we first change all arr[i] to arr[i]
+ 1. In the end, we make all values positive and subtract 1 to get old values back.
442
// starting at arr[i]. This function assumes values in
// arr[] be from 1 to n. It changes arr[j-1] to i+1
// if arr[i-1] is j+1
void rearrangeUtil(int arr[], int n, int i)
{
// 'val' is the value to be stored at 'arr[i]'
int val = -(i+1); // The next value is determined
// using current index
i = arr[i] - 1; // The next index is determined
// using current value
// Update arr[]
arr[i] = val;
443
{
int i;
for (i=0; i<n; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Drive program
int main()
{
int arr[] = {2, 0, 1, 4, 5, 3};
int n = sizeof(arr)/sizeof(arr[0]);
rearrange(arr, n);
Output:
Given array is
2 0 1 4 5 3
Modified array is
1 2 0 5 3 4
The time complexity of this method seems to be more than O(n) at first look. If we take a closer look, we
can notice that no element is processed more than constant number of times.
This article is contributed by Arun Gupta. Please write comments if you find anything incorrect, or you
want to share more information about the topic discussed above
Source
http://www.geeksforgeeks.org/rearrange-array-arrj-becomes-arri-j/
Category: Arrays
Post navigation
← SAP Labs Interview Experience | Set 4 (Off-Campus) Find maximum depth of nested parenthesis in a
string →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
444
Chapter 118
Suppose you have a sorted array of infinite numbers, how would you search an element in the array?
Source: Amazon Interview Experience.
Since array is sorted, the first thing clicks into mind is binary search, but the problem here is that we don’t
know size of array.
If the array is infinite, that means we don’t have proper bounds to apply binary search. So in order to find
position of key, first we find bounds and then apply binary search algorithm.
Let low be pointing to 1st element and high pointing to 2nd element of array, Now compare key with high
index element,
->if it is greater than high index element then copy high index in low index and double the high index.
->if it is smaller, then apply binary search on high and low indices found.
Below are implementations of above algorithm
C++
445
// function takes an infinite size array and a key to be
// searched and returns its position if found else -1.
// We don't know size of arr[] and we can assume size to be
// infinite in this function.
// NOTE THAT THIS FUNCTION ASSUMES arr[] TO BE OF INFINITE SIZE
// THEREFORE, THERE IS NO INDEX OUT OF BOUND CHECKING
int findPos(int arr[], int key)
{
int l = 0, h = 1;
int val = arr[0];
// Driver program
int main()
{
int arr[] = {3, 5, 7, 9, 10, 90, 100, 130, 140, 160, 170};
int ans = findPos(arr, 10);
if (ans==-1)
cout << "Element not found";
else
cout << "Element found at index " << ans;
return 0;
}
// C++ program to demonstrate working of an algorithm that finds
// an element in an array of infinite size
#include<iostream>
using namespace std;
446
}
// Driver program
int main()
{
int arr[] = {3, 5, 7, 9, 10, 90, 100, 130, 140, 160, 170};
int ans = findPos(arr, 10);
if (ans==-1)
cout << "Element not found";
else
cout << "Element found at index " << ans;
return 0;
}
Python
if r >= l:
mid = l+(r-l)/2
if arr[mid] == x:
return mid
if arr[mid] > x:
447
return binary_search(arr,l,mid-1,x)
return binary_search(arr,mid+1,r,x)
return -1
l, h, val = 0, 1, arr[0]
# Driver function
arr = [3, 5, 7, 9, 10, 90, 100, 130, 140, 160, 170]
ans = findPos(arr,10)
if ans == -1:
print "Element not found"
else:
print"Element found at index",ans
Output:
Let p be the position of element to be searched. Number of steps for finding high index ‘h’ is O(Log p). The
value of ‘h’ must be less than 2*p. The number of elements between h/2 and h must be O(p). Therefore,
time complexity of Binary Search step is also O(Log p) and overall time complexity is 2*O(Log p) which is
O(Log p).
This article is contributed by Gaurav Sharma. Please write comments if you find anything incorrect, or
you want to share more information about the topic discussed above
Source
http://www.geeksforgeeks.org/find-position-element-sorted-array-infinite-numbers/
448
Chapter 119
The worst case time complexity of a typical implementation of QuickSortis O(n2 ). The worst case occurs
when the picked pivot is always an extreme (smallest or largest) element. This happens when input array
is sorted or reverse sorted and either first or last element is picked as pivot.
Although randomized QuickSort works well even when the array is sorted, there is still possibility that the
randomly picked element is always an extreme. Can the worst case be reduced to O(nLogn)?
The answer is yes, we can achieve O(nLogn) worst case. The idea is based on the fact that the median
element of an unsorted array can be found in linear time. So we find the median first, then partition the
array around the median element.
Following is C++ implementation based on above idea. Most of the functions in below progran are copied
from K’th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time)
449
// Partition the array around median
int p = partition(arr, l, h, med);
// If position is same as k
if (pos-l == k-1)
return arr[pos];
if (pos-l > k-1) // If position is more, recur for left
return kthSmallest(arr, l, pos-1, k);
450
// Else recur for right subarray
return kthSmallest(arr, pos+1, r, k-pos+l-1);
}
451
quickSort(arr, 0, n-1);
cout << "Sorted array is\n";
printArray(arr, n);
return 0;
}
Output:
Sorted array is
1 5 6 7 8 9 10 20 30 900 1000
Source
http://www.geeksforgeeks.org/can-quicksort-implemented-onlogn-worst-case-time-complexity/
Category: Arrays Tags: Sorting
Post navigation
← Belzabar Software Interview Experience | Set 4 Microsoft Interview Experience | Set 46 (Onsite) →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
452
Chapter 120
Given an unsorted of distinct integers, find the largest pair sum in it. For example, the largest pair sum in
{12, 34, 10, 6, 40} is 74.
Difficulty Level: Rookie
Expected Time Complexity: O(n) [Only one traversal of array is allowed]
We strongly recommend to minimize your browser and try this yourself first.
This problem mainly boils down to finding the largest and second largest element in array. We can find the
largest and second largest in O(n) time by traversing array once.
453
if (arr[0] > arr[1])
{
first = arr[0];
second = arr[1];
}
else
{
first = arr[1];
second = arr[0];
}
Output:
Source
http://www.geeksforgeeks.org/find-the-largest-pair-sum-in-an-unsorted-array/
Category: Arrays
454
Chapter 121
Given a stream of characters (characters are received one by one), write a function that prints ‘Yes’ if a
character makes the complete string palindrome, else prints ‘No’.
Examples:
Let input string be str[0..n-1]. A Simple Solution is to do following for every character str[i] in input string.
Check if substring str[0…i] is palindrome, then print yes, else print no.
A Better Solution is to use the idea of Rolling Hash used in Rabin Karp algorithm. The idea is to keep
track of reverse of first half and second half (we also use first half and reverse of second half) for every index.
Below is complete algorithm.
455
1) The first character is always a palindrome, so print yes for
first character.
456
#include<stdio.h>
#include<string.h>
int h = 1, i, j;
457
{
// Add next character after first half at beginning
// of 'firstr'
h = (h*d) % q;
firstr = (firstr + h*str[i/2])%q;
Output:
a Yes
a Yes
b No
a No
a Yes
c No
a No
a No
b No
a No
a Yes
The worst case time complexity of the above solution remains O(n*n), but in general, it works much better
than simple approach as we avoid complete substring comparison most of the time by first comparing hash
values. The worst case occurs for input strings with all same characters like “aaaaaa”.
This article is contributed by Ajay. Please write comments if you find anything incorrect, or you want to
share more information about the topic discussed above.
458
Source
http://www.geeksforgeeks.org/online-algorithm-for-checking-palindrome-in-a-stream/
459
Chapter 122
Given two unsorted arrays that represent two sets (elements in every array are distinct), find union and
intersection of two arrays.
For example, if the input arrays are:
arr1[] = {7, 1, 5, 2, 3, 6}
arr2[] = {3, 8, 6, 20, 7}
Then your program should print Union as {1, 2, 3, 5, 6, 7, 8, 20} and Intersection as {3, 6}. Note that the
elements of union and intersection can be printed in any order.
Method 1 (Naive)
Union:
1) Initialize union U as empty.
2) Copy all elements of first array to U.
3) Do following for every element x of second array:
…..a) If x is not present in first array, then copy x to U.
4) Return U.
Intersection:
1) Initialize intersection I as empty.
2) Do following for every element x of first array
…..a) If x is present in second array, then copy x to I.
4) Return I.
Time complexity of this method is O(mn) for both operations. Here m and n are number of elements in
arr1[] and arr2[] respectively.
Method 2 (Use Sorting)
1) Sort arr1[] and arr2[]. This step takes O(mLogm + nLogn) time.
2) Use O(m + n) algorithms to find union and intersection of two sorted arrays.
Overall time complexity of this method is O(mLogm + nLogn).
Method 3 (Use Sorting and Searching)
Union:
1) Initialize union U as empty.
2) Find smaller of m and n and sort the smaller array.
3) Copy the smaller array to U.
4) For every element x of larger array, do following
460
…….b) Binary Search x in smaller array. If x is not present, then copy it to U.
5) Return U.
Intersection:
1) Initialize intersection I as empty.
2) Find smaller of m and n and sort the smaller array.
3) For every element x of larger array, do following
…….b) Binary Search x in smaller array. If x is present, then copy it to I.
4) Return I.
Time complexity of this method is min(mLogm + nLogm, mLogn + nLogn) which can also be written as
O((m+n)Logm, (m+n)Logn). This approach works much better than the previous approach when difference
between sizes of two arrays is significant.
Thanks to use_the_force for suggesting this method in a comment here.
Below is C++ implementation of this method.
int temp = m;
m = n;
n = temp;
}
// Sort the first array and print its elements (these two
// steps can be swapped as order in output is not important)
sort(arr1, arr1 + m);
for (int i=0; i<m; i++)
cout << arr1[i] << " ";
461
// Prints intersection of arr1[0..m-1] and arr2[0..n-1]
void printIntersection(int arr1[], int arr2[], int m, int n)
{
// Before finding intersection, make sure arr1[0..m-1] is smaller
if (m > n)
{
int *tempp = arr1;
arr1 = arr2;
arr2 = tempp;
int temp = m;
m = n;
n = temp;
}
462
int arr2[] = {3, 8, 6, 20, 7};
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
cout << "Union of two arrays is \n";
printUnion(arr1, arr2, m, n);
cout << "\nIntersection of two arrays is \n";
printIntersection(arr1, arr2, m, n);
return 0;
}
Output:
Source
http://www.geeksforgeeks.org/find-union-and-intersection-of-two-unsorted-arrays/
463
Chapter 123
Given an array of integers, write a function that returns true if there is a triplet (a, b, c) that satisfies a2 +
b2 = c2 .
Example:
Method 1 (Naive)
A simple solution is to run three loops, three loops pick three array elements and check if current three
elements form a Pythagorean Triplet.
Below is C++ implementation of simple solution.
464
if (x == y + z || y == x + z || z == x + y)
return true;
}
}
}
Output:
Yes
465
{
// Square array elements
for (int i=0; i<n; i++)
arr[i] = arr[i]*arr[i];
// Now fix one element one by one and find the other two
// elements
for (int i = n-1; i >= 2; i--)
{
// To find the other two elements, start two index
// variables from two corners of the array and move
// them toward each other
int l = 0; // index of the first element in arr[0..i-1]
int r = i-1; // index of the last element in arr[0..i-1]
while (l < r)
{
// A triplet found
if (arr[l] + arr[r] == arr[i])
return true;
Output:
Yes
Source
http://www.geeksforgeeks.org/find-pythagorean-triplet-in-an-unsorted-array/
466
Category: Arrays
Post navigation
← BrowserStack Interview | Set 7 (Online Coding Questions) Point to next higher value node in a linked
list with an arbitrary pointer →
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
467
Chapter 124
In a daily share trading, a buyer buys shares in the morning and sells it on same day. If the trader is allowed
to make at most 2 transactions in a day, where as second transaction can only start after first one is complete
(Sell->buy->sell->buy). Given stock prices throughout day, find out maximum profit that a share trader
could have made.
Examples:
We strongly recommend to minimize your browser and try this yourself first.
A Simple Solution is to to consider every index ‘i’ and do following
468
Maximum possible using one transaction can be calculated using following O(n) algorithm
Maximum difference between two elements such that larger element appears after the smaller number
Time complexity of above simple solution is O(n2 ).
We can do this O(n) using following Efficient Solution. The idea is to store maximum possible profit of
every subarray and solve the problem in following two phases.
1) Create a table profit[0..n-1] and initialize all values in it 0.
2) Traverse price[] from right to left and update profit[i] such that profit[i] stores maximum profit achievable
from one transaction in subarray price[i..n-1]
3) Traverse price[] from left to right and update profit[i] such that profit[i] stores maximum profit such that
profit[i] contains maximum achievable profit from two transactions in subarray price[0..i].
4) Return profit[n-1]
To do step 1, we need to keep track of maximum price from right to left side and to do step 2, we need to
keep track of minimum price from left to right. Why we traverse in reverse directions? The idea is to save
space, in second step, we use same array for both purposes, maximum with 1 transaction and maximum
with 2 transactions. After an iteration i, the array profit[0..i] contains maximum profit with 2 transactions
and profit[i+1..n-1] contains profit with two transactions.
Below are implementations of above idea.
C++
469
// max_price
profit[i] = max(profit[i+1], max_price-price[i]);
}
return result;
}
// Drive program
int main()
{
int price[] = {2, 30, 15, 10, 8, 25, 80};
int n = sizeof(price)/sizeof(price[0]);
cout << "Maximum Profit = " << maxProfit(price, n);
return 0;
}
Python
470
if price[i]> max_price:
max_price = price[i]
for i in range(1,n):
result = profit[n-1]
return result
# Driver function
price = [2, 30, 15, 10, 8, 25, 80]
print "Maximum profit is", maxProfit(price, len(price))
Output:
Source
http://www.geeksforgeeks.org/maximum-profit-by-buying-and-selling-a-share-at-most-twice/
471