How can I go through several possibilities without exhausting loops? [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 7 years ago.
Improve this question
How can I go through several possibilities without exhausting loops in a program that sum of the proper divisors(divisors including 1 but not itself) of the number is greater than the number, but no subset of those divisors sums to the number itself.
For Example:
12=(factors sum)1+2+3+4+6
Atleast Any Subset Sum should be equal to the number i.e 12 in this case.
Question link
:http://www.practice.geeksforgeeks.org/problem-page.php?pid=301
Approach:
Step1 (Doubt)
By looking at the problem I found that its optimal solution will be Dynamic Programming.(Since I don't know that).
Apart from that I thought of the solution like this
For Example 12=1,2,3,4,6(divisors)
So,Solving it through like this with 1 starting having all permutations: 1+2+3+4+6 or 1+3+4+6 or 1+4+6 or 1+6(checking at each step whether it is <=12.
Similarly,I checked starting with 2: 2+3+4+6 or 2+4+6(I got and moved out of the loop)
The solution I am thinking here is very long.I have to put seperate loop for each number and also I am saving the divisors in a string.
Can anyone give me "Hint" how to start the problem without the Dynamic Programming(Dp) approach as I am learning Dp these days.

I assume you have a method
int[] getDivisors(int number)
the hard task here is to evaluate the possible sums.
An easy but long way would be, to iterate over all of those like that.
Think of such a sum as a binary code, 1 if its in sum, 0 if its not.
Now you can write a for-loop which, from 0...0 to 1...1 (in binary) with #1s = divisors.length;
Maybe a better way would be recursion methods. Like:
boolean hasSameSum(int number, Stack<Integer> divisors, int sum){
if(sum==number)
return false;
if(divisors.isEmpty())
return true;
int div = divisor.pop();
return hasSameSum(number, divisors, sum)&&hasSameSum(number, divisors, sum+div);
}
But honestly, that's not a programming problem, but more a mathematical approach problem :-)

Related

Using threads for insertion-sort [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 days ago.
Improve this question
I am using insertion-sort to sort the k first values in my array,
First i start by sorting the k first in decreasing order, after that i check the [k+1,n] values if they are larger thatn the lowest number in k, being k-1.
the check is done with threads, where I give every thread a segment of [k+1,n], where each of them will check the values in their segment if they are larger than k-1, if yes they will set the value in the correct place.
My problem is that the parallel version is MUCH slower than if I do it in a sequence, like 100x slower, the test i gave was makin k = 100 and n = 10milion.
Anyone know if you can parallel insertion sort?
If code is needed I can post

New thread per each boolean true combination [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 1 year ago.
Improve this question
I have a doubt about what would be the better way to achieve this:
I have a Class that contains 5 boolean variables (example: 11111, being one combination). Now, the thing is that I'm gonna start a new thread per each true combination, meaning that if i have 11000 i want to start a thread for 11000, another for 10000 and the last one for 01000.
I have to know all the values of the input combinations. The most obvious and inefficient way would be just comparing all the 32 (in this case) combinations (00000, 00001, etc.) and starting only when and AND of that and the actual value (11000) is != 0, that would be the cases that i previous mentioned (11000, 10000 and 01000).
In that case I'm gonna have to do 32 comparisons every single time. The thing is that if then i have 6 booleans now i have to do 64, and so on.
Anyone can think of a better strategy to "capture" every combination?
I think the only way to do it faster is through pre-calculation.
So, String[][] arr; and for (String a : arr[5]) {/* thread creation etc */} should be used.
Where '5' it is '00101'.
Also might helps: Integer.toBinaryString(5) and (int) Long.parseLong("00101", 2).

What is string frequency [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 5 years ago.
Improve this question
Write a java code to check if the given string is even or not? Eg. aabbcc, aacbbc is even string.
I was asked this program in one interview. Actually i did not understand what is frequency here.
For a string s of length n, consider s[0] XOR s[1] ... XOR s[n - 1] where [i] is the (i)th letter of the string. Use java.lang.String#charAt(int) in java to extract a character.
If that is zero you have an even string, else you have an odd string.
Test n % 2 first for an immediate pay rise: If that is not zero then there must be at least 1 occurrence of a character that appears an odd number of times.
Normally folk who wrote computer games in machine code as kids in the 1980s will ask this question as it seems obvious to them. I doubt it is any more: XOR was a very fast way of writing sprite images.
Depending on what the interviewer was asking, string frequency is either,
how many times a string is found in another string.
how many times a character is found in a string.

how to get the largest sum of indexes in array that have a negative numbers in 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 5 years ago.
Improve this question
hy , i'm a beginner java student and we were asked to take the sequence of indexes with the largest sum , the array have negative and positive numbers and may contain 0
our job is to get the largest sum of indexes that are in a row and it can be one index , with method not with algorithm because we haven't learned that yet
thank you for your help
I don't want to do your homework for you but here's some tips:
How do you know if a sequence is the largest?
When should you stop counting a sequence?
If the next number in the sequence is a negative is your sum getting larger?
How would you brute force this problem?
Do you have to brute force it? (No. Look at points 2 and 3 and think about it)
You should be able to figure it out from the questions above. If not I would definitely go ask your professor to get some help.
Now that you've figured it out, here are some questions to further your understanding:
What if I only wanted the largest 3 numbers in a row? 5? 7?
What if all of the numbers are negative? Positive?
If the parameters for question 5 and 6 were added into your question, even though you might think that they narrow down the problem, it actually makes it much harder to solve because of the constraints. This is why we need algorithms, so that we can solve problems with a set of constraints without having to brute force.

java boolean array and turning every third value to false [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 8 years ago.
Improve this question
The Boolean array is initialized to true. I need to know how to turn every third value to false and also going through the array over and over. It is essentially duck duck goose without the randomness.
This sounds like a beginners programming exercise, so I'm going to just give you a hint or two:
Go back to your textbook, lecture notes, tutorial and reread the stuff on for loops. Focus on the old style ones.
Think about how to write a for loop that steps through integer index values in the pattern that you require.
Re "... going through the array over and over" - not sure what you mean, but maybe the hint for this is to think about using nested loops; i.e. a loop inside another loop.
But the most important advice is to try and work this out for yourself.
Well I'm really not sure what you mean by going through the array over and over but the following code will turn every third value to false.
for (int i = 0; i < myVar.length; i++) {
if (i % 3 == 0) {
myVar[i] = false;
}
}
Edit: Oops someone beat me to it while I was typing lol.

Categories