How to calculate permutations for a collection, not a set [closed] - java

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 8 years ago.
Improve this question
Suppose we have the following collection of integers: {1,1,2}. We can arrange the order of this collection in 3 possible ways:
1,1,2
1,2,1
2,1,1
How can we calculate the number of ways we can arrange a collection of integers in general? Suppose the size of the collection is very large (10^5 in worst case scenario) but the answer is always small enough to fit into a long. Does an efficient solution to this problem exist, and if so, how could one implement it in Java?

Suppose you have n integers made from n_i copies of integer x_i.
To work out the number of arrangements simply compute
t = n!
However, as you don't care about how numbers with the same value are arranged, for each value of i reduce the total by:
t = t / n_i!
In you case, you have 3 integers, with 1 copy of 2, and 2 copies of 1.
You compute:
t = 3! = 6
t = t/1! = 6/1 = 6
t = t/2! = 6/2 = 3
so the answer is 3.

How can we calculate the number of ways we can arrange a collection of integers in general?
Loop through the collection, counting the number of times each number appears in the collection.
The formula for the number of permutations is size factorial / number of duplicate integers factorial. In your example of 1, 1, 2, the size is 3 and the number of duplicate integers is 2.
3! / 2! = 3 * 2 * 1 / 2 * 1 = 3;
Another, more computer friendly way of calculating 3! / 2! is to divide out the 2 factorial first, which leaves you with 3.
If the collection has no duplicate integers, then the number of permutations is size factorial.

Related

