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.
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 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.
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 4 years ago.
Improve this question
I have been given a question that I have been stuck on for quite a while.
Question:
Given a sorted array of N integers, divide the array in to a maximum of R adjacent and non-overlapping subarrays with a maximum of M elements, so that we can minimize the difference between the largest and smallest valaue within each subarray.The output should contain the maximum difference within any of the subarrays after minimizing the difference in each of the subarrays.
Example:
N = 7
R = 4
M = 3
Original Array: [1,2,3,3,3,5,6]
Optimal subarrays(1 possible case): [1], [2], [3,3,3],[5,6]
Correct Output: 1
I was thinking of testing every possible value for the minimum difference and testing each value in O(N) time, but this would lead to a runtime more costly than nlogn.
What would be the most time and memory efficient solution to solve this problem?
I suggest using bisection to find the largest difference for which it is possible to divide the array in the desired way.
To test if the division is possible, greedily assign elements to subarrays starting from the left while the constraints are met.
Only start a new subarray when forced (either by the difference getting too large, or by reaching the maximum number of elements allowed in an array).
I believe this greedy assignment should always find a solution if one exists for a particular value of difference.
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.
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
Given a sorted array of both positive and negative numbers (Example:-9, -7, -4, -0.9, 1, 2, 3, 8) i need to output the elements in the array in sorted order of their absolute values in less than O(N^2) without using any inbuilt function.
Does anyone know any acceptable solution for this simple problem?
I was thinking of modifying the quicksort algorithm to make it check the abs values for elements.
I would binary search for 0, conceptually split the list into 2 parts, and merge the two lists (the negative value one as the positive value of itself) into a single new one by walking the negative in reverse and the positive forward.
O(log n) for binary search, O(n) for the merge of 2 sorted lists.
Pick any 2-way comparison sorting algorithm of your pleasure with runtime bounds less than O(N^2). Quicksort is a valid choice.
When doing comparisons (which will show up in the 2-way comparison sorting algorithm), instead of comparing the values a and b compare abs(a) and abs(b). Just make sure that you don't replace a and b with abs(a) and abs(b), just use the latter two when doing comparasons
Just have two pointers at the end of the arrays then compare the absolute value with the end.
start == array[0]
end == array.length
while start != end
if abs(start) => end
put start in front of end
start++
end--
else
end--
I might be missing some pieces but that's the idea.
0(n) solution.
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 9 years ago.
Improve this question
SHORT STORY :
I need a 8x8 matrix, that can have many solutions (a
well known existing problem)
Solutions as in, it should have only 8 positions which are 1, rest 0.
A good example can be 8 queen problem.(Placing 8 queens in such a way that they don't kill each other)
Queen problem
so we can create a matrix, which will have 64 positions in all,and 1 represents the current queen position, and 0 a blank position.
LONG STORY:
I am creating an algorithm for Steganography which needs a 8x8 matrix(say A) to store the pixels of an image(8 pixels at a time,8 bits each).
Another 8x8 matrix say B, is to be created which contains data such that Only 8 places should have a 1,rest 0.
These bits are then collectively mapped to matrix A, to check and evaluate an 8 bit resultant
ASCII valued string.
So, I can make many combinations like that, say combinations of 8 kings on a 8x8 chessboard so that the don't kill each other.But it's not a well known,or a tricky problem.
Any ideas for creating such a matrix? The idea can be from anywhere, not generally related to a chessboard.
Consider the 8x8 fields as the adjacency matrix of a graph with 8 nodes and 8 edges
Enumerate all these graphs
Find "interesting" properties of these graphs.
Example: "Has a chromatic number of 3"
Example: "Contains at least / at most / exactly A nodes with degree B"
...
Profit
One way to proceed is to start with a unit matrix (i.e. 1 entries only in the main diagonal and 0 entries elsewhere) and to randomly shuffle the rows.
Since you're not going into detail about the problem itself, I guess what you are asking for is a data structure to accommodate the matrix, and not the algorithm to solve the problem.
A valid datastructure would be a two-dimensional array int[8][8] or even boolean[8][8], since you only need two states per field.
If you're asking for a datastructure that verifies the rule of not more or less than eight marked fields, you can implement a wrapper class around such array that contain the logic to validate.
I might have grossly misunderstood you though.