Summing up of corresponding elements of two arrays or more - arrays

Enter a number: 10
Even Numbers: 0 2 4 6 8 10
Odd Numbers: 1 3 5 7 9
Prime Numbers: 2 3 5 7
Sum : 3 8 14 20 17 10
i wanted to sum up the corresponding elements of the array like above what should i do???

Please update your question to clearly explain what you have and what you need / where it is going wrong. If you have 3 arrays for example, then keep an i = 0, and increment this to max(max(len(arr1), len(arr2)), len(arr3)) - meaning the max count of the 3 arrays.
A for loop can go from i=0 to i=max-value-above and then you can generate a new array by summing up the values of each array at that index i, taking care that you don't go out of bounds for each array.

Related

Find the last smaller or equal number for every element in the array?

I have been given an array. I need to find the last (right-most) smaller or equal number for every element in the array.
For example:
2 5 1 6 4 7
2 has last smaller or equal number 1,
5 has last smaller or equal number 4 and not 1, etc.
Another example:
5 100 8 7 6 5 4 3 2 1
Here, every element has last smaller or equal number 1. I know the naive approach, i.e. O(n²), but need a better approach.
You could go from right to left and build a min array of the minimum number until now.
For your example 2 5 1 6 4 7, it would be:
Start at rightmost position:
7
4 7 (4 < 7)
4 4 7 (6 > 4)
...
So the min array for your example would be: 1 1 1 4 4 7
Now for each query, you start at the same position in the min array and go right until finding a number which is greater:
For 2:
2 5 1 6 4 7
1 1 1 4 4 7
^
------^
First element greater than 2 is 4, so return number just before = 1
For 5:
2 5 1 6 4 7
1 1 1 4 4 7
^
----------^
First element greater than 5 is 7, so return number just before = 4
To find efficiently the first element greater for each element in the input array you can use upper_bound algorith (example in C++ http://www.cplusplus.com/reference/algorithm/upper_bound/) to find the first element which is greater
Upper_bound takes log(N) time, so overall time to process every element in input array is O(NlogN)
Memory complexity is linear for the min array

HeIp understanding Fibonacci Search

On the internet I only find code for the algorithm but I need understand in form of text first because I have trouble understand things from code only. And other description of the algorithm are very complicated for me (on Wikipedia and other sites).
Here is what I understand for far:
Let say we want search in array the element 10:
Index i 0 1 2 3 4
2 3 4 10 40
Some fibonacci number here:
Index j 0 1 2 3 4 5 6 7 8 9
0 1 1 2 3 5 8 13 21 34
First thing we do is find fibonacci number that is greater-equal to array length. Array length is 4 so we need take fibonacci number 5 that is in index position j=5.
But where we divide the array now and how continue? I really don't understand it.. Please help understand for exam...
The algorithm goes in the following way:
The length of the array is 5, so the fibonacci number which is greater than or equal to 5 is 5. The two numbers which are preceding in the Fibonacci sequence are 2 [n-2] and 3 [n-1] - (2, 3, 5).
So, arr[n-2] i.e. arr[2] is compared with the number to be searched which is 10.
If the element is smaller than the number, then the sequence is shifted 1 time to the left. Also, the previous index is saved for next iteration to give an offset for the index. In this case, since 4 is smaller, n-2 becomes 1 (1, 2, 3). arr[1 + 2(prev)] = arr[3] = 10. So, the index of the number is 3.
If the element is larger, the sequence is shifted 2 times to the left.
Always the min(n-2+offset,n)th element is compared with number to get the matching result.

Reshape and rearrange array

I have a large array, which looks like this:
1
4
5
3
6
2
7
4
3
I want to rearrange this array that it looks like this:
7 4 3
3 6 2
1 4 5
My original array has the size 13700x1, so I cannot do it manually and if I use the reshape function, the array gets shaped in the wrong way:
1 3 7
4 6 4
5 2 3
I hope my intention is clear. Thanks!
Try
tmpArray = [1
4
5
3
6
2
7
4
3]
flipud(reshape(tmpArray, 3, 3).')
x = [1,4,5,3,6,2,7,4,3]';
A = flipud(reshape(x,3,3)');
The other answers assume your vector contains a square number of elements, 4, 9, 16 .... This is true for the example vector, but not for the one you're actually working with (it's 13700x1 according to the question).
This means that the flipud(reshape()) approach will give an error:
Product of known dimensions, 3, not divisible into total number of
elements, 13924.
This is not a problem if you don't want a square matrix, as numbers that can be represented as a product of any of the numbers: 2, 5, 137.
If you want a square matrix, you need to pad the vector with zeros, NaNs or something else. This can be done the following way:
A = randi(100,13700,1); %% Random 13700x1 matrix
n = numel(A); %% Number of elements in A (13700 in this case)
elements = ceil(sqrt(n))^2; %% Number of elements needed in order to make a square matrix
B = [A; zeros(elements-n,1)]; %% Pad the vectors with zeros.
%% You can also d0 B = [A; nan(elements-n,1)];
final_matrix = flipud(reshape(B, sqrt(elements),[]).'); %% Final operation

Need some simple logic help, been stuck for a few hours

The problem is asking to take any amount of numbers, and find the highest possible sum of difference(using absolute value) between consecutive numbers. For example numbers 1 2 and 3 would be arranged 3 1 2 to get a sum of 3 (3-1 = 2, and 1-2 = 1).
Now my first thoughts were to take the highest number in the list followed by the lowest number and arrange in that way through the end, but that doesnt work out as the end of the list will end up having all of the numbers in the middle accumulating almost no differences. The only other thing I have thought of is to find every single possible order and return the highest sum, but with a longer list this will take way too long and I assume there might be a better way.
For reference here are some sample input and output numbers
9 2 5 3 1 -> 21
7 3 4 5 5 7 6 8 5 4 -> 24
Any help at all would be much appreciated, even if its just pointing me in the right direction.
There are 2 approaches to this problem.
Approach 1:
Brute force.
Approach 2:
Figure out an algorithm for how to arrange the numbers.
I always like approach 2 better if it is feasible.
It seems reasonable that you would get a high sum if you order the numbers high-low-high-low-high...
So start by sorting the numbers and then divide them into two equally large groups of low and high numbers. If there is an odd number of numbers the middle number will be left over.
Then you just pick numbers alternately from the two groups.
It is easy to prove that the order of the interior numbers doesn't matter as long as you stick with the high-low-high-low ordering.
However, since the start and end number only has one neighbour, the first and last number should be the middle numbers.
Finally, if you have an odd number of numbers, place the last number at the start or end, whatever gives the biggest difference.
Example:
7 3 4 5 5 7 6 8 5 4 -> [sort] -> 3 4 4 5 5 5 6 7 7 8
high numbers: 5 6 7 7 8
low numbers: 3 4 4 5 5
Arranged:
5 3 6 4 7 4 7 5 8 5 = 24
Example:
9 2 5 3 1 -> [sort] -> 1 2 3 5 9
high numbers: 5 9
low numbers: 1 2
left over: 3
Arranged:
3 5 1 9 2 = 21 (3 goes at the start, because |3-5| > |3-2|)

Two-Dimensional Sorting Lower Bound

Given: an arbitrary array of n^2 unsorted numbers
Output: n x n matrix of the inputs, where all rows and columns are sorted.
For example, if n = 3, n^2 = 9, and the array is {1,2,...,9}
Possible outputs include:
1 4 7
2 5 8
3 6 9
and
1 3 5
2 4 6
7 8 9
So I know that this problem can be done in O(n^2log n) time simply by sorting the n^2 numbers and then using the first n numbers as the first row, the second n numbers as the second row, and so on, but how would I prove a matching lower bound, i.e. Ω(n^2log n)?

Resources