Get identical Array from increments to 2 values [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 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.

Related

Find the longest even sum in array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I'm trying to solve a question which takes an array of size n, and find the longest range of numbers in the array which have an even sum.
For example, for the array: [2,3,1,8,4,7,2] the value 5 will be returned, as the range of the first five numbers are the longest range to make an even sum.
I got an algorithm that works, but it's the bonus I'm looking to go into.
The request is to only read from the array's value once and only once.
My solution included summing up the entire array, and then from start to finish find the maximum length, and then from finish to start.
But that's reading the array 3 times.
Can you maybe guide me in the right direction? Thanks!
The sum of two odd numbers is even and the sum of two even numbers is even, so a subarray has an even sum if and only if there are an even number of odd numbers. Thus, we can find the answer by keeping track of the index of the first odd number, the index of the last odd number, and the number of odd numbers. If there are an even number of odd numbers, the entire array's sum is even. Otherwise, we can get maximum length of the subarray from the index of the first odd number plus one to the end of the array and the subarray from the front of the array to one less than the last index of an odd number. These subarrays are both the largest possible with an even number of odd numbers, since one odd number is excluded from each one.
function longestEven(arr) {
let firstOdd = -1,
lastOdd = -1,
oddCount = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 1) {
if (firstOdd === -1) firstOdd = i;
lastOdd = i;
++oddCount;
}
}
return oddCount % 2 === 0 ? arr.length :
Math.max(arr.length - (firstOdd + 1), lastOdd);
}
console.log(longestEven([2, 3, 1, 8, 4, 7, 2]));

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.

How to calculate permutations for a collection, not a set [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 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.

Sort absolute values of array in less than O(n^2) JAVA [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 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.

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