The meaning of target value of Leetcode Search in Rotated Sorted Array - arrays

The original problem is like this:
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
The link is here https://oj.leetcode.com/problems/search-in-rotated-sorted-array/
I don't know the meaning of the 'target value' here. Is it the value we want to find or something else? Why it is given to me?

Is it the value we want to find or something else?
Yes, for example, if you have rotated array:
4 5 6 7 0 1 2
and you are given number 6, you should return 2 - the index of 6 in the array (assuming indexes start from 0). If you are given number 8, which doesn't occur in the array - return -1.

Related

How to find minimum moves to set all elements in array to 0?

I encountered a problem of counting minimum moves to set all elements in array to 0. An array consist of integer values from 0 to 6 (including 6) and the conditions are:
It is possible to change one or more subsequent values in one step (move) by the same value e.g. change values from index 5 to 8 (so indexes 5, 6, 7, 8) by adding 2 but it is forbidden to change indexes 5, 6, 8 (without 7, so they are not subsequent) by adding 2 in one step (I would have to change 5, 6 firstly and then in next step 8 to do it correctly).
Array consist of values from 0 to 6 e.g. when value is 5 and you add 3, it will be set on 1 because 6 is maximum and with value higher than 6 it ,,goes from start (0)" or if value is 1 and you substract 2, finally it will be set on 6 because 0 is minimum and with value lower than 0 it ,,goes from end (6)". In other words -1 will be equal to 6, -2 equal to 5, 8 equal to 1.
I have to write algorithm, which solves described problem and have no idea how should I start.
I tried to count occurrences of every number and then in first step set values to 0 in array from first occurrence to last but using this approach in some cases answer is wrong. For example array with values {1,3,2,2,2}. Step 1 - set 2 to 0 {1,3,0,0,0} and then in two separated moves set 1 and 3 to 0 what finally gives 3 moves. Valid answer is 2 moves because in first step values from 3 to last 2 should be decreased by 2 - {1,1,0,0,0} and then in next move decrease first two values by 1 - so 2 moves.

Array pattern issue to maintain uniformity

There is an existing array of size 64 that has values 6 values distributed as 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 ...
Please see the image for complete data.
The number of occurrence of 0 in the array is 11 times (at every 6th index), 1 is 11 times ... where as 4 and 5 occurs 10 times each.
There is a necessity to reduce the occurrence of any of these numbers [0 to 5] to a lesser number that could be any number from 0 to 10.
For example, it could be to reduce occurrence of 0 to 6 and 1 to 9.
I am looking for a solid idea to do this. Certainly all the numbers are to be evenly distributed and not something like 0 0 0 0 0 2 2 2 2 2 2 ...
I tried to find the index/position where the reduced value has to filled (64/occurrence of 0 or 2). But at times the index collide with each other and thus is not robust one.
From the example I quoted above, number of occurrence of 0 must be changed to 6 and occurrence of 1 to 9, the result after my algorithm is below -
New location to fill 0 = (Array size)/(new occurrence of 0) = 64/6 = ~10th index
New location to fill 1 = (Array size)/(new occurrence of 1) = 64/9 = ~7 index
For filling 6 0's and 9 1's, first the array is reset after which each of the values are filled to maintain balanced distribution.
After filling 6 0's, the array would be come like this:
Then, after filling 9 1's, the array would be come like this:
The index at 55 already has value 0 and apparently 8th 1 also index to 55 that creates a collision. So I believe, this algorithm to balance the distribution does not work.
How do I populate 6 's, 9 1's and rest of the numbers {2, 3, 4, 5} in the array in a balanced way?

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.

Qsort for a particular range in C?

I have to q sort an array but for a given range for ex.
Given array
4 5 3 7 2 1
and then i have the range 2 & 5 this means i have to sort from index 2 all the way through 5.
Resultant array
4 5 2 3 7 1
I know we can set one bound
like
qsort(array,4,sizeof(int),compa)
this will sort the array till 3rd index but will always start from index 0. I want to start the first bound by a desired value. Any Suggestions??
Just pass address to the middle of the array.
qsort(array + 2, 5 - 2, sizeof(int), compa);

Matlab : Matrix indexing Logic

i am doing very simple Matrix indexing examples . where code is as give below
>> A=[ 1 2 3 4 ; 5 6 7 8 ; 9 10 11 12 ]
A =
1 2 3 4
5 6 7 8
9 10 11 12
>> A(end, end-2)
ans =
10
>> A(2:end, end:-2:1)
ans =
8 6
12 10
here i am bit confused . when i use A(end, end-2) it takes difference of two till first column and when there is just one column left there is no further processing , but when i use A(2:end, end:-2:1) it takes 6 10 but how does it print 8 12 while there is just one column left and we have to take difference of two from right to left , Pleas someone explain this simple point
The selection A(end, end-2) reads: take elements in last row of A that appear in column 4(end)-2=2.
The selection A(2:end, end:-2:1) similarly reads: take elements in rows 2 to 4(end) and starting from last column going backwards in jumps of two, i.e. 4 then 2.
To check the indexing, simply substitute the end with size(A,1) or size(A,2) if respectively found in the row and col position.
First the general stuff: end is just a placeholder for an index, namely the last position in a given array dimension. For instance, for an arbitrary array A(end,1) will pick the last element in column 1, and A(1,end) will pick the last element in the first row.
In your example, A(end, end-2) picks an element in the last row two columns before the last one.
To interpret a statement such as
A(2:end, end:-2:1)
it might help to substitute end with the actual index of the last row/column elements, so this is equivalent to
A(2:3, 4:-2:1)
Furthermore 4:-2:1 is equivalent to the list 4,2 since we are instructing to make the list starting at 4, decreasing in steps of 2, up to (minimum) 1. So this is equivalent to
A([2 3],[4 2])
Finally, the following combination of indices is implied by A([2 3],[4 2]):
A(2,4) A(2,2)
A(3,4) A(3,2)

Resources