Max Sum Contiguous Subarray in C - c

The problem is to find the max contiguous sub array sum; I am getting 0 as the output and in some case a random number(I checked for going out of array bound but all seems fine).
I saw the similar problems asked in STACK OVERFLOW but most of them was for small input and in other programming language.
here is my code:
int maxSubArray(const int* A, int n1)
{
int i,j,max[n1],L;
for (i=0;i<n1-1;i++)
{
int sum=A[i]+A[i+1];
max[i]=sum;
for(j=i+1;j<n1-1;j++)
{
sum=sum+A[j+1];
if(sum>max[i])
{
max[i]=sum;
}
}
}
for(i=1;i<n1;i++)
{
L=max[0];
if(max[i]>L)
{
L=max[i];
}
}
return(L);
}
Please help me finding what's wrong with my code.

the OP really wants to sum at least 2 entries (not 0 or 1).
The L=max[0] needs to be before the second outer loop (in which we determine which entry is largest).
The second outer loop goes one entry too far, because the last entry is not defined (because of the 2 entries rule)

Here is the pseudocode:
current_sum = 0
best_sum = 0
Iterate through array:
if current sum less than current sum + array[i]
current_sum = current_sum + array[i]
if best sum less than current sum
best_sum = current_sum

Related

Max score to reach end of array with step length

