Printing values using loop - arrays

How can print following values using loop.
Basically i am taking size of array and adding first entry to middle of array if array is of size odd. If it is of even then taking first entry to below to half of array.Just like below i add 1 to 5th location.
Then second entry to first location. Third entry to below of first entry. Fourth entry to below to second entry. Fifth entry to below to Third entry and up to so on.
In simple First entry to center, second entry to top, third entry to below of center and up to so on.
2,4,6,8,1,3,5,7

Convention of pseudocode varies from author to author. This cannot be done in-place. Therefore, you need another array to copy the results.
left = 0, right = res.length/2
for i = 0 to res.length - 1:
if i is even:
res[right] = arr[i]
right++
else
res[left] = arr[i]
left++
arr is your original array and res is the empty array you are populating.

Related

Find all possible combinations of items in a list placed into a list of larger size

I'm programming this in Java but don't worry about the language you choose to respond in. This is more of a logical question.
I've got an array of size n of items say: [a, b, c...]. I've got a second empty array of size p. Note that the empty array of size p will always be larger than the previous array size n. I want to iterate over all the combinations of placements of elements from the first array into the empty array. (Note that items in the populated array will always be in that order. The order they come in cannot change; however, the space between element placing can change.
Examples of combinations are (assume n=3 and p=5):
n = 3 = [a, b, c]
could make:
[a,b,c,_,_]
[a,b,_,c,_]
[a,b,_,_,c]
[a,_,b,c,_]
[a,_,b,_,c]
[a,_,_,b,c]
[_,a,b,c,_]
etc...
I know that I would start by shifting the last element all the way to the end 1 by 1 then shifting the second element over by 1 and repeating the shift of the last element until the second last element is at the end as well and thus requiring the third and final element to be shifted over once and repeat.
The problem I'm having is representing this in code. The sizes of the arrays are variables and not known to me but I know for a fact that n < p. I don't need the number of combinations it can make. I would like to have code that gives me the iteration to make the combinations so I can do further checks.
If anyone could help me represent this in code, it would be extremely helpful.

insert element in array at specific index

I was asked in a interview to insert a element at a index(the index was given) in the most optimized way.
I have already tried this
/* Make room for new array element by shifting to right */
for(i=size; i>=pos; i--)
{
arr[i] = arr[i-1];
}
arr[pos-1] = num; /* Insert new element at given position and increment size */
size++;
Is there any better way than this to do?
you can do the following optimization:
-put iterator on last element's position and iterate from right to left until you reach pos and apparently maintaining variable last which keeps the last new encountered element
-on every iteration, check if current element is equal to last, if yes then skip it, if no then copy current element to current_index+1 and assign current element to variable last
-finally assign new element to pos
As we see, this approach is based on assumption that comparison is cheaper than shift (i.e. it is better to do comparison on every iteration and shift element from time to time, instead of shifting ALL elements from pos till end) but I'm not quite sure on what extent this assumption is true - considering that moving an element would require two assembly operations involving memory (from memory addr. to register and back to new memory addr.) vs. one operation involving memory (from memory addr. to register) and one arithmetic operation for comparison
The worst case is when there are no equal elements adjacent to each other - in that case we will do both comparison and shift on every iteration.

Merge Sort: Why are there no remaining elements in the right part?

Using code from this link, the merge function does not have a while loop for the remaining j elements from the right part of the temp (helper) array into the original (numbers) array.
It still works fine. I was wondering if someone could help explain why j always reaches high (because of which there are no remaining elements on the right part of the helper array which needs to be simply copied into the numbers array).
The function merge(low, middle, high) starts from copying the elements of numbers to the helper array.
That is why numbers array already has the elements and there is no point to copy them back from the helper.

How to re-arrange elements of Array after Deleting

Recently I was reading a Programming book and found this question:
I have an array :
array = [2,3,6,7,8,9,33,22];
Now, Suppose I have deleted the element at 4th position i.e. 8 .
Now I have to rearrange the array as:
Newarray = [2,3,6,7,9,33,22];
How Can I do this. And I have to also minimize the complexity.
Edit I have no choice to make another copy of it.I have to only modify it.
You can "remove" a value from an array by simply copy over the element by the next elements, that's easy to do with memmove:
int array[8] = {2,3,6,7,8,9,33,22};
memmove(&array[4], &array[5], sizeof(int) * 3);
The resulting array will be {2,3,6,7,9,33,22,22}.
And from that you can see the big problem with attempting to "remove" an element from a compile-time array: You can't!
You can overwrite the element, but the size of the array is fixed at time of compilation and can't actually be changed at run-time.
One common solution is to keep track of the actual number of valid elements in the array manually, and make sure you update that size as you add or remove elements. Either that or set unused elements to a value that's not going to be used otherwise (for example if your array can only contain positive numbers, then you could set unused elements to -1 and check for that).
If you don't want to use a library function (why not?) then loop and set e.g.
array[4] = array[5];
array[5] = array[6];
and so on.
Do this, just use these two functions and it will work fine
index=4;//You wanted to delete it from the array.
memcpy(newarray,array,sizeof(array));
memmove(&newarray[index], &newarray[index + 1], sizeof(newarray)-1);
now the newarray contains your exact replica without the character that you wished to remove
You can simply displace each element from the delIdx(deletion index) one step forward.
for(int i=delIdx; i<(arr_size-1);i++)
{
arr[i]= arr[i+1];
}
If required you can either set the last element to a non-attainable value or decrease the size of the array.

Worst-case time for adding 1 element to expanding array

Suppose we have an expanding array that doubles in size when we try to add something to it but it is filled.
According to the bottom of the slide "Amortization: Expanding Vectors" the worst-case time for adding one element is 2N, not N? Why is this?
I thought it would be N because copying N elements to the new, double sized array, would take N time.
For example, say I'm adding just one element to a filled array of size 4. It would go like this:
Make new array of size 8 (double of 4).
Copy all of elements of original array to new array (copy 4 times).
Set 5th element of the new array to the additional element (copy 1 time).
So that would copy elements 5 times, which is N + 1, not 2N?
The operations needed for adding an element to an exanding array is O(2N) because first you need to go through the whole array and check for empty space with O(N). Because this is the worst case there isn't any and you have to create a new array and copy the whole content from the first to the new array with O(N). Both operations combined result in O(2N).
When you think of array length the length is actually the size that is reserved for the array and not the number of elements inside. That is why you need to loop through the whole array for an empty space if you did not save the number of elements anywhere. I am speaking of 'empty space' because it is not stated that just elements were added and none were deleted inside the array.

Resources