Get identical Array from increments to 2 values [closed]

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 last year.
Improve this question
I want to find the minimum number to be subtracted to get from an array like this
[8, 10, 5]
to an array like this
[3, 3, 3]
I am only allowed to change the values of TWO indexes (in this case either index 0 and 1 OR index 1 and 2). I am only allowed to subtract values and have to subtract the same value over both indexes(ex. If I wanted to subtract 5 I'd have to do it on two indexes, either index 0 and 1 or index 2 and 3).
Edit: Clarification #1: There is no "base value". Just that all indexes are identical through the lowest number of subtraction values (not operations, but values subtracted.)
Clarification #2: At any given time, I am only allowed to subtract the same value from 2 indexes, and only 2
I am not sure of how to tackle this problem, any smallest hints and advice would be greatly appreciated.
If you want to make all the values identical, that means your lowest and highest value should be equal.
for example, in your array
arr[] = {8,5,10};
You need to first sort it.
arr[] = {5,8,10};
Now, you see that the difference between the highest and lowest is 5,
therefore you subtract 5 from the 2 maximum elements
arr[] = {5,3,5};
then again you sort it
arr[] = {3,5,5};
now the difference between maximum and minimum 2, therefore you subtract it from the maximum 2 elements present in your array .
and your array becomes this
arr[] = {3,3,3};
The idea is to bring the maximum number closest to the smallest number and that happens if you subtract the difference between them from the maximum number.

How do I split an indice of an array into two different pices, I.E to validate that neither number is odd/even? [closed]

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 2 years ago.
Improve this question
I have to check, roomNumber[i], if BOTH numbers (assuming it's two-digit) are not even, and not odd. Only then can I accept the number. Also need to determine if the second digit is not less than double the first. I assume that I need to some how split these into two different ints (a,b), but I'm not sure.
If you assume that you have only two-digit numbers, you can go with it this way:
int[] roomNumbers = new int[] { 12, 34, 56, 67, 78 };
for (int num : roomNumbers) {
int second = num % 10;
int first = (num - second) / 10;
System.out.println("first: " + first + " second: " + second);
}
You take the second number using the modulo (%) operator. To get the first number, you need to subtract the second number and divide it by 10.
Now you can process those numbers further.
Your questions have nothing to do with arrays. Let's reword them:
How do I test if a number is even?
You can google this and easily find the answer. Hint: use the modulus operator (%).
How do I test if the second digit is not less than double the first digit
Break this into smaller pieces: First you need to separate out the first and second digit. Again, you can google this if you don't know. (Hint: it's very similar to the first question. You can use the % operator and division (/).
Then you have to compare these according to the rule. Try to write something on your own and see what you can come up with.

Find minimum number of integers >=2 that divide all array elements [closed]

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 2 years ago.
Improve this question
I have to find minimum number of integers, (where each such integer>=2) that divide all array elements.
For example, if the array is [2,4,6,8,10] then the answer is 1 (because 2 divides all array elements).
Another example, if the array is [2,3,4,9] then the answer is 2 (because we need at least 2 different numbers, 2 and 3, that satisfy the conditions).
I decided to generate all sub-arrays of the given array, store in a 2D array, then sort all sub-arrays in descending order of their sizes. Then iterate through the 2D array, and as soon as a number that divides all elements of a sub-array is found, return the index of that sub-array+1.
So, for the first example above, the first sub-array to be checked would be the main array itself (after sorting), and a number (2) will be found that divides all its elements. So, the return value would be index+1=0+1=1. However, this does not always work. Language is Java.
For every number the prime factors are decisive: 12 = 2 * 2 * 3, so {2, 3} is needed knowledge.
Having such a list of sets, prime factors, you need to find a set of numbers that then make up all sets.
Such an algorithm you work out on paper, how you would do it. And then code your algorithm out.
For instance a set with only one factor 49: {7} would imply, that 7 must be a categorizing number.
For a minimum between ambiguous choices, say {2, 3}, {3, 5}, {5, 7} you have to think of something.
Good luck.

Number of the binary strings without consecutive zeroes of a certain length correlates with Fibonacci numbers. [closed]

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 6 years ago.
Improve this question
I'm trying to develop algorithm which would determinate number of binary strings without consecutive zeroes of a certain length. I found solution, which is based on Fibonacci sequence. I don't understand, how the binary string, that ends at the 0 or 1 and not contains repeated 0's depends of Fibonnacci sequence. Can anybody explain it?
For example, for length 3 answer will be 5, because:
000
001
*010
*011
100
*101
*110
*111
*Strings without consecutive zeroes
Let's Z(k) is number of valid binary strings, ending with 0. Denote such string *0
Let's O(k) is number of valid binary strings, ending with 1. Denote such string *1
We can build *0 with length k+1 only with adding 0 to the end of *1, so
Z(k+1) = O(k)
We can build *1 with length k+1 adding 1 both to the end of any *1, and to the end of any *0, so
O(k+1) = O(k) + Z(k)
Consider all valid strings with length (k+2)
F(k+2) =
Z(k+2) + O(k+2) =
O(k+1) + O(k+1) + Z(k+1) =
O(k) + Z(k) + O(k+1) + Z(k+1) =
F(k) + F(k+1)
Do you see Fibonacci-like relation?

How can I print the biggest number unless one is missing [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a loop that checks the biggest number so far and adds one to it; but for example 1 2 3 4 5 I press button 6 is created, what if the list was 1 2 3 5 6 and I want it to print 4 so I can get a correct number line then proceed to increase it by one?
EDIT: I should mention that my list has a possiblity not being in order so 2 1 3 5
You should check incrementally that the numbers are good:
So say you have an array of int called numbers[]
you could do:
int placehodler = numbers[0] + 1;
for (int i = 1; i < numbers.length(); i++){
if (placeholder != numbers[i]) // checks if the next number is equal to the previous + 1
break; // breaks out of the loop if it isn't
placeholder++; // increment placeholder by 1
}
System.out.println(placeholder); // prints placeholder
So if the placeholder breaks out of the loop early, it is your 1,2,3,5,6 situation and it would have 4 (succeeds on the loop where placeholder = 3, fails when placeholder = 4) in the 1,2,3,4,5 situation, it would succeed on all and end the loop with a value of 6.
First, I would find the count of your current list. If your list is [1 2 3 5] the count is 4.
Then, I would create a new list that is a range of numbers from 1 to the count => [1 2 3 4]
comparing the two lists will let you see if any numbers are missing, if no numbers are missing then you can add 1 to the current count and append that to the end of the list.
sorting the list before doing any of this will take care of the out of order issue mentioned in the other comment.

Categories

Resources