This question already has answers here:
Quickest way to find missing number in an array of numbers
(31 answers)
Closed 8 years ago.
Can someone explain what this question is asking for?
Array A contains n‐1 unique integers in the range [0,n‐1] and there is one
number from this range that is not in A. Design an O(n) algorithm for finding that
number. You are allowed to use only O(1) additional space besides the array A itself.
Does this question means the length of the array is (for example: 5). And array contains = {0,1,x,3,4}. Find x?
What is O(n) and O(1)? How do I find the missing number in the array
using O(n) algorithm?
Help is appreciated. Thanks.
Does this question means the length of the array is (for example: 5). And array contains = {0,1,x,3,4}. Find x?
Yes. (More or less)
What is O(n) and O(1)?
Read your algorithmics text book. Or your lecture notes. Or http://en.wikipedia.org/wiki/Big_O_notation.
How do I find the missing number in the array using O(n) algorithm?
That would be solving the problem for you!
Hint: what is the formula for the sum of 1 to N?
Does this question means the length of the array is (for example: 5). And array contains = {0,1,x,3,4}. Find x?
not exactly: if n was 5, the array would contain four unique naturals from 0 to 4 (n-1): {3, 1, 4, 0}.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
How to find the maximum difference between two numbers in an array using single loop and single iteration?
Ex: Consider an array A[20]={10,3,6,8,9,4,3} How to find the maximum difference between two numbers in the array using single loop and single iteration?
To solve this problem in a single loop consider how the numbers A and B that produce maximal difference A-B influence the difference:
The difference will be greater when you pick larger A, and
The difference will be greater when you pick smaller B
Once you make this observation, it becomes clear that you are looking for the largest A and the smallest B in order to achieve the maximum difference. This can be done in a single loop in O(n) time and O(1) space.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am new to C and would appreciate your help in a code.
I need to allocate dynamically an array and check which prefix in the string is the shortest that it's concatenation builds the string, and print it and it's length.
Here are a few examples for the outputs:
for "ababab", the output should be: "ab" of length 2
for "aaaa", the output should be: "a" of length 1
for "abcaabca", the output should be: "abca" of length 4
for "abcdefg", the output should be: "abcdefg" of length 7
for "acacaac" the output should be "acacaac" of length 7
My problem is that I don't know how to build the function that supposed to to it. The cases in which the string contains only the same letter, or all letters are different from each other are fine, but I don't know how to take care of all the other cases.
I can't use another string for this code, but I'm allowed to use other pointers
to help me.
Thanks
I will not write the effective code, but the idea itself.
First, this "prefix" of yours has a length that is a divisor of the big length. So if the length i of the prefix doesn't divide the length n of the string then it should be skipped.
To check if a length i is valid, then you need to compare the characters from positions j and j+i, for all possible values of j.
It can be pretty simple, two nested loops, the outer one being a for, from 1 to length/2, and the inner a while, using a pointer increased in each step by the value of the iteration variable of the outer loop. Inside the loop you can use strncmp() to compare the a proper substring, and break the loop if comparison fails. If all comparisons are OK, you have found the "shortest prefix", so you can break the (for) loop. The for loop can be optimized too, by skipping the lengths that are not divisors of the input string length.
This doesn't require making a copy or copies.
And yes, I will leave you actually write the code.
You said you cannot use another string, hopefully You are allowed to use a small temporary array of booleans to count the number of different characters in the string.
1- Find the number of different characters in the string, using an array of boolean. Let this number be x.
2- check if the first x characters constitute a prefix. (check each pointer position p against p+x, p+2x, etc). If unsuccessful, the shortest prefix is the whole string.
In between, you can make it faster by quickly checking if x divides the length of the string..
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Given a number how to recognize if it is bleak or supported by some number in efficient manner?
Given an array of numbers, how to check efficiently whether each number is supported with in the
array or bleak if not supported with in the array?
Brute force : Find binary equivalent, count number of 1's and search for it in the array.
About Bleak and supported numbers:
For each number, count the number of ones in its own binary representation, and add this count to itself to obtain the value of the number it supports. That is, if j is the number of ones in the binary representation of m, then m supports m+j.
Example:number eight (1000 in binary) supports nine, whereas nine supports eleven.
However, in this way not all the numbers get supported; some are left without support, and these numbers are called bleak. For example since one supports two, two supports three and three supports five, there is no number less than four, which would support four, so four is bleak.
If n is not bleak, it must be supported by a number in the range n-ceil(log2(n)) to n-1. This gives a very small range you have to check. For the array, first sorting the array then using the same principle should give you an efficient solution.
Denote a as the given array and count(x) as the number of 1 bits in x.
Question 2:
Iterate through the array and save the the number a[i] + count(a[i]) into a binary search tree. Time: O(n log n)
Iterate through the array and output "Supported" if a[i] is in the binary search tree and "Bleak" otherwise. Time: O(n log n)
Note: a[i] is the current element at the iteration.
This question already has answers here:
Separate the alphabet and digit such that their relative order remains the same in O(n) time and O(1) space
(2 answers)
Stable separation for two classes of elements in an array
(3 answers)
Closed 9 years ago.
Given an array that contains alphabets and numbers, provide an algorithm to move the numbers to the front of the array and alphabets to the end of the array without changing their order in the given array.
Expected Space complexity: In place
Expected Time complexity: O(n)
E.g:
Input:
{1,2,a,3,b,c,4,d,5,e}
Output:
{1,2,3,4,5,a,b,c,d,e}
I came across this question in a website and couldn't figure out an algorithm that satisfies the space and time requirements. Can anyone please tell if it is possible to solve it inplace in O(n) time with the algorithm?
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to generate a random number from within a range - C
I'm looking for something I can use in C that will give me a random number between a and b. So something like rand(50) would give me a number between 1 and 50.
From the comp.lang.c FAQ: How can I get random integers in a certain range?
You can use either rand or random to get an arbitrary random value, then you can take the result of that and mod it by the size of the range and then add the start offset to put it within the desired range. Ex:
(rand()%(b-a))+a
You need to use rand() and srand().
http://linux.die.net/man/3/rand