AfterAcademy Tech
•
04 Dec 2019

Difficulty: Hard
Asked in: Amazon, Google
Problem Description: Given an array, find the smallest missing positive integer.
For example
Input: A[] = [2, 3, -7, 6, 8, 1, -10, 15]
Output: 4
Input: A[] = [3, 2, -1, 1]
Output: 4
Input: A[] = [7, 8, 9, 11, 12]
Output: 1
Possible follow-up questions to ask the interviewer:-
We are discussing four ways to solve this problem :
The simple solution is to search all positive integers starting from 1 (Think!). At worst case, we have to search for n+1 numbers.
Pseudo Code
int smallestPositive(int A[], int n)
{
for(i = 1 to n+1) // search for 1 to n+1 elements
{
bool flag = false
for(j = 0 to n-1) // iterating through the array
{
if(A[j] == i)
{
flag = true
break
}
}
if(flag == false)
return i
}
}
Complexity Analysis
Time Complexity: Search positive integers from 1 to n+1 linearly in the array = O(n²)
Space Complexity: O(1)
Critical ideas to think!
We can sort the array and linearly scan the array to find the smallest positive element.
Pseudo Code
int smallestPositive(int A[], int n)
{
sort(A)
small = 1
for(i = 0 to n-1)
{
if(small > A[i]) // to handle repetition and non positive numbers
continue
if(small == A[i])
small = small + 1
if(A[i] > small)
return small
}
return small
}
Complexity Analysis
Suppose we are using heap sort to solve this problem.
Time Complexity: Sorting + Linear Scan for finding smallest positive
= O(nlogn) + O(n) = O(nlogn)
Space Complexity: O(1)
Critical ideas to think!
We can build a hash table to store the integers present in the given array. Now we can look out for positive integers starting from 1 to n+1 in the Hash Table.
Solution Steps
Pseudo Code
int smallestPositive(int A[], int n)
{
Create a hash table H
for(i = 0 to n-1)
H[A[i]] = true
// search for first n+1 positive integer
for(i = 1 to n+1)
{
// integer not present in the hash table
if(i not in H)
return i
}
}
Complexity Analysis
Time Complexity: Building hash table + Searching elements in Hash Table = O(n) + O(n) = O(n)
Space Complexity: O(n), To store the hash table.
Critical ideas to think!
The idea is to store the occurrences of the numbers itself in the array. Since the range for missing elements for an array of size n is [1,n+1], so we can use the value of the index in the array to mark the presence of a number in the array (keeping in mind to retrieve the original elements after updating it).How should we mark the presence of an element? (Think!)
When we come across an element k, where 1 ≤ k ≤ N, we will update the value at index (k-1) to its negative value, i.e. A[k-1] = -A[k-1]. (Why index k-1 and not k?) But this approach can still fail for some types of cases, can you guess which cases?
We need to take care of not accidentally modifying the value at the same index more than once when we encounter duplicates. Also, this approach doesn’t work if there are non-positive numbers. So we first need to segregate positive numbers from negative numbers and then apply the above discussed in-place hashing approach.
Solution Steps
Pseudo Code
int smallestPositive(int A[], int n)
{
N = segregate(A, n)
for (k = 0 to N - 1)
{
if(abs(A[k]) - 1 < N)
A[abs(A[k]) - 1] = - abs(A[abs(A[k]) - 1])
}
for(k = 0 to N - 1)
if(A[k] > 0)
return k + 1
return N + 1
}
// segregates non-negative numbers to right side of array
int segregate(int A[], int n)
{
int j = n-1, i = 0
while( i <= j )
{
if(A[i] <= 0)
{
swap(A[i], A[j]) // swap values at A[i] and A[j]
j = j - 1
}
else
i = i + 1
}
return i
}
Complexity Analysis
Time Complexity: Segregate the array and then Linear Scan.
= O(n) + O(n) = O(n)
Space Complexity: O(1)
Critical ideas to think!

May the code be with You!
Enjoy Algorithms!
AfterAcademy Data Structure And Algorithms Online Course — Admissions Open
AfterAcademy Tech
Given an integer, your task is to find the square root of the integer. For an integer x to be the square root of the given integer N, x*x must be equal to N. This is an interview problem asked in companies like Amazon, Microsoft and Facebook.

AfterAcademy Tech
You are given an array A[] consisting of n elements. You need to find and return the number which appears more than n/2 times.

AfterAcademy Tech
Given an array A[] of size n, find the most frequent element in the array, i.e. the element which occurs the most number of times.

AfterAcademy Tech
Given an array A[] of size n, you need to find the maximum and minimum element present in the array.Your algorithm should make minimum number of comparisons.
