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

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.

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.

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

Searching the binary table [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 7 years ago.
Improve this question
I have a question: Can some one give me an example of searching the binary table to find the change from 1 to 0? For example, one line have
0 0 0 1 1 0 0 0 0 1 1 1 0 0
and it should give me 2 changes. I want to search only one line.
for(int i=1; i< binarytableline.length; i++){
if (binarytableline[i - 1] == 1 && line[i] == 0) changes++;
}
Remember that the indexes start at zero:
(Java docs)
Essentially, whats happening in the code above is that we are looping through the line. Now, i is the index. Lets start at the beginning of the loop.
i=1
So, binarytableline[1-1] = the first index of the binary table line, or the very first number. Now, we are seeing if it equals 1, and the second index, which is i, equals 0. To check, we do the following:
binarytableline[i - 1] == 1 && line[i] == 0
This would mean that there is a change in binary digits from one digit to the next, and in our example that is the 0th index to the first. Now, we would iterate our variable, changes by 1 by doing changes++. Again, this is in a for loop, meaning we will loop through all the elements like this. The amount of times it has changed will be recorded in int changes.
Let me know if this helped,
Ruchir

Print 2 ArrayLists Side by Side [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have created 2 ArrayLists of the same size (5) and i want to print out their contents side by side in columns.
For example:
list 1 = [1 , 2 , 3 , 4 , 5]
list 2 = [5 , 4 , 3 , 2 , 1]
I want to print it out to the console like this:
1 5
2 4
3 3
4 2
5 1
I have tried using a for loop within another for loop but I think I may be overthinking it.
Since, you need the lists to print side by side nested loops are not required.
for (int i = 0 ; i < list1.size(); i++) {
System.out.printf("%d\t%d\n", list1.get(i), list2.get(i));
}
Notice, the loop assumes that the two lists are of the same size. So, we need only one loop counter.
Output :
1 5
2 4
3 3
4 2
5 1

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.

Categories

Resources