## Arrays With Elements In The Range 1 to N

When an array has all its elements in the range of 1 to N ( where N is the length of the array ) we can use the indices to store the ordered state of the elements in the array. This ordered-state can in-turn be used to solve a variety of problems which we’ll explore soon. First, a very simple demonstration of this property.

Here is an array which has unique elements in the range of 1 to N.
Given array (A) : 5,3,1,4,2
Indices:                0,1,2,3,4

Sort in Linear Time

The first use-case of this unique property is being able to sort in O(N) time i.e. a special-case(all unique elements) of the Counting Sort. The crux of this sort is to check whether an element is at its corresponding index and swap it to its correct index if it’s not. Following is a demonstration of this logic:

Given array (A) : 5,3,1,4,2
Indices:                0,1,2,3,4

For each A[i] check if A[A[i] – 1] equals A[i] or not. If they are not equal then swap element at A[A[i] – 1] with A[i]. Basically the correct value for any index i is when A[i] contains i+1.

A[A – 1] or A[5-1] orA which is 2 and A = 5. This means that A[A[i] – 1] is not equal to A[i] and hence not in its correct position. So we need to swap in order to put A -> 5 to its correct position which is index 4 and A will hold 4 after the swap. Similarly, we need to repeat this check & swap for all the elements.

What if we cancel-out the common terms and modify the check from  `A[i] != A[A[i] - 1]` to `i != A[i]-1` ?

Find The Missing Integer

A similar approach can help us find the smallest missing positive-integer in a given array. By smallest missing positive-integer, we just mean the smallest positive integer that does not exist in the given list of numbers. For example:

Given Array: `-2, 3, 0, 1, 3`
In the above case, the smallest missing positive integer is 2.

If we were to apply the usual sorting techniques and then scan the array for the smallest positive integer absent it would imply a time-complexity of O(NLog(N)) + O(N). We can definitely do better than this! At first glance, it seems that this problem does not lie within the unique property of elements being in the range of 1 to N since the numbers in the given array are well outside the range, but to solve this problem we still only need to figure out whether we have elements from 1 to N present in the given array or not.

How do we know whether the given array has elements from 1 to N? We can use the counting-sort discussed earlier to put each element in its “correct position”, i.e index 0 should hold 1, index 1 should hold 2 and so on. The smallest index that does not hold its correct element is the missing integer.

If we sort the given array using counting sort described above, we will get: `1, 0, 3, -2, 3`. And the smallest index `i` to not hold its correct value i.e. `i+1` will give us the answer to the smallest missing positive integer. In this case, that index is 1 since it does not hold 2, thus the smallest positive missing integer is 2.

Find The Duplicate Element

The third use-case of this property is to figure out the duplicate elements without using any extra space. We can iterate over the array A and mark the corresponding index of the encountered element as negative – unless it has already been marked negative! For example: if A = 3 (or -3 ) then mark `A[ Abs - 1]` as negative, this way whenever we encounter 3 (or -3) again in any of the A[i] we will know that the value 3 has been visited before since A[3-1] will be negative.

Given array (A) : 5,3,1,4,3
Indices:                0,1,2,3,4

When we encounter A i.e. 5, we make A[5-1] i.e. A negative, so the array becomes:
`5,3,1,4,-3`
Next, we encounter A i.e. 3, we make A[3-1] i.e. A negative, so the array becomes:
`5,3,-1,4,-3`
Next, we encounter A i.e. -1, we make A[1-1] i.e. A negative, so the array becomes:
`-5,3,-1,4,-3`
Next, we encounter A i.e. 4, we make A[4-1] i.e. A negative, so the array becomes:
`-5,3,-1,-4,-3`
Next, we encounter A i.e. -3, we want to make A[3-1] i.e. A negative, but in this case, A is already negative thus we know that A has been visited before! Which means `Abs(A)` i.e 3 is the duplicate element.

Here is a snippet to demonstrate the code for sorting an array in linear time as per the above approach. The exact same approach can be used to solve the other two applications i.e. Finding the Duplicate and Finding The Missing Integer.

```        int swap=0;

for(int i = 0; i < nums.length;){

if(nums[i] > 0 && nums[i] < nums.length) {

if(nums[nums[i]-1] != nums[i]){
swap = nums[i];
nums[i] = nums[nums[i] - 1];
nums[swap - 1] = swap;
}else{
i++;
}

}else{
i++;
}
}
```

If you are preparing for a technical interview in companies like Amazon, Facebook, etc and want help with preparation, please register for a coaching session with us.

# Minimum number of pages to read

In previous post Ceiling in sorted array using binary search , we understood a very important concept about application of binary search in problems where minimum or maximum of something is asked. In the post mentioned above, we were asked to find minimum element which is greater than target value. We will use the same concept to solve another interesting problem : Find minimum number of pages to read for each student. Problem statement:
Given N different books and M students. Each book has certain pages. Every student is assigned to read some consecutive books.  Find a minimum number of pages each student has to read, so that all books are read. It should be noted that a student cannot read partial book, he/she needs to read entire book. For example, if number of pages of 8 books are as given below and there are 3 students to finish those books, a student has to read at least 84 pages. Books have to be read in sequence and either complete book is read or not read at all by student. Books read by each student is shown below If we change the order of books as shown below, minimum number of pages each student has to read are 82 ## Minimum number of pages to read : Thought process

