I am trying to solve a complex problem on HackerRank.com that involves creating a solution that accepts both small and large arrays of data ranging from 10 integers to 99,000 integers in length.
Find the problem here -> https://www.hackerrank.com/challenges/array-and-simple-queries
The Problem
How to put this simple is that I have take a array, copy a range of numbers from that array that the user specifies, then append it to a new array.
i = 2
j = 4
a = [1, 2, 3, 4, 5, 6, 7, 8]
for numbers in range(i, j + 1):
b.append(a[numbers - 1])
The range of numbers is appended to the b[] array. This should be 2, 3, 4 in the above example. Now I want to remove() the 2, 3, 4 from the a[] array. This is where I run into problems.
for numbers in range(i, j + 1):
a.remove(a[i-1])
This should remove numbers 2, 3, 4 and leave the a[] array as 1, 5, 6, 7, 8. This works in most cases as specified.
However, in larger arrays such as 500 in length. I see that a.remove() randomly removes numbers not in the range of i, j + 1.
Example
i = 239
j = 422
It removes a[47] and places it in another position as well removes i through j. I have NO IDEA why a[47] is being removed with the code specified above. Is remove() buggy?
What I Need Help On
I'm not trying to have the problem solved for me. I'm trying to understand why remove() is not working correctly. Logic says that it should not be removing anything from i through j, yet it is. Any help is greatly appreciated.
The .remove method on arrays doesn't remove elements by their index, but their value. If you want to delete part of the list, use the del operator (e.g. del a[5] to delete the sixth element, and del a[1:4] to delete the second, third, and fourth elements).
(As for solving this problem efficiently: if you look at the operations in reverse order, I think you don't have to actually manipulate an array.)
Related
A friend of mine asked this question long time ago. He asked me to this without iterating the array. I had in my mind to find the sum on N number as then subtract from it the sum of array numbers. and another the XOR calculation.
But these solutions still needs to iterate the array.
I wounder if there exists such solution or algorithm to do this without iterating the array.
Also if you are going to flag this question duplicate please refer me the link.
What's the missing number in this array ?
1, 2, 3, 4, 5, 6, *
(The * stands for a number you are not allowed to read, otherwise that would be iterating.)
If there is a missing number in an array you have to inspect it, meaning iterate of the array. No way to do th is without iterating.
In general case, you can't do this. Imagine, that you're given Yves Daoust's sample:
[1, 2, 3, 4, 5, 6, ?]
and you're allowed to read any items of the array, but the last one. What is it? Do I hear seven? No, that's a typical wrong solution:
item = i + (i-1)*(i-2)*(i-3)*(i-4)*(i-5)*(i-6)*F(i)
where F(i) is an arbitrary function (well, not arbitrary, there're some loose restrictions, however - F(i) can't be, say 1/(i-3)). Let
F(i) == 0 -> last item == 7
F(i) == 1 -> last item == 727
F(i) == (pi-i)/720 -> last item == pi
...
You have to have more restrictions, e.g. array represents values of a polynom of the least possible power; in that case the solution is 7
I need to design an algorithm that finds the k'th smallest element in unsorted array using function that called "MED3":
This function finds the n/3 (floor) and 2n/3 (ceil) elements of the array if it was sorted (very similar to median, but instead of n/2 it returns those values).
I thought about kind of partition around those 2 values, and than to continue like QuickSelect, but the problem is that "MED3" doesn't return indices of the 2 values, only the values.
for example, if the array is: 1, 2, 10, 1, 7, 6, 3, 4, 4 it returns 2 (n/3 value) and 4 (2n/3 value).
I also thought to run over the array and to take all the values between 2 and 4 (for example, in the given array above) to new array and then use "MED3" again, but can be duplicates (if the array is 2, 2, 2, 2, ..., 2 I would take all the elements each time).
Any ideas? I must use "MED3".
* MED3 is like a black box, it runs in linear time.
Thank you.
I think you're on the right track, but instead of taking 2 to 4, I'd suggest removing the first n/3 values that are <= MED3.floor() and the first n/3 values that are >= MED3.ceil(). That avoids issues with too many duplicates. If two passes/cycle aren't too expensive, you can remove all values < MED3.floor() + up to a total of n/3 values = MED3.floor() (do the same for ceil())
then repeat until you are at the k'th smallest target.
Using the Ruby language, have the function ArrayAdditionI(arr) take the array of numbers stored in arr and return the string true if any combination of numbers in the array can be added up to equal the largest number in the array, otherwise return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the output should return true because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the same elements, and may contain negative numbers.
Could someone please explain to me why this code starts at 'i=2' and not 'i=0'?
def ArrayAdditionI(arr)
i = 2
while i < arr.length
return true if arr.combination(i).map{|comb| comb.inject(:+)}.include?(arr.max)
i += 1
end
false
end
ArrayAdditionI(STDIN.gets)
Correct me if I'm wrong but with i=2, the while loop will iterate [2..4] and then stop. But does this allow for all the potential combinations?...=> code works, so obviously it does but I'm just not seeing it.
i is not the index of the array it the number of elements that is being used to create a combination. So if the max number in the array can be made with the sum of just two elements it stops if not it tries three and so on.
array.combination(i) returns all possible combination of elements in an array with length i.
For example
if
ar=[4, 6, 23, 10]
then
array.combination(2).to_a
returns
[[4,6],[4,23],[4,10],[6,23],[6,10],[23,10]]
So basically you need to find sum in your program and getting sum requires combinations of length two (You need to operands in all possible combination ). Hence you don't start with i=0 or i=1.
You can not give it an empty array,so 0 leads to false. If you have 1 element in an array,it is also meaningless. So I guess 2 is a starting point which makes this test meaningful.
Eg I have array (/1,3,4,5,7,9,11/), how do I remove its 3rd element? I couldn't find an array function which does that, nor I find a loop an elegant solution, since I don't know how to append to an array (this means, add an element next to the previous defined element.)
I want to remove all even elements from an array... I know there is only one.
I can find its index using MINLOC, but I don't know how to remove an element from array.
Let
a = (/1,3,4,5,7,9,11/)
then
pack(a,mod(a,2)/=0)
will return the odd elements of a. This isn't quite the same as removing the 3rd element, but your question suggests that removing the even element(s) is really what you want to do.
If you declare
integer, dimension(:), allocatable :: oddones
then
oddones = pack(a,mod(a,2)/=0)
will leave oddones containing the odd elements of a. You'll need an up-to-date compiler to use this automatic allocation.
Note that in Fortran, as in any sane language, arrays are of fixed size so removing an element isn't really supported. However, if a itself were allocatable then you could use a on the lhs of the expression. Let's leave it to the philosophers whether or not a remains the same under this operation.
I have not used FORTRAN in a while, but look at the functions CSHIFT and EOSHIFT. I think you can't change the size of the array.
If you work with a fixed maximum size, but want dynamic behaviour within, you can just shift the elements:
integer, parameter :: max=10
integer :: a(max)
integer :: length_a, index, i
length_a = 8
a(1:length_a)=[1, 3, 4, 5, 7, 9, 11, 13]
!remove (third) element
index = 3
do i=index,length_a-1
a(i)=a(i+1)
end do
length_a=length_a-1
The advantage is that this does not use an array temporary.
You could just do some slicing.
integer, parameter:: max = 10
integer used ! number of elements used
integer, dimension(1:max):: store
...
used = max
...
! Remove element n
if (n > 0 .and. n < used) then
store(n:(used - 1)) = store((n+1):used)
end if
if (n > 0) used = used - 1
A working example, thanks to this discussion. Thanks, Dick Hendrickson and Dave Frank.
Quote:
joel GUERRERO wrote
(Thursday, September 02, 2004 6:29 PM)
I have this question, how can I do to add a record or to delete a
record from a list or array n fortran 90??
That is, let suppose that I have the following array:
(1 4 3 9 10 2 15 8)
And I want to add the value 13 in the 4th position in order to obtain:
(1 4 3 13 9 10 2 15 8)
You asked above several weeks back, which led to my asking the newsgroup what syntax F2003 adds to facilitate this operation.
Dick Hendrickson's response indicates below would work using F2003
syntax.
integer,allocatable :: v(:)
v = [1, 4, 3, 9, 10, 2, 15, 8]
v = [v(:3), 13, v(4:)] ! insert 13 into expanded size v at v(4)
dave_frank 9/3/2004 8:07:59 AM
Working answer: as asked, removes 1 matching item only:
program hello
integer a(8)
integer b(7)
a=[1, 3, 4, 5, 7, 9, 11, 13]
index = minloc(a, dim=1, mask=(mod(a, 2) .eq. 0))
b=[a(1:index-1), a(index+1:size(a))]
print *, b
end program Hello
You may be able to loop, "while (index)" with additional edits as necessary, to remove all matches, if you like.
I jave a 2D array like this, just like a matrix:
{{1, 2, 4, 5, 3, 6},
{8, 3, 4, 4, 5, 2},
{8, 3, 4, 2, 6, 2},
//code skips... ...
}
(The Array is not sorted)
I want to get all the "4" position, instead of searching the array one by one, and return the position, how can I search it faster / more efficient? thz in advance.
You can't. There is no magic way. Your algorithm will always need to check each cell in your matrix, so it will always be O(n*m) for a matrix of size n * m.
If you can sort your matrix first, then you can get away with O(log(n) * m), as you can use a binary search inside each row.
The only way to do this is less than m * n is to have it presorted in some way. It is not clear from your question if that is possible.
There is no obvious algorithmic optimisation (unless you have some a priori knowledge of the data, e.g. that it's sorted, or you know how many 4s there are). However there are micro-optimisations that you can use, e.g. if your array is 32 bit int and you can use SSE then you can load and compare 4 elements at a time.
You can choose speed or memory consumption. If Memory is not important you could create a List of positions where values are stored. So you have still your m*n array, but additionaly an array of "position-lists". You would have to create "setter"-methods which write down a position in the lists each time a value is added or changed. So the idea is not to improve the search but avoid it.
Example:
You have a 2*2 Array.
{{0,0}
{0,0}}
And you want to add a 4 in the . So you have to call your method write which is called with the parameters X, Y, and Value. This method would change your array to
{{4,0},
{0,0}}
but also create a list
List4=>{(0,0)}
with the position of fours. If you add a second 4 it would look like
{{4,0}
{4,0}}
List4=>{(0,0),(1,0)}
So if you want to find all 4 in your matrix you just have to go to all positions in your List4. Of course you would have to create a list for each value in your array. So you could have a maximum of m*n lists with positions if the matrix contains each value only once.