Find the longest even sum in array [closed] - java

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]));

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.

What the function rpcToken = Math.round(1E8 * Double.parseDouble("0." + (int) (Math.random() * Integer.MAX_VALUE))); do in java? [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 5 years ago.
Improve this question
Hello I an not a java programmer,
I need know what this function does:
rpcToken = Math.round(1E8 * Double.parseDouble("0." + (int) (Math.random() * Integer.MAX_VALUE)));
This just generate a random 8 number string? I need to make this exactly in PHP.
Thank you
Math.random() gives you a pseudorandom number less than 1 but at least 0.
This is multiplied by Integer.MAX_VALUE, which is 2147483647. The result is a number between 0 and 2147483646.
(int) converts this number to an integer.
"0." + converts this to a String, and adds "0." to the beginning of it.
Double.parseDouble converts this String back to a double. So now we again have a number between 0 and 1. But any number greater than 0 but less than 0.1 is impossible due to the way we created the String. "0.9" and "0.900" would both convert to 0.9.
1E8 * multiplies this by 1E8, which is 100000000.
And finally, Math.round rounds this off to the nearest integer.
So, in total, we have a supposedly random number somewhere between 0 and 99999999, but the final result is very skewed. Many numbers are impossible to generate with this code (numbers between 1 and 9999999 seem impossible, but 0 is still possible) and this code will generate certain numbers more often than other numbers (close to half the generated numbers will begin with 1, and more than average may end with 0), and I find it very unlikely that all of this is intentional (although it's impossible to say for sure without knowing the code's purpose.)
If you just need a random 8 digit number, there are easier, faster, and better ways. Why not just multiply that random number you got in the first step by 100000000, and round it off?

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.

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