Assume an array and we start from element at index 0. We want to go from 0 index to last index of the array by taking steps of at max length K.
For example, suppose an array is [10,2,-10,5,20] and K is 2, which means maximum step length is 2 (We can assume K is always possible and less than length of array).
Now as we start from index 0, our score currently is 10 and then we can either go to 2 or can go to -10. Suppose we go to 2 from here so total score becomes 10+2=12. Now from 2 we can go to -10 or 5 so you go to 5 making score 12+5=17. From here you directly go to last index as you have no way other than that, hence total score is 17+20=37.
For given array of length N and an integer K we need to find maximum score we can get.
I thought of a solution, to divide it into sub problems by deciding weather to go at index i or not and recursively call the remaining array. But I sense some dynamic programming out of this problem.
How can this be solved for given array of size N and integer K.
Constraint : 1<=N<=100000 and 1<=K<=N
Came up with a O(n*k) solution.
Main function call would be findMax(A,N,K,0).
MAX = new Array();
MAX[i] = null. For 0<=i<N
null denoting the particular element has not been filled.
procedure findMax(A,N,K,i)
{
if (MAX[i]!=null)
return MAX[i];
else if (i==N-1)
MAX[i]=A[i];
else
{
MAX[i]=A[i]+findMax(A,N,K,i+1);
for (j=2; j<=K&&(i+j)<N; ++j)
if (A[i]+findMax(A,N,K,i+j)>MAX[i])
MAX[i]=A[i]+findMax(A,N,K,i+j);
}
return MAX[i];
}
The problem has optimal sub-structure property. To calculate optimal solution, all sub-problems need to be computed. So at a quick glance, I guess the time complexity wont go below O(n*k).
This can be solved in O(n) time and memory
basically: go from back from i = n-1 to 0 and you have to know somehow what is the best index from i+1 up to i+k right? Then best answer for i would be to jump on the best index in range [i+1, i+k]
To get that information you can create some sort of queue (but you need to be able to perform pop from both sides in c++ you can use dequeue).
In that queue you keep two informations: (time, value), where time is the time at which you pushed element and value is best sum you can get if you start from element.
Now when you are in index i: first pop until current time (lest name it t) minus queue.top.time is > k: while( t-que.top.time > k) que.pop
Then you can take que.top.value + array[i] and that is the best value you can get from index i.
Last part to do is updating queue. You create new element e = (t, que.top.value + array[i]) and take que.back (instead of que.top) and perform
while (que.back.value <= e.value) que.pop_back
Then you can push back
que.push_back(e)
and increase t++
This works because, when your new element has better value then elements inserted on the que in the past its better to keep this element instead, because you will be able to use it longer.
Hope it makes sense :)
Try to walk backward this way you can achieve that in O(n*logk).
If the array was on size 1 the the max was that element. Consider you in the i-element - you can take him or one of the next K element -> choose the one that maximize your final result.
Consider the following pseudo code:
Base on #RandomPerfectHashFunction answer with some change
Consider Max as our answer array and tree as AVL Tree( self balancing binary search Tree)
findMaxStartingFromIndex(A,N,K,i, Max, Tree)
if Max[i] != null
return Max[i]
max = Tree.Max // log k - just go down all the way to the right
if (i + k > N) // less then k element to end of array
max = max(max,0) // take the maximum only if he positive
Max[i] = A[i] + max
Tree.add(Max[i])
if (i + k < N)
Tree.remove(Max[i+k]) // remove the element from tree because it is out of the rolling-window of k elements
return Max[i]
In Main:
Init Max array at size N
Init Tree as empty AVL tree
Max[N-1] = A[N-1]
Tree.add(MAX[N-1])
for (i = N-2; i >= 0 ; i--)
findMaxStartingFromIndex(A,N,K,i, Nax, Tree)
When all done look for the max in the first k element of the Max array (no always choosing the first element is the best option)
Adding finding and removing element to binary search tree is log n -> in our case tree will hold only k element -> we achieve O(n*logk) complexity
This can be done in O(n). I'm assuming you're already familiar with the basic DP algorithm, which runs in O(nk). We have dp[i] = value[i] + (max(dp[j]) for i - k < j < i). The k factor in the complexity comes from finding the minimum of the last k values in our DP array, which we can optimize to O(1).
One optimization might be to maintain a binary search tree containing the last k values, which would make an O(n log k) solution. But we can do better by using a double-ended queue instead of a binary search tree.
We maintain a deque containing the candidates for the maximum of the last k elements. Before we push the current dp value into the back of the deque, we pop off the value at the back if it is less than or equal to the current value. Because the current value is both better (or at least as good) than the value in the back and will be in the deque for longer, the value at the back will never be the maximum in the deque and can be discarded. We repeat this until the value at the back is no longer less than or equal to the current value.
We can then pop off the front value if its index is less than the current index minus k.
The way we popped off numbers from the back makes our queue always decreasing, so the maximum value is at the front.
Note that even though the loop popping off the values at the back might run as much as n - 1 times in an iteration of the main loop, the total complexity is still O(n) because each element in the DP array popped off at most once.
this can be solved with dynamic programming. dp[i] means the maximum scores we can collect from nums[0] to nums[i]. Transition is dp[i] = max(dp[i-1], dp[i-2],...,dp[i-k])+nums[i]. Time complexity is O(nk).
A greedy solution. You might find it is easier to understand.
class Solution {
public static void main(String[] args) {
//Init
int[]path= {10,2,-10,5,20};
int maxStep=2;
int max=path[0];
if(path.length==0)System.out.println(0);
for(int i=0;i<path.length-1;) {
int index=0,temp=Integer.MIN_VALUE;
//for each step, choose the step that has max value
for(int j=1;(j<=maxStep)&&(i+j<=path.length-1);j++) {
if(i+j>path.length-1)break;
if(path[i+j]>temp) {
temp=path[i+j];
index=j;
}
}
//change the index and the max value
i+=index;max+=temp;
}
System.out.println(max);
}
}
This was asked today in my interview .Two of the answers here posted best approach. Just adding code here for the same.
Time Complexity : O(n) n - number of elements of array
package main.java;
import java.util.*;
public class Main {
public static int solve(int[] a, int k) {
int ans = Integer.MIN_VALUE;
MaxSlidingWindow maxSlidingWindow = new MaxSlidingWindow(k);
for (int i = 0; i < a.length; i++) {
ans = maxSlidingWindow.getMax() + a[i];
maxSlidingWindow.add(i, ans);
}
return ans;
}
public static void main(String[] args) {
int[] input = {-9, -11, -10, 5, 20};
System.out.print(Main.solve(input, 2));
}
}
// at any point MaxSlidingWindow will have atmost k nodes
// with 'index' and 'val' monotonically decreasing from head to tail
class MaxSlidingWindow {
int k;
Deque<Node> q;
class Node {
int index;
int val;
Node(int index, int val) {
this.index = index;
this.val = val;
}
}
MaxSlidingWindow(int k) {
this.k = k;
this.q = new LinkedList<Node>();
}
public void add(int index, int val) {
if (q.isEmpty()) {
q.addLast(new Node(index, val));
} else {
if (index - q.peekFirst().index + 1 > k) {
q.pollFirst(); // removing head as it is out of range
}
while (!q.isEmpty() && q.peekLast().val <= val) {
q.pollLast(); // removing values in last less than current
}
q.addLast(new Node(index, val));
}
}
public int getMax() {
if (q.isEmpty()) {
return 0;
}
return q.peekFirst().val;
}
}

Segmentation fault in test cases

