(A*B)>0 condition cannot be checked if A & B are large.
How to find check this condition for (A,B) lies between -2147483648 to 2147483647
provided the solution is optimal
Simply divide the result by one of the operands (unless that is zero), and check whether the result of the division is the other operand.
Related
If lowerBound is greater than upperBound your code will crash
Why does this run (nothing will be outputted, but still runs, also, floating point can't be inputted so "<1" would seeming be "0"))...
for i in 1..<1 {}
But this doesn't...
for i in 1...0 {}
?
The former expresses "starting at 1 and incrementing by one, if the value is less than one, include it." That is a set with no values in it, but it's a reasonable thing to say.
The later expresses "starting at 1 and incrementing by one, until the value equals 0, include it." That is really a set of all positive integers, but in practice you definitely didn't mean that, and it is is instead explicitly defined to be an error.
Another way to say the same thing is to consider the former to be all integers greater than or equal to 1 and also less than 1. Again, that's an empty set.
The latter is a set of all values greater than or equal to 1 and less than or equal to 0. That's also an empty set, but almost certainly not what you meant, so it's defined to be an error.
The error message tells you the answer:
Can't form Range with upperBound < lowerBound
That is the only rule that matters.
How the range gets formed is irrelevant to that rule. It doesn't matter whether the operator that forms the range is ... or ..<. All that matters is that when we try to obey the operator and instantiate the range, it must turn out that the upper bound is not smaller than the lower bound.
Well, in 1..<1, the upper bound is not smaller than the lower bound. So it's a legal range.
It is also an "empty" range; it contains no integer (neither 0, nor 1, nor 2, nor any other integer). But it's still a range.
Now, if you think about it, that's a very valuable thing. It doesn't look valuable in your example, because you're using literals. But when the lower bound and upper bound come from variables, it's a very good thing that lo..<hi doesn't crash in the corner case where lo and hi happen to be equal! That case arises a lot, and for good reasons.
For example, consider cycling thru the elements of an array. If the array is empty, its indices are (you guessed it) 0..<0. You want it to be legal to cycle thru this array. Nothing happens, but it's not illegal. And that's just what this rule says.
Design_picture
As in the graph, is this possible?
So I am trying to check if the N-bit input is zero or not.
I thought of doing this, ORing every bit in N-bit and then following it with Not-gate, and so if all bits are zero, Or gate would generate a 0 output, but yet I am not sure, would how would OR-gate access every bit? I am really confused! How to OR every bit in the N-bit? How does it work? without knowing whats N?
And how can I check if the most significant bit is 1?
Thanks!
you can use shift operator along with & operator (concept of masking with 1). Right shift the bits until the number becomes 0 and in each step do Anding with 1.Check at each step if the result after Anding with 1 is 0 or non zero.
Here each row contains a bit representation of a number.These numbers come from 1..N Exactly one number is missing.Find the bit representation of the missing number.
The interviewer asked me this question.
I said: "We can find the sum of the given numbers and subtract it from the sum of first n numbers(which we know as (N*(N+1))/2)"
He said that involves changing from base 10 to base 2.
Can you give me a hint on how I can solve it without changing bases?
You can XOR together all numbers from 0..N range, then XOR the numbers from the array. The result will be the missing number.
Explanation: XORing a number with itself always results in zero. The algorithm above XORs each number exactly twice, except for the missing one. The missing number will be XOR-ed with zero exactly once, so the result is going to equal the missing number.
Note: the interviewer is wrong on needing to convert bases in order to do addition: adding binary numbers is easy and fun - in fact, computers do it all the time :-)
You can just XOR these numbers together, and XOR with 1..n. The fact that the numbers are stored in binary is a good hint, BTW.
In fact, any commutative operator with a inverse should work, since if the operator is commutative, the order does not matter, so it can be applied to the numbers you have and 1..n, with the difference being the first one is not operated on the number that is not in the array. Then you can use its inverse to find that number, with the two results you have. SO + and -, * and /, XOR and XOR and any other operators that meets the requirement all should work here.
I was trying to write an algorithm for given problem:
we are given a set of numbers- {n1,n2,n3,n4,n5......}
and we have to check that can we derive a number(Say X) using addition and subtraction by given numbers. X will always be less than all elements of the given set.
Eg.
Set: {2,3,4,6,9}
given number: 1, Result: Yes
9-4-4 =1
Set: {3,4,6,9}
given number: 2, Result: Yes
6-4 = 2
Thanks in advance.
Effectively you are looking for the ideal generated by the numbers in your set. The intergers form a principal ideal domain, which means every ideal is generated by a single integer. All you have to do is find this single integer -- say g -- and check whether X can be devided by g. Finding g is also easy -- it's the greatest common divisor of all elements in your set, which can be found using the Euclidean algorithm.
You example sets can generate every integer by addition and substraction, since the can generate 1. For example for the set {3,4,6,9} you have 1=4-3, and any integer n can be written as n times the sum of 4-3.
Assuming, from your first example, that you can use a number multiple times.
The given number must be a multiple of the GCD of your set. That is the only condition. It doesn't matter how big it is.
If you only want an Yes/No answer then it is sufficient to find the GCD. If you also want an expression for the given number the problem can be replaced with finding an expression for the GCD.
GCD = X+Y+..+Z-T-U-...-V
Given an array, A, containing, in some order, all but one of the k-bit non-negative integers find the missing integer with the following constraint:
You are allowed to do only two things with one of these integers:
Obtain the value of its ith bit
Swap them with one another in the array
For example: if k is 3 the array will contain all but one the integers 0 through 7 inclusive, the task is to find the missing integer.
You should check that each bit (for example, if k = 3 you need to check that all 3 bits) are present 2^k-1 times as 0 & 1.
If you are sure of the invariant of the list that you outlined (only one number is missing and there are no duplicates) - sure that it always holds - then you could simply check only one, arbitrary bit holds the condition outlined above.
For example, say that the invariant holds, k = 3, and you have the list [0-7], excluding 6.
For each number in the list, get its first bit (least significant) and do the following:
if bit value = 0 then zeroValues++;
if bit value = 1 then oneValues++;
zeroValues should equal oneValues and both should equal 2^k-1, in this case 4.
Edit: Rereading your question, you're looking for the entire number. In order to find the whole integer value that you are looking for, just do the procedure for all bits. For each bit you do it, you will find either a 0 value or 1 value missing. The missing value is the bit value in the result integer. Doing this for all bits will find the entire bit representation of your integer.
Hint: Can you find out if the missing number is even or odd?
For a hint for a totally different approach, can you sort the array?