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).
Related
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 2 years ago.
Improve this question
I created a same sized - stack based simple calculator for adding 2 operands. I want to know whether my addition becomes incorrect when specific (valid) values are entered. The 2 stacks take integer values and are having the same number of digits (i.e. 400 [3 digits] and 900 [3 digits]).
It depends on the algorithm you're using. From the question it is not clear, but let's assume your calculator can perform basic arithmetic. First off, you want to test each operation separately, because they have different equivalence classes of their inputs. For example, for multiplication it would be: 0, 1, minimum and maximum values, and their negations. Testing almost always will not be exhaustive, but using equivalence classes, you can pick one value from each class to make sure that each class is covered with a test.
Back to your question, you may use min/max values, and anything that you think may break your code.
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 4 years ago.
Improve this question
The thing is I need to create an unidimensional array that represents a certain number of objects.
Those objects are organized as shown in the picture.
Link
And I have to be able to tell wich one is conected to.
The number of objects is the only thing given.
Is there any algorithm of some sort to do this?
This sort of organization is often used to implement heaps in arrays: https://www.geeksforgeeks.org/array-representation-of-binary-heap/
You just put the objects into the array in level order (top 1 first, then the 2 from level 2, then the 4 from level 3, etc.).
Assuming 0-based indexing, then, the object in array[i] has children array[2*i+1] and array[2*i+2].
If your array starts at [1], then the object in array[i] has children array[2*i] and array[2*i+1]
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.
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
I am actually new to java libraries. I dont know what this code is doing.
I have been given a task to optimize this. Any help will be useful.
I mean to ask whether str.equals(local) checks for entire string aur for particular index value in the for loop.
public void fnc(String str, int[] ar1)
{
String local = "findnumber";
for(int i=0; i<ar1.length; i++)
{
if(str.equals(local) && ar1[i] * 2 > 10)
{
Integer ip = new Integer(ar1[i]);
ip = ip * 2;
System.out.print(ip.toString());
}
}
}
The big O complexity of the algorithm is O(N) where N is the array size. You cannot improve on that ... for the arguments provided and producing the same output.
There are some things that can be done to improve efficiency though.
Hint: look for a computation that is performed on each loop iteration that could be performed once.
Hint: look for some unnecessary object creation1
Hint: unnecessary use of a reference type.
There are one or two other questionable micro-optimizations, but see if you can spot them without any hints. (I say "questionable" because I suspect that the JIT compiler would do the same optimization itself.)
One final note: the actual speed of the code will be dominated by the print statement, and its ability of the OS to write stuff to (for example) the console. And probably by JVM startup / warmup effects ... unless the fnc method is called many times.
1 - Notwithstanding anything else, new Integer(...) is the wrong way to convert an int to an Integer.
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 :-)