So, the question is as follows:
Given any array(reasonably large) of integers, return the maximum difference between any two elements in the array such that the larger element occurs at a higher index than the smaller element. Return -1, if no such pair is found.
Example:
7
2
3
10
2
4
8
1
where the first element is the size of the array(or the number of lines being entered), and the rest are the elements.
Sample output is 8(10-2) for the above.
My code is as follows:
int A[20],size;
scanf("%d",&size);
for(int i=0;i<size;i++){
scanf("%d\n",&A[i]);
}
int diff = A[1]-A[0];
int currsum = diff;
int maxsum = currsum;
for(int i=1; i<size-1; i++)
{
// Calculate current difference for the loop
diff = A[i+1]-A[i];
// Calculate current sum for the loop
if (currsum > 0)
currsum += diff;
else
currsum = diff;
// Update max sum(if needed)
if (currsum > maxsum)
maxsum = currsum;
}
printf("%d",maxsum);
This is a question from Hackerrank, but it runs for only three out of 10 possible testcases. The rest of the cases return a segmentation fault. Any idea would be helpful.
As mentioned in the comments, you've declared A to hold just 20 integers. But the question can send up to 1,000,000 integers. That's the mistake!
Using pointers make this more important. First declare A as pointer of integers, then, read the first element of the array, using this integer you can allocate memory dynamically (malloc() or calloc() function) for your array A. so the size of A will dynamic and you can resize it in function of the first element.

find the largest ten numbers in an array in C

I have an array of int (the length of the array can go from 11 to 500) and i need to extract, in another array, the largest ten numbers.
So, my starting code could be this:
arrayNumbers[n]; //array in input with numbers, 11<n<500
int arrayMax[10];
for (int i=0; i<n; i++){
if(arrayNumbers[i] ....
//here, i need the code to save current int in arrayMax correctly
}
//at the end of cycle, i want to have in arrayMax, the ten largest numbers (they haven't to be ordered)
What's the best efficient way to do this in C?
Study maxheap. Maintain a heap of size 10 and ignore all spilling elements. If you face a difficulty please ask.
EDIT:
If number of elements are less than 20, find n-10 smallest elements and rest if the numbers are top 10 numbers.
Visualize a heap here
EDIT2: Based on comment from Sleepy head, I searched and found this (I have not tested). You can find kth largest element (10 in this case) in )(n) time. Now in O(n) time, you can find first 10 elements which are greater than or equal to this kth largest number. Final complexity is linear.
Here is a algo which solves in linear time:
Use the selection algorithm, which effectively find the k-th element in a un-sorted array in linear time. You can either use a variant of quick sort or more robust algorithms.
Get the top k using the pivot got in step 1.
This is my idea:
insert first 10 elements of your arrayNum into arrMax.
Sort those 10 elements arrMax[0] = min , arrMax[9] = max.
then check the remaining elements one by one and insert every possible candidate into it's right position as follow (draft):
int k, r, p;
for (int k = 10; k < n; k++)
{
r = 0;
while(1)
{
if (arrMax[r] > arrNum[k]) break; // position to insert new comer
else if (r == 10) break; // don't exceed length of arrMax
else r++; // iteration
}
if (r != 0) // no need to insert number smaller than all members
{
for (p=0; p<r-1; p++) arrMax[p]=arrMax[p+1]; // shift arrMax to make space for new comer
arrMax[r-1] = arrNum[k]; // insert new comer at it's position
}
} // done!
Sort the array and insert Max 10 elements in another array
you can use the "select" algorithm which finds you the i-th largest number (you can put any number you like instead of i) and then iterate over the array and find the numbers that are bigger than i. in your case i=10 of course..
The following example can help you. it arranges the biggest 10 elements of the original array into arrMax assuming you have all positive numbers in the original array arrNum. Based on this you can work for negative numbers also by initializing all elements of the arrMax with possible smallest number.
Anyway, using a heap of 10 elements is a better solution rather than this one.
void main()
{
int arrNum[500]={1,2,3,21,34,4,5,6,7,87,8,9,10,11,12,13,14,15,16,17,18,19,20};
int arrMax[10]={0};
int i,cur,j,nn=23,pos;
clrscr();
for(cur=0;cur<nn;cur++)
{
for(pos=9;pos>=0;pos--)
if(arrMax[pos]<arrNum[cur])
break;
for(j=1;j<=pos;j++)
arrMax[j-1]=arrMax[j];
if(pos>=0)
arrMax[pos]=arrNum[cur];
}
for(i=0;i<10;i++)
printf("%d ",arrMax[i]);
getch();
}
When improving efficiency of an algorithm, it is often best (and instructive) to start with a naive implementation and improve it. Since in your question you obviously don't even have that, efficiency is perhaps a moot point.
If you start with the simpler question of how to find the largest integer:
Initialise largest_found to INT_MIN
Iterate the array with :
IF value > largest_found THEN largest_found = value
To get the 10 largest, you perform the same algorithm 10 times, but retaining the last_largest and its index from the previous iteration, modify the largest_found test thus:
IF value > largest_found &&
value <= last_largest_found &&
index != last_largest_index
THEN
largest_found = last_largest_found = value
last_largest_index = index
Start with that, then ask yourself (or here) about efficiency.