Before we solve it, let’s revisit the basic premise to use binary search algorithm.

Binary search can be used if and only if for all x in candidate Set S, predicate(x) implies predicate(y) for all y > x.

In this problem, if students can finish `N books` with each student reading `K pages`, then it is definitely possible to finish N books by reading `K+1` and more pages. This statement implies, that problem satisfy to apply binary search.

For binary search algorithm, three things are required : `search space` or candidate solution set, `lower bound` and `upper bound` of search space.
Assume that there is only one student, what will be the minimum number of pages he or she has to read to finish all books? Obviously, student has to read at least all pages in all books. This gives us upper bound of our solution set. Answer of this problem cannot be more than this `upper bound`.

Now, assume that we have N students but there is no book to read. Then minimum number of pages to be read by each student is zero. Well, student cannot read less than zero pages, hence `lower bound` of solution is zero.
At this point, we know lower and upper bound of solution. How can we find the required minimum number of page with N books and M students?

Idea is to start with middle of lower and upper bounds of pages to be read. Let’s call it `K`. With each student reading K pages, will all books be completed? If yes, it is always possible to finish all books with each student reading more than K pages, hence, there is no need to check from K to upper bound. All we need to verify that if there is a solution with each student reading K or less than K pages each.

##### Designing predicate function

What will be predicate? Predicate will be implemented by going through each book’s pages and see when sum of pages goes more than current candidate minimum. As soon it current sum goes more than candidate minimum, we add one more student. When all books are finished, we check if we required less than equal to M students. If yes, this candidate solution is valid and predicate should return true. If more than M students are required to finish all books, then current candidate is not valid and hence function return false.

Based on what is returned from predicate function, either right or left subset of candidate solution is discarded. In this example, if predicate function returns true, upper bound to be searched will be set to K. Else lower bound will be set to K+1.

## Minimum number of pages to read  implementation

```package com.company;

import java.util.Arrays;
import java.util.Scanner;

/**
* Created by sangar on 28.3.18.
*/
public class Books {
public static boolean predicate(long[] books, long candidate, int days){

long currentPages = 0;
int studentRequired = 1;
int i = 0;

while(i<books.length){
if(books[i] > candidate){
return false;
}
if(currentPages + books[i] <= candidate){
currentPages+=books[i];
i++;
}else{
currentPages = 0;
studentRequired++;
}
}
return days >= studentRequired;
}

public static void main(String args[] ) throws Exception {
Scanner scanner = new Scanner(System.in);

int books = scanner.nextInt();
int students = scanner.nextInt();

long [] pages = new long[books];

for(int i=0; i<books; i++){
pages[i] = scanner.nextLong();
}

long low = 0;
long high = Arrays.stream(pages).sum();

while(low < high){
long mid  = low + ( (high - low) >> 1);

if(predicate(pages, mid, students)){
high = mid;
}else{
low = mid+1;
}
}
System.out.println(low);
}
}
```

Complexity of algorithm to find minimum number of pages will be O(sum of pages of all books).

### More problems on similar lines

It’s very interesting to see how many problems can be solved using same approach. I solved one on Hacker Rank : BooBoo and upsolving

```  public static boolean predicate(long[] time, long candidateTime, int days){

long currentTime = 0;
int daysRequired = 1;
int i = 0;

while(i<time.length){
if(time[i] > candidateTime){
return false;
}
if(currentTime + time[i] <= candidateTime){
currentTime+=time[i];
i++;
}else{
currentTime = 0;
daysRequired++;
}
}
return days >= daysRequired;
}

public static void main(String args[] ) throws Exception {
Scanner scanner = new Scanner(System.in);

int days = scanner.nextInt();

long [] time = new long[tasks];

time[i] = scanner.nextLong();
}

/* What will be the maximum time he has to practice?
It will be when he has only one day and all problems needs to be solved.
that will give us the upper bound of time.

What will be minimum time required? When he has no problems to be solved.
That will give us lower bound of time.

Idea is to start with middle of lower and upper bounds.And see if all problems can be solved
by practicing that amount of time each day. If yes, there is a possibility that it can be done
in less than that, hence, we try to find reduce our search space from lower bound to mid. Should mid be included?

If all problems can not be solved by practicing mid amount of time, then there is no way it can be done
by practicing less. Hence we increase the time and start looking in mid+1 to higher bound
*/

//first let's set lower and higher bound.
long low = 0;
long high = Arrays.stream(time).sum();

while(low < high){
long mid  = low + ( (high - low) >> 1);

if(predicate(time, mid, days)){
high = mid;
}else{
low = mid+1;
}
}

System.out.println(low);
}
```

Similar method can be applied to topcoder problem Fair Work, try it yourself, if are able to solve it, please drop code in comment.

Please share if there is something is wrong or missing. If you want to contribute to website and share your knowledge with learners, please write to [email protected]