I already know how to calculate the median, mean, mode from an array in Java. But is there actually a way to do the reverse like creating an array from the median, mode, mean, given the range and the number of numbers in the array ?
The numbers in the array can be created by randomness as long as it satisfies the condition above. It can stop when successfully find the first array with that condition. And plus the range can be from 0 to 10 or 0 to 100, not so much.
Yes / No
When you do an array to mmm, you are applying a lossy algorithm, you are losing the original data.
There can be literally an infinite number of array combinations that would result in the same mmm.
Some naive ideas
Median: for example your array have 2n+1 elements, put n elements which are less than your median, n elements which are greater than median;
Mean: say your array have m elements, Sum = m * Mean, generate m elements by partitioning Sum;
Mode: count the frequency of the numbers you put into the array, make sure Mode has the highest frequency;
Related
I want to generate 4 random numbers, ranging from 1 through 6 inclusive. Then I want to get the sum of these elements excluding the smallest value.
I am currently creating one stream to populate a list:
List<Integer> values = r.ints(4,1,7).boxed().collect(Collectors.toList())
Then I remove the smallest value and use another stream to get the sum of the values:
values.stream().mapToInt(Integer::intValue).sum();
Can someone suggest a way to perform all these operations in a single stream?
Sort the stream, then skip the first (ie smallest) element:
int sumExceptSmallest = IntStream.of(4,1,7).sorted().skip(1).sum(); // 11
or in your specific case:
int sumExceptSmallest = r.ints(4,1,7).sorted().skip(1).sum();
Note that while this may be the coolest and most efficient for the coder, it is not the most efficient possible solution because the sort has time complexity of O(n log n). The most efficient run time would be a single pass to both find the smallest and compute the sum, then subtract one from the other, yielding the solution in O(n) time.
Using IntSummaryStatistics can calculate this in your stream with a single pass:
IntStream stream = /* random stream of integers */;
IntSummaryStatistics stats = stream.summaryStatistics();
int sum = stats.sum();
sum -= stats.min(); //sum, minus the lowest element
I have a list where I am trying to find the sum of combination of the lists entries, except the entries where both values to add are equal to each other (ie 2+2 would not be added) and add them to another list.
As an example:
[1,2,3] would yield the list of sums [3,4,5] because 1+2=5,1+3=4, and 2+3=5
However, my issues arises with not knowing how many sums will be produced. I am working in java and am limited to native arrays, therefore the size of the array has to be set before I can add the sum values to it.
I know I would not be able to find the exact size of the sum list due to the possibility that a sum would not get added if the two elements are the same, but I am trying to ballpark it so I don't have massive arrays.
The closest 'formula' I have gotten is setting the following, but it is never precisely what the max value would be for any list
(list length of original numbers * list length of original numbers) / 2
I am trying to keep time complexity in mind, so keeping a running count of how many sums there are, setting an array to that size, and looping through the original list again would not be efficient.
Any suggestions?
Can you add same sums to array, I mean, your array is {1,2,3,4,5}. Would you print the both result of 1+5 and 2+4 =6.
If your answer is yes. You can get the length of array and multiply it with 1 less and divide them to 2. For instance; our array → {1,2,3,4,5} the lenght is 5 the length of our result array will be 5*4/2=10.
Or you can use lists in java if you cant define a length for array. Keep in mind.
I need your help. Could you help me please.
Input: Array A with n natural numbers.
count = 0
for each subset S of 4 elements of A do:
sum = "sumFormula" from i = 0 to 3 S[i]
for i from 0 to n-1 do:
if sum == A[i]:
count = count+1
return count.
I didn't understand it. What time does it take?
My idea: I think it has exponential runtime, because when I double the input size it squares. But I am not sure.
A lot of the complexity of the algorithm is hidden.
In particular:
for each subset S of 4 elements in A
How are these subsets determined?
There are 2^n possible subsets of a set with n elements.
Simply that first step might be the cause of the exponential runtime.
The remainder of the algorithm is essentially figuring out the sum of each of those subsets. That won't affect runtime much.
I came across this question in a recent interview.
Given an array find the maximum product that can be obtained multiplying any 3 numbers in the array.
The solution I came up with is,
Sort the array
Multiply the 3 largest number.
This is O(nlogn)
Is there a O(n) solution to the problem?
Instead of sorting all the values, you can retain the top three, which is O(3n) which is O(n)
Doing this in a single pass is likely to be more efficient than three passes.
One way of doing this is to do an insertion sort into an array of 3, discarding the lowest value each time. (you can start at the lowest value in the array)
You can also implement this using a series of if/else comparison to update 3 variables.
How about negatives?
The only complication is if you can have negative values e.g. 5 * -4 * -4 > 5 * 5 * 3
For this reason it would makes sense to search for the three largest and the two most negative. You can check whether the largest * the next two largest or largest * the two most negative is bigger.
what if they are all negative?
In this case, you also need the three smallest negative values as well to get the product closest to positive infinity.
You can do it in O(n) as follows:
Find the largest number in O(n); remove the number from the array (make it zero)
Find the largest number among the remaining numbers in O(n); make it zero as well
Find the largest number among the remaining numbers, again in O(n)
You repeat the same O(n) loop three times, so it's O(3*n), which is the same as O(n) because constants are ignored.
This won't cater to negatives
It is relatively easy to modify this algorithm to work with negative numbers. Each pass through the array needs to find the largest and the smallest negative value, so at the end you would have three large positive numbers P0, P1, P2 and thee large negative numbers N0, N1, N2, in ascending order by magnitude. Now you need to compare P0*P1*P2 vs. N1*N2*P2, and pick the larger one.
There are O(n)-time complexity solutions to this problem, but these will only occur depending on the input of your array, as you can read in this article
You can perform this in 1 pass by storing largest elements from array A to to array of 3 say B.
For each element n in array A if n is greater than any element in array B then replace array B element with array A element.
Finally multiply the 3 elements in resulting array. This is constant time. This may still be 3n since you are checking against array of 3 as well.
For negative it would be additional 2 elements which contain the smallest. So it may be 5n but still constant time
ive been trying to the problem and have become stuck. The specification of the problem is a as follows
Median of k numbers is defined as the (k/2)th
smallest number if k is even; and the ((k+1)/2)th
smallest number if k is
odd. For example, median of the 4 numbers: 2 1 8 7 is the 2nd smallest number i.e. 2, and the median of the 5
numbers: 2 1 8 7 6 is the 3rd smallest number i.e. 6.
In this problem, you'll be given N numbers. Let the kth
median or m(k) be defined as the median of the first k
numbers (1<=k<=N). i.e. the 5th
median or m(5) is the median of the first 5 numbers, the 8th
median or m(8) is the
median of the first 8 numbers, etc. In other words, let Ai
denote the ith
number, then the kth
median or m(k) is
defined as the median of the numbers A1, A2, …, Ak.
Your task is to find m(1) + m(2) + m(3) + ...+ m(n), output the sum modulo 100000
so basically you have to read in numbers, first number is stored in the variable N and dictates how many numbers to be read in after that point is. (this is always 5). Then you must iterate through the reading in of the numbers, storing them in array, then sorting them and then finding the median of the median and then store the value in the and repeating until all numbers have been read in, sorted and median found.
I have managed to read the numbers in fine and i am able to sort them to some degree but my sorting algorithm, sorts them highest values first and smallest values last and in order to get the program to run properly to get the desired output i need it to be the other way round and i cant work out how.
The numbers read in will be like so
5
10
5
1
2
15
and the answer would be 27.
This is my code
Any help on this problem would be amazing because im just going round in cirles, btw the System.out.println(myIntArray [4]) etc is just a tracer to see how the sorting is taking place
You seem to be sorting the entire array each time through. You need to sort from 0 to i (exclusive) at each iteration. Also, for odd length, you need parentheses when computing the subscript: myIntArray[(i+1)/2] instead of myIntArray[i+1/2]. Because of operator precedence, Java will evaluate the latter as myIntArray[i+(1/2)], which is just myIntArray[i], since 1/2 is 0 in integer division.