Sorting an Array without changing it C

Hey guys I have been working on this for 3 days and have come up with nothing from everywere I have looked.
I am trying to take an Array of around 250 floats and find the Kth largest value without changing the array in anyway or making a new array.
I can change it or create a new one because other functions need the placing of the data in the correct order and my Arduino cant hold any more values in its memory space so the 2 easiest options are out.
The values in the Array can ( and probably will ) have duplicates in them.
As an EG : if you have the array ::: 1,36,2,54,11,9,22,9,1,36,0,11;
from Max to min would be ::
1) 54
2) 36
3) 36
4) 22
5) 11
6) 11
7) 9
8) 9
9) 2
10) 1
11) 1
12) 0
Any help would be great.
It may be to much to ask for a function that would do this nicely for me :) hahaha
here is the code I have so far but I have not even tried to get the duplicates working yet
and it for some reason only gives me one answer for some reason that's 2 ,,, no clue why though
void setup()
{
Serial.begin(9600);
}
void loop ()
{
int Array[] = {1,2,3,4,5,6,7,8,9,10};
int Kth = 6; //// just for testing putting the value as a constant
int tr = 0; /// traking threw the array to find the MAX
for (int y=0;y<10;y++) //////////// finding the MAX first so I have somewhere to start
{
if (Array[y]>Array[tr])
{
tr = y;
}
}
Serial.print("The max number is ");
int F = Array[tr];
Serial.println(F); // Prints the MAX ,,, mostly just for error checking this is done
///////////////////////////////////////////////////////// got MAX
for ( int x = 1; x<Kth;x++) //// run the below Kth times and each time lowering the "Max" making the loop run Kth times
{
for(int P=0;P<10;P++) // run threw every element
{
if (Array[P]<F)
{
for(int r=0;r<10;r++) //and then test that element against every other element to make sure
//its is bigger then all the rest but small then MAX
{
Serial.println(r);
if(r=tr) /////////////////// done so the max dosent clash with the number being tested
{
r++;
Serial.println("Max's Placeing !!!!");
}
if(Array[P]>Array[r])
{
F=Array[P]; ////// if its bigger then all others and smaller then the MAx then make that the Max
Serial.print(F);
Serial.println(" on the ");
}
}}}}
Serial.println(F); /// ment to give me the Kth largest number
delay(1000);
}
If speed isn't an issue you can take this approach (pseudocode):
current=inf,0
for i in [0,k):
max=-inf,0
for j in [0,n):
item=x[j],j
if item<current and item>max:
max=item
current=max
current will then contain the kth largest item, where an item is a pair of value and index.
The idea is simple. To find the first largest item, you just find the largest item. To find the second largest item, you find the largest item that isn't greater than your first largest item. To find the third largest item, you find the largest item that isn't greater than your second largest item. etc.
The only trick here is that since there can be duplicates, the items need to include both a value and an index to make them unique.
Here is how it might be implemented in C:
void loop()
{
int array[] = {1,2,3,4,5,6,7,8,9,10};
int n = 10;
int k = 6; //// just for testing putting the value as a constant
int c = n; // start with current index being past the end of the array
// to indicate that there is no current index.
for (int x = 1; x<=k; x++) {
int m = -1; // start with the max index being before the beginning of
// the array to indicate there is no max index
for (int p=0; p<n; p++) {
int ap = array[p];
// if this item is less than current
if (c==n || ap<array[c] || (ap==array[c] && p<c)) {
// if this item is greater than max
if (m<0 || ap>array[m] || (ap==array[m] && p>m)) {
// make this item be the new max
m = p;
}
}
}
// update current to be the max
c = m;
}
Serial.println(array[c]); /// ment to give me the Kth largest number
delay(1000);
}
In the C version, I just keep track of the current and max indices, since I can always get the current and max values by looking in the array.

Find the Smallest Integer Not in a List

