It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Suppose Alice has picked three distinct integers and placed them into a stack S in random order. Write a short, straightline piece of pseudo-code (with no loops or recursion) that uses only one comparison and only one variable x, yet guarantees with probability 2/3 that at the end of this code the variable x will store the largest of Alice's three integers. Argue why your method is correct.
Data Structure on Java
x = S.pop()
return max(S.pop(), x)
explanation:
I got a S stack [A, B, C]
x = S.pop() // x points to A
return max(S.pop(), x) // compares B to A
once I know that the greatest is returned from B and A there is one possibility that the greatest integer is not between those two integers (B and A) that is C
so.. I have two possibilities of getting the greaters number between three -> 2/3
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
So, I have created a program that runs in two parts. Firstly, I generate a list of 500 random numbers and store them in a text file. Secondly, I am required to find the max, min, average, etc. However, I am also supposed to find how many consecutive numbers come up. This is where my problem lies. I am not sure how to approach it.
Looking for a place to start.
Thank you
As you go along the array you will keep the list of the following things: current largest seen element, current smallest seen, total of all elements seen so far, ..., the last element seen & total count of consecutive numbers. If current number == last element seen + 1, you increase the consecutive count. There should also be a flag for the first element in the consecutive sequence, so that, say, {1, 2} counted 2 occurrences instead of one
last_seen = -1
previous_consecutive = false
for(x in numbers_list):
if x == last_seen + 1:
if not previous_consecutive:
number_of_consecutive_elements += 1
number_of_consecutive_elements += 1
previous_consecutive = true
else:
previous_consecutive = false
last_seen = x
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
i need help in solving the following test question (it's not a homework or assignment)
the user can input from the console values from 1-9 and the program accepts 3 numbers one number per line.
if the user inputs the following:
3
2
4
the output should be:
1
2 2
3 3 3
1
2 2
1
2 2
3 3 3
4 4 4 4
i don't have any experience with trees, so please advise about what kind of tree is that and where can i start to accomplish the above program (i need some hints and advises that will help me to make this program)
thanks in advance.
No tree data structure is required. The general features of your program would be the following:
Read the three inputs
For each input n, use a for loop from 1 to n to print n lines using n as the output values
(The bit harder part:) On each line, you will also need to generate the required spacing. This will involve a calculation on your part based on n and the max value of n. Since n is a single digit, you won't have to take into account n taking up more characters once it's >= 10.
Define a 2D char array that will hold the digits. Start from the top middle. Use recursion.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need help thinking up a formula for finding the factors of a number:
Write a method named printFactors that accepts an integer as its
parameter and uses a fencepost loop to print the factors of that
number, separated by the word " and ". For example, the number 24's
factors should print as:
1 and 2 and 3 and 4 and 6 and 8 and 12 and 24
You may assume that the number parameter's value is greater than 0.
Please don't give me a COMPLETE program as I would like to try it out myself.
The current code I have has a for loop to control the number of "and's" appearing, however, I printed out the last number by itself since I don't want a "24 and" attached to it...
So the output looks something like this at the moment:
"1 and 2 and 3" (I haven't yet thought up the equation hence the 1,2,3...)
I'm currently thinking that the factors requires a % kind of formula right? Will I need division? I was also thinking of printing out 1 and whatever the number (in this case, 24) you are finding factors for, since 1 and the number itself are always factors of itself.
What else am I missing??
Thanks in advance!! :)
I'm currently thinking that the factors requires a % kind of formula right?
Yes.
I was also thinking of printing out 1 and whatever the number (in this case, 24) you are finding factors for, since 1 and the number itself are always factors of itself.
If you test every number from 1 to n (e.g. from 1 to 24) then 1 and the number itself don't need to be special cases (because they'll simply satisfy your ordinary "% kind of formula").
Maybe 1 is a special case because it doesn't have the word "and" in front of it.
What else am I missing??
This may be more complicated than you want, but to find all the factors of n you only need to loop up to the square root of n.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Enter number: " " <- if I enter 7 here, the output will be 7x7 box of asterisk. But the condition is Inside that box of asterisk, there is a Diamond.
*******
*** ***
** **
* *
** **
*** ***
*******
What is the Program for this using an array? "import java.io.*......"
I won't give full code because this looks like homework, but here's an outline of one possible algorithm you could use.
For each position (i, j) in your grid calculate the Manhattan distance from the center of your diamond. If it exceeds the "radius" of the diamond, print a * otherwise print a space.
If the center of the diamond is (x, y) the Manhattan distance to the position (i, j) is given by this formula:
int distanceFromCenter = Math.abs(x - i) + Math.abs(y - j);
Note that if you use the formula for the Euclidean distance instead of the Manhattan distance you will get a circle instead of a diamond, though it might be difficult to see the difference between these two shapes at a 7x7 resolution.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Given an array of integers, give two integers from the array whose addition gives a number N.
This was recently covered on the ihas1337code blog. See the comments section for solutions.
Essentially the most efficient way to solve this is to put the numbers in a hash_map and then loop through the array a second time checking each element x if element (N - x) exists in the hash_map.
You can optimize a bit from there, but that is the general idea.
Follow these steps:
1.Sort the numbers using merge sort in O(n logn) in descending order(can be ascending also but for this text assumed them to be sorted in desceending order).
2.Use two pointer variables one pointing to starting element (say p1) and other pointing to last element( say p2).
3.Now add *p1 + *p2 ( temp_sum= *p1 + *p2 ) and compare it with required sum
Repeat these steps untill p1>p2
i.If sum ==temp_sum then our job is over .
ii.If sum > temp_sum then decrease p2 to make it point to a bigger value then its current value so that temp_sum can increase.
iii.If sum < temp_sum then decrease p1 to make it point to a smaller value then its current value so that temp_sum can decrease.