This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Check if a number is divisible by 3
Is it true that a binary number is divisible by 3 iff it has an even number of ones?
like 11000 is divisible by 3 whereas 1110 is not.
No - there is a trick but it's a little more complicated than that - you have to count the number of 1s at even positions and the number of 1s at odd positions. See e.g. Check if a number is divisible by 3.
No, that's wrong. For example 5_dec = 101_bin is not divisble by 3. To check for divisbility by three, you have to count the number of ones in even position and substract the number of ones in odd positions. If the difference is divisble by three, the original number is divisbilble by three (which, in turn, can be checked by reiterating the same rule).
Related
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 5 years ago.
Improve this question
Hackers love bits, so does Alex. Alex has just started his career as hacker and found a special binary array
A
A (containing
0
s
and
1
s
).
In one operation, he can choose any two positions in the array, and can swap their values. He has to perform exactly one operation and maximize the length of the subarray containing only
1
s
.
As Alex is a newbie in his field, help him for the same, and output the required length of the sub array containing only
1
s
1s.
Input Format:
First line consists of one integer
N, denoting the number of elements in the array.
Second line consists of N space-separated integers, denoting the elements of the array.
Output Format:
Print the required length of the sub array containing only
1
s
1s.
Input Constraints:
1 ≤ N ≤ 100
1 ≤ N ≤ 1000 ≤ A[i] ≤ 1
Input:
5
1 1 1 0 1
Output:
4
General algorithm:
Have a variable, inSub, that keeps track of the substring you are looking at. Its value will be -1 at first, indicating that you are not looking at a particular substring atm.
Iterate through the string until you find a 1. The index of this 1 will be the new value of inSub.
Also have a variable, hitZero (initialized to False), which keeps track of if the current substring has encountered a zero yet. This is because one zero can be replaced by a one, assuming there is another one in the whole list. If a zero is hit and hitZero is False, it turns to True. If hitZero is already True, you will need to store the substring's length in a list SubList, inSub will be set to -1, and hitZero will be False again.
At the end of this you will have a list of substring lengths. If there are multiple substrings in the list, the answer will be the longest substring (because you can take a one from one of the other substrings). If there is only one substring, the answer will be the longest contiguous string of 1's in the substring. (Edited)
Let me explain you what thought process you can use to solve this problem.
Whenever you see a problem, try to come up with some test cases in your mind.
For this particular question, any of the following mentioned test cases could be a good starting points.
Example 1:-
Input:
1 1 1 1 0 0 0
Output
4
In the above example, you can clearly see that there is no swap possible and therefore the output would be 4.
Example 2:-
Input
1 1 1 1 1 1
Output
6
In the above example also no swap is possible, as there is no sub-string separated by 0, so your answer is basically equal to the length of the whole string.
Example 3:-
Input
1 1 1 0 0 0 1 1 1 1
Output
5
In the above example, you can see one swap of one from the first contiguous strings of one with the zero present near the other strings will give you, your answer.
So now you have three good examples covering most of the corner case.
Look at the comment below and above algorithm to get a good view about how to solve this problem.
Thanks to Carl for reviewing my algorithm.
Hope this helps!
This question already has answers here:
How to check if a number is a power of 2
(32 answers)
Closed 8 years ago.
What is the fastest (in asymptotic worst-case time complexity) algorithm for determining if a sum of arbitrary positive integers is a power of two?
One cute bit twiddling trick is to test if x&(x-1) is equal to 0.
Note that you need to decide what to do if x is equal to 0, this test will mark 0 as a power of 2 so you may want an exception for this case.
Subtract 1 from the sum and perform a bitwise AND with the original number. Powers of 2 will have a result of 0.
This question already has answers here:
Creating a random number generator from a coin toss
(4 answers)
Closed 8 years ago.
Given a boolean random number generator. How can I use it to generate random number in the range 0 to n.
Set the bits of the number one after the other. Say you use the generator 10 times. That'll give you 10 chances to have either '0' or '1' in each round. You'll now generate a random number between 0 to 1023 inclusive.
To get a random number from 0 to n you'll need to use the generator lg(n) times (lg = log base 2).
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
This is an interview question, not a homework.
N friends are playing a game. Each of them has a list of numbers in front of himself.
Each of N friends chooses a number from his list and reports it to the game administrator. Then the game administrator sorts the reported numbers and shouts the K-th largest number.
I must the count all possible numbers that the game administrator can shout.
For example, if N = 3 and K = 3, and the lists for the 3 friends are 2 5 3, 8 1 6, 7 4 9. The output is 6, since the possible values are 4 5 6 7 8 9.
Can anybody suggest a decent algorithm for this problem? What I am doing is to create all possible permutations taking one number from each list, then sorting the resultant and printing the kth-largest. But that takes too much time.
To know if a number is present in the result or not, you need to know for each other list if there are numbers above and if there are numbers below. List where there are both numbers above and below are not a problem as you can choose a number in them as it suits you. The problem are lists where there are only numbers above or only numbers below. The first ones must be at most N-K, the second ones must be at most K. If this is not true, your number cannot be picked. If this is true, you can always choose numbers in the lists where there are both number above and below so that your number is picked.
This can be checked in linear time, or even better if your first sort your lists, thus giving an overall complexity of O(n.log(n)) where n is the number of numbers in total. Without sorting you got a n² complexity.
In your example with lists :
{2 5 3}, {8 1 6}, {7 4 9}
say we are looking for the 2-greatest number. For each number we ask if it can be shout by the administator. For each of them we look if in the other list there are both numbers below and numbers above. Let's look further for some of them
For 5 : there is numbers above and below in both other lists. So "5" can be shout by the administrator.
For "2" : there is numbers above and below in the second list so I can freely choose a number above or below in this list. But there are only numbers above in the third list, so the picked number in this list will always be greater. Since i can freely choose a number below in the second list, thus making my "2" the 2nd greatest number.
For "1" : there is only numbers above in the second list, so "1" will always be the smallest element.
For "9" : this is the other way round, it is always the greatest.
take the smallest number from each set. find the K-th -largest of these. This is the smallest number that is in the result. Similarly, find the largest number in the result. Prove that Each number between the two is in the result.
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
Wiki http://en.wikipedia.org/wiki/Mathematics_of_Sudoku says Sudoku has 6,670,903,752,021,072,936,960 possible permutations.I tried to find out but it seems difficult.Can someone tell me how this number is calculated.
You can find all about it in this Wiki: http://en.wikipedia.org/wiki/Mathematics_of_Sudoku.
"the number of valid Sudoku solution grids for the standard 9×9 grid was calculated by Bertram Felgenhauer and Frazer Jarvis in 2005 to be 6,670,903,752,021,072,936,960 . This number is equal to 9! × 722 × 27 × 27,704,267,971, the last factor of which is prime. The result was derived through logic and brute force computation."
You can read the most recent rewrite of the original publication by Bertram Felgenhauer and Frazer Jarvis : Mathematics of Sudoku, it details the computation over 7 pages. The calculation actually isn't trivial (the idea being to enumerate distinct and valid Sudoku grids, rather than all possible arrangements of digits over a 9x9 grid).
Interestingly there was an estimation of the number of possible sudokus posted in an internet forum before the actual value was calculated and published by Felgenhauer & Jarvis.
The author of the post points out that there are some unproven assumptions in his guess. But the estimated value differs by 0.2% from the actual value published later.
In this Wiki you can find some estimation of other types of sudoku based on similar guesses.
Here is the full post from The New Sudoku Players' Forum:
by Guest » Fri Apr 22, 2005 1:27 pm
Lets try this from a whole different direction:
Step A:
Pretend that the only 'rule' was the 'block' rule, and that the row and column rules did not exist. Then each block could be arranged 9! ways, or 9!^9 ways to populate the puzzle (1.0911*10^50 'solutions').
Step B1:
If we then say 'let us add a rule about unique values in a row', then the top three blocks can be filled as follows:
Block 1: 9! ways
Block 2: 56 ways to select which values go in each 3-cell row, and 3! ways to arrange them (remember that we haven't invented a column rule yet).
Block 3: with 1 and 2 filled, the values that go in each row is now defined, but each row can be arranged 3! ways.
Therefore, we have 9! * 56 * 3!^6 ways to fill the top three blocks, and this value cubed to fill all nine blocks. (or 8.5227*10^35 solutions). Note that this represents a 'reduction ratio' (denoted as R) of 1.2802*10^14, by adding this one new rule.
Step B2: But we could have just as easily added a 'unique in columns' rule, and achieved the same results downward instead of across, with the same value of R.
Step C: (and here is where my solution is not rigorous) What if we assume that each of these rules would constrain the number of valid solutions by exactly the same ratio? Then there would be a combined reduction ratio of R^2. So the intitial value of 1.0911*10^50 solutions would reduce by a factor of R^2, or 1.639*10^28, leaving 6.6571*10^21 valid solutions.
This post and the account are attributed to Kevin Kinfoil (Felgenhauer & Jarvis).
Additional notes
Assume the Block 1 is
1 2 3
4 5 6
7 8 9
Then we have the following possibilities for Block2, if we ignore the order of the rows
1 2 3 4 5 6
4 5 6 7 8 9
7 8 9 1 2 3
this is 1 possibility
1 2 3 7 8 9
4 5 6 1 2 3
7 8 9 4 5 6
this is 1 possibility
1 2 3 two of 4,5,6, one of 7,8,9 3*3
4 5 6 the two remaining of 7,8,9, one of 1,2,3 3
7 8 9 the two remaining of 1,2,3, the remaining of (two of 4,5,6) 1
these are (3*3)*3*1=27 possibilities
1 2 3 two of 7,8,9, one of 4,5,6 3*3
4 5 6 two of 1,2,3, the remaining of 7,8,9 3
7 8 9 the two remaining of 4,5,6, the remaining of two of 1,2,3 1
these are (3*3)*3*1=27
So all in all these are 1+1+27+27=56 possibilities.