An interesting interview question that a colleague of mine uses:
Suppose that you are given a very long, unsorted list of unsigned 64-bit integers. How would you find the smallest non-negative integer that does not occur in the list?
FOLLOW-UP: Now that the obvious solution by sorting has been proposed, can you do it faster than O(n log n)?
FOLLOW-UP: Your algorithm has to run on a computer with, say, 1GB of memory
CLARIFICATION: The list is in RAM, though it might consume a large amount of it. You are given the size of the list, say N, in advance.
If the datastructure can be mutated in place and supports random access then you can do it in O(N) time and O(1) additional space. Just go through the array sequentially and for every index write the value at the index to the index specified by value, recursively placing any value at that location to its place and throwing away values > N. Then go again through the array looking for the spot where value doesn't match the index - that's the smallest value not in the array. This results in at most 3N comparisons and only uses a few values worth of temporary space.
# Pass 1, move every value to the position of its value
for cursor in range(N):
target = array[cursor]
while target < N and target != array[target]:
new_target = array[target]
array[target] = target
target = new_target
# Pass 2, find first location where the index doesn't match the value
for cursor in range(N):
if array[cursor] != cursor:
return cursor
return N
Here's a simple O(N) solution that uses O(N) space. I'm assuming that we are restricting the input list to non-negative numbers and that we want to find the first non-negative number that is not in the list.
Find the length of the list; lets say it is N.
Allocate an array of N booleans, initialized to all false.
For each number X in the list, if X is less than N, set the X'th element of the array to true.
Scan the array starting from index 0, looking for the first element that is false. If you find the first false at index I, then I is the answer. Otherwise (i.e. when all elements are true) the answer is N.
In practice, the "array of N booleans" would probably be encoded as a "bitmap" or "bitset" represented as a byte or int array. This typically uses less space (depending on the programming language) and allows the scan for the first false to be done more quickly.
This is how / why the algorithm works.
Suppose that the N numbers in the list are not distinct, or that one or more of them is greater than N. This means that there must be at least one number in the range 0 .. N - 1 that is not in the list. So the problem of find the smallest missing number must therefore reduce to the problem of finding the smallest missing number less than N. This means that we don't need to keep track of numbers that are greater or equal to N ... because they won't be the answer.
The alternative to the previous paragraph is that the list is a permutation of the numbers from 0 .. N - 1. In this case, step 3 sets all elements of the array to true, and step 4 tells us that the first "missing" number is N.
The computational complexity of the algorithm is O(N) with a relatively small constant of proportionality. It makes two linear passes through the list, or just one pass if the list length is known to start with. There is no need to represent the hold the entire list in memory, so the algorithm's asymptotic memory usage is just what is needed to represent the array of booleans; i.e. O(N) bits.
(By contrast, algorithms that rely on in-memory sorting or partitioning assume that you can represent the entire list in memory. In the form the question was asked, this would require O(N) 64-bit words.)
#Jorn comments that steps 1 through 3 are a variation on counting sort. In a sense he is right, but the differences are significant:
A counting sort requires an array of (at least) Xmax - Xmin counters where Xmax is the largest number in the list and Xmin is the smallest number in the list. Each counter has to be able to represent N states; i.e. assuming a binary representation it has to have an integer type (at least) ceiling(log2(N)) bits.
To determine the array size, a counting sort needs to make an initial pass through the list to determine Xmax and Xmin.
The minimum worst-case space requirement is therefore ceiling(log2(N)) * (Xmax - Xmin) bits.
By contrast, the algorithm presented above simply requires N bits in the worst and best cases.
However, this analysis leads to the intuition that if the algorithm made an initial pass through the list looking for a zero (and counting the list elements if required), it would give a quicker answer using no space at all if it found the zero. It is definitely worth doing this if there is a high probability of finding at least one zero in the list. And this extra pass doesn't change the overall complexity.
EDIT: I've changed the description of the algorithm to use "array of booleans" since people apparently found my original description using bits and bitmaps to be confusing.
Since the OP has now specified that the original list is held in RAM and that the computer has only, say, 1GB of memory, I'm going to go out on a limb and predict that the answer is zero.
1GB of RAM means the list can have at most 134,217,728 numbers in it. But there are 264 = 18,446,744,073,709,551,616 possible numbers. So the probability that zero is in the list is 1 in 137,438,953,472.
In contrast, my odds of being struck by lightning this year are 1 in 700,000. And my odds of getting hit by a meteorite are about 1 in 10 trillion. So I'm about ten times more likely to be written up in a scientific journal due to my untimely death by a celestial object than the answer not being zero.
As pointed out in other answers you can do a sort, and then simply scan up until you find a gap.
You can improve the algorithmic complexity to O(N) and keep O(N) space by using a modified QuickSort where you eliminate partitions which are not potential candidates for containing the gap.
On the first partition phase, remove duplicates.
Once the partitioning is complete look at the number of items in the lower partition
Is this value equal to the value used for creating the partition?
If so then it implies that the gap is in the higher partition.
Continue with the quicksort, ignoring the lower partition
Otherwise the gap is in the lower partition
Continue with the quicksort, ignoring the higher partition
This saves a large number of computations.
To illustrate one of the pitfalls of O(N) thinking, here is an O(N) algorithm that uses O(1) space.
for i in [0..2^64):
if i not in list: return i
print "no 64-bit integers are missing"
Since the numbers are all 64 bits long, we can use radix sort on them, which is O(n). Sort 'em, then scan 'em until you find what you're looking for.
if the smallest number is zero, scan forward until you find a gap. If the smallest number is not zero, the answer is zero.
For a space efficient method and all values are distinct you can do it in space O( k ) and time O( k*log(N)*N ). It's space efficient and there's no data moving and all operations are elementary (adding subtracting).
set U = N; L=0
First partition the number space in k regions. Like this:
0->(1/k)*(U-L) + L, 0->(2/k)*(U-L) + L, 0->(3/k)*(U-L) + L ... 0->(U-L) + L
Find how many numbers (count{i}) are in each region. (N*k steps)
Find the first region (h) that isn't full. That means count{h} < upper_limit{h}. (k steps)
if h - count{h-1} = 1 you've got your answer
set U = count{h}; L = count{h-1}
goto 2
this can be improved using hashing (thanks for Nic this idea).
same
First partition the number space in k regions. Like this:
L + (i/k)->L + (i+1/k)*(U-L)
inc count{j} using j = (number - L)/k (if L < number < U)
find first region (h) that doesn't have k elements in it
if count{h} = 1 h is your answer
set U = maximum value in region h L = minimum value in region h
This will run in O(log(N)*N).
I'd just sort them then run through the sequence until I find a gap (including the gap at the start between zero and the first number).
In terms of an algorithm, something like this would do it:
def smallest_not_in_list(list):
sort(list)
if list[0] != 0:
return 0
for i = 1 to list.last:
if list[i] != list[i-1] + 1:
return list[i-1] + 1
if list[list.last] == 2^64 - 1:
assert ("No gaps")
return list[list.last] + 1
Of course, if you have a lot more memory than CPU grunt, you could create a bitmask of all possible 64-bit values and just set the bits for every number in the list. Then look for the first 0-bit in that bitmask. That turns it into an O(n) operation in terms of time but pretty damned expensive in terms of memory requirements :-)
I doubt you could improve on O(n) since I can't see a way of doing it that doesn't involve looking at each number at least once.
The algorithm for that one would be along the lines of:
def smallest_not_in_list(list):
bitmask = mask_make(2^64) // might take a while :-)
mask_clear_all (bitmask)
for i = 1 to list.last:
mask_set (bitmask, list[i])
for i = 0 to 2^64 - 1:
if mask_is_clear (bitmask, i):
return i
assert ("No gaps")
Sort the list, look at the first and second elements, and start going up until there is a gap.
We could use a hash table to hold the numbers. Once all numbers are done, run a counter from 0 till we find the lowest. A reasonably good hash will hash and store in constant time, and retrieves in constant time.
for every i in X // One scan Θ(1)
hashtable.put(i, i); // O(1)
low = 0;
while (hashtable.get(i) <> null) // at most n+1 times
low++;
print low;
The worst case if there are n elements in the array, and are {0, 1, ... n-1}, in which case, the answer will be obtained at n, still keeping it O(n).
You can do it in O(n) time and O(1) additional space, although the hidden factor is quite large. This isn't a practical way to solve the problem, but it might be interesting nonetheless.
For every unsigned 64-bit integer (in ascending order) iterate over the list until you find the target integer or you reach the end of the list. If you reach the end of the list, the target integer is the smallest integer not in the list. If you reach the end of the 64-bit integers, every 64-bit integer is in the list.
Here it is as a Python function:
def smallest_missing_uint64(source_list):
the_answer = None
target = 0L
while target < 2L**64:
target_found = False
for item in source_list:
if item == target:
target_found = True
if not target_found and the_answer is None:
the_answer = target
target += 1L
return the_answer
This function is deliberately inefficient to keep it O(n). Note especially that the function keeps checking target integers even after the answer has been found. If the function returned as soon as the answer was found, the number of times the outer loop ran would be bound by the size of the answer, which is bound by n. That change would make the run time O(n^2), even though it would be a lot faster.
Thanks to egon, swilden, and Stephen C for my inspiration. First, we know the bounds of the goal value because it cannot be greater than the size of the list. Also, a 1GB list could contain at most 134217728 (128 * 2^20) 64-bit integers.
Hashing part
I propose using hashing to dramatically reduce our search space. First, square root the size of the list. For a 1GB list, that's N=11,586. Set up an integer array of size N. Iterate through the list, and take the square root* of each number you find as your hash. In your hash table, increment the counter for that hash. Next, iterate through your hash table. The first bucket you find that is not equal to it's max size defines your new search space.
Bitmap part
Now set up a regular bit map equal to the size of your new search space, and again iterate through the source list, filling out the bitmap as you find each number in your search space. When you're done, the first unset bit in your bitmap will give you your answer.
This will be completed in O(n) time and O(sqrt(n)) space.
(*You could use use something like bit shifting to do this a lot more efficiently, and just vary the number and size of buckets accordingly.)
Well if there is only one missing number in a list of numbers, the easiest way to find the missing number is to sum the series and subtract each value in the list. The final value is the missing number.
int i = 0;
while ( i < Array.Length)
{
if (Array[i] == i + 1)
{
i++;
}
if (i < Array.Length)
{
if (Array[i] <= Array.Length)
{//SWap
int temp = Array[i];
int AnoTemp = Array[temp - 1];
Array[temp - 1] = temp;
Array[i] = AnoTemp;
}
else
i++;
}
}
for (int j = 0; j < Array.Length; j++)
{
if (Array[j] > Array.Length)
{
Console.WriteLine(j + 1);
j = Array.Length;
}
else
if (j == Array.Length - 1)
Console.WriteLine("Not Found !!");
}
}
Here's my answer written in Java:
Basic Idea:
1- Loop through the array throwing away duplicate positive, zeros, and negative numbers while summing up the rest, getting the maximum positive number as well, and keep the unique positive numbers in a Map.
2- Compute the sum as max * (max+1)/2.
3- Find the difference between the sums calculated at steps 1 & 2
4- Loop again from 1 to the minimum of [sums difference, max] and return the first number that is not in the map populated in step 1.
public static int solution(int[] A) {
if (A == null || A.length == 0) {
throw new IllegalArgumentException();
}
int sum = 0;
Map<Integer, Boolean> uniqueNumbers = new HashMap<Integer, Boolean>();
int max = A[0];
for (int i = 0; i < A.length; i++) {
if(A[i] < 0) {
continue;
}
if(uniqueNumbers.get(A[i]) != null) {
continue;
}
if (A[i] > max) {
max = A[i];
}
uniqueNumbers.put(A[i], true);
sum += A[i];
}
int completeSum = (max * (max + 1)) / 2;
for(int j = 1; j <= Math.min((completeSum - sum), max); j++) {
if(uniqueNumbers.get(j) == null) { //O(1)
return j;
}
}
//All negative case
if(uniqueNumbers.isEmpty()) {
return 1;
}
return 0;
}
As Stephen C smartly pointed out, the answer must be a number smaller than the length of the array. I would then find the answer by binary search. This optimizes the worst case (so the interviewer can't catch you in a 'what if' pathological scenario). In an interview, do point out you are doing this to optimize for the worst case.
The way to use binary search is to subtract the number you are looking for from each element of the array, and check for negative results.
I like the "guess zero" apprach. If the numbers were random, zero is highly probable. If the "examiner" set a non-random list, then add one and guess again:
LowNum=0
i=0
do forever {
if i == N then leave /* Processed entire array */
if array[i] == LowNum {
LowNum++
i=0
}
else {
i++
}
}
display LowNum
The worst case is n*N with n=N, but in practice n is highly likely to be a small number (eg. 1)
I am not sure if I got the question. But if for list 1,2,3,5,6 and the missing number is 4, then the missing number can be found in O(n) by:
(n+2)(n+1)/2-(n+1)n/2
EDIT: sorry, I guess I was thinking too fast last night. Anyway, The second part should actually be replaced by sum(list), which is where O(n) comes. The formula reveals the idea behind it: for n sequential integers, the sum should be (n+1)*n/2. If there is a missing number, the sum would be equal to the sum of (n+1) sequential integers minus the missing number.
Thanks for pointing out the fact that I was putting some middle pieces in my mind.
Well done Ants Aasma! I thought about the answer for about 15 minutes and independently came up with an answer in a similar vein of thinking to yours:
#define SWAP(x,y) { numerictype_t tmp = x; x = y; y = tmp; }
int minNonNegativeNotInArr (numerictype_t * a, size_t n) {
int m = n;
for (int i = 0; i < m;) {
if (a[i] >= m || a[i] < i || a[i] == a[a[i]]) {
m--;
SWAP (a[i], a[m]);
continue;
}
if (a[i] > i) {
SWAP (a[i], a[a[i]]);
continue;
}
i++;
}
return m;
}
m represents "the current maximum possible output given what I know about the first i inputs and assuming nothing else about the values until the entry at m-1".
This value of m will be returned only if (a[i], ..., a[m-1]) is a permutation of the values (i, ..., m-1). Thus if a[i] >= m or if a[i] < i or if a[i] == a[a[i]] we know that m is the wrong output and must be at least one element lower. So decrementing m and swapping a[i] with the a[m] we can recurse.
If this is not true but a[i] > i then knowing that a[i] != a[a[i]] we know that swapping a[i] with a[a[i]] will increase the number of elements in their own place.
Otherwise a[i] must be equal to i in which case we can increment i knowing that all the values of up to and including this index are equal to their index.
The proof that this cannot enter an infinite loop is left as an exercise to the reader. :)
The Dafny fragment from Ants' answer shows why the in-place algorithm may fail. The requires pre-condition describes that the values of each item must not go beyond the bounds of the array.
method AntsAasma(A: array<int>) returns (M: int)
requires A != null && forall N :: 0 <= N < A.Length ==> 0 <= A[N] < A.Length;
modifies A;
{
// Pass 1, move every value to the position of its value
var N := A.Length;
var cursor := 0;
while (cursor < N)
{
var target := A[cursor];
while (0 <= target < N && target != A[target])
{
var new_target := A[target];
A[target] := target;
target := new_target;
}
cursor := cursor + 1;
}
// Pass 2, find first location where the index doesn't match the value
cursor := 0;
while (cursor < N)
{
if (A[cursor] != cursor)
{
return cursor;
}
cursor := cursor + 1;
}
return N;
}
Paste the code into the validator with and without the forall ... clause to see the verification error. The second error is a result of the verifier not being able to establish a termination condition for the Pass 1 loop. Proving this is left to someone who understands the tool better.
Here's an answer in Java that does not modify the input and uses O(N) time and N bits plus a small constant overhead of memory (where N is the size of the list):
int smallestMissingValue(List<Integer> values) {
BitSet bitset = new BitSet(values.size() + 1);
for (int i : values) {
if (i >= 0 && i <= values.size()) {
bitset.set(i);
}
}
return bitset.nextClearBit(0);
}
def solution(A):
index = 0
target = []
A = [x for x in A if x >=0]
if len(A) ==0:
return 1
maxi = max(A)
if maxi <= len(A):
maxi = len(A)
target = ['X' for x in range(maxi+1)]
for number in A:
target[number]= number
count = 1
while count < maxi+1:
if target[count] == 'X':
return count
count +=1
return target[count-1] + 1
Got 100% for the above solution.
1)Filter negative and Zero
2)Sort/distinct
3)Visit array
Complexity: O(N) or O(N * log(N))
using Java8
public int solution(int[] A) {
int result = 1;
boolean found = false;
A = Arrays.stream(A).filter(x -> x > 0).sorted().distinct().toArray();
//System.out.println(Arrays.toString(A));
for (int i = 0; i < A.length; i++) {
result = i + 1;
if (result != A[i]) {
found = true;
break;
}
}
if (!found && result == A.length) {
//result is larger than max element in array
result++;
}
return result;
}
An unordered_set can be used to store all the positive numbers, and then we can iterate from 1 to length of unordered_set, and see the first number that does not occur.
int firstMissingPositive(vector<int>& nums) {
unordered_set<int> fre;
// storing each positive number in a hash.
for(int i = 0; i < nums.size(); i +=1)
{
if(nums[i] > 0)
fre.insert(nums[i]);
}
int i = 1;
// Iterating from 1 to size of the set and checking
// for the occurrence of 'i'
for(auto it = fre.begin(); it != fre.end(); ++it)
{
if(fre.find(i) == fre.end())
return i;
i +=1;
}
return i;
}
Solution through basic javascript
var a = [1, 3, 6, 4, 1, 2];
function findSmallest(a) {
var m = 0;
for(i=1;i<=a.length;i++) {
j=0;m=1;
while(j < a.length) {
if(i === a[j]) {
m++;
}
j++;
}
if(m === 1) {
return i;
}
}
}
console.log(findSmallest(a))
Hope this helps for someone.
With python it is not the most efficient, but correct
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import datetime
# write your code in Python 3.6
def solution(A):
MIN = 0
MAX = 1000000
possible_results = range(MIN, MAX)
for i in possible_results:
next_value = (i + 1)
if next_value not in A:
return next_value
return 1
test_case_0 = [2, 2, 2]
test_case_1 = [1, 3, 44, 55, 6, 0, 3, 8]
test_case_2 = [-1, -22]
test_case_3 = [x for x in range(-10000, 10000)]
test_case_4 = [x for x in range(0, 100)] + [x for x in range(102, 200)]
test_case_5 = [4, 5, 6]
print("---")
a = datetime.datetime.now()
print(solution(test_case_0))
print(solution(test_case_1))
print(solution(test_case_2))
print(solution(test_case_3))
print(solution(test_case_4))
print(solution(test_case_5))
def solution(A):
A.sort()
j = 1
for i, elem in enumerate(A):
if j < elem:
break
elif j == elem:
j += 1
continue
else:
continue
return j
this can help:
0- A is [5, 3, 2, 7];
1- Define B With Length = A.Length; (O(1))
2- initialize B Cells With 1; (O(n))
3- For Each Item In A:
if (B.Length <= item) then B[Item] = -1 (O(n))
4- The answer is smallest index in B such that B[index] != -1 (O(n))

Resources