Counting number of variables in an array not passing test cases - arrays

So I am doing a program on Hackerrank.
The problem is
You have been asked to help study the population of birds migrating across the continent. Each type of bird you are interested in will be identified by an integer value. Each time a particular kind of bird is spotted, its id number will be added to your array of sightings. You would like to be able to find out which type of bird is most common given a list of sightings. Your task is to print the type number of that bird and if two or more types of birds are equally common, choose the type with the smallest ID number.
For example, assume your bird sightings are of types . There are two each of types and , and one sighting of type . Pick the lower of the two types seen twice: type .
Function Description
Complete the migratoryBirds function in the editor below. It should return the lowest type number of the most frequently sighted bird.
migratoryBirds has the following parameter(s):
arr: an array of integers representing types of birds sighted
Input Format
The first line contains an integer denoting , the number of birds sighted and reported in the array .
The second line describes as space-separated integers representing the type numbers of each bird sighted.
Constraints
It is guaranteed that each type is , , , , or .
Output Format
Print the type number of the most common bird; if two or more types of birds are equally common, choose the type with the smallest ID number.
This is my code. It is passing all but one of the test cases. Can you help?
x = []
for i in range (0,len(arr)):
x.append(arr.count(arr[i]))
for i in range(0,len(x)):
if x[i] == max(x):
out = i
return out

I don't see that your code passes any test cases, even with the indentation fixed. Your input size can be as large as 105, so that pretty much eliminates any O(n2) solutions that require traversing the entire list, which is unnecessary. The culprit here is arr.count(), which traverses the entire list for every element. You probably meant to traverse the list for every bird type, which results in 5 traversals of the input list:
x = []
for i in range(1, 6):
x.append(arr.count(i))
out = 0
best = x[0]
for i in range(len(x)):
if x[i] > best:
best = x[i]
out = i
return out + 1
The important restriction to note in this problem is that there are only 5 bird types (numbered 1-5), so we can initialize a counts array of length 6 with a dummy element at position 0 so indices map to counts neatly. Then, we can iterate through our input bird type list in one pass, updating our counter array. Lastly, take the max index of the counter array. The "smallest id for ties" restriction is automatically handled by the way max() works--it'll take the smallest index automatically. Here's the code:
counts = [-1, 0, 0, 0, 0, 0]
for bird in arr:
counts[bird] += 1
return counts.index(max(counts))
Note that the variable names are descriptive. This helps ensure that the purpose of each variable is clear and reduces bugs and erroneous assumptions about the program state.

Related

Scala Collections - Making a new array from an existing using certain rules

I'm trying to make a function in Scala that when -
println(thisIsAFunction(Array(1,1,4,5,4,4)))
is called, it will give me a new array that is always half the size of the array input (the array input will always be an even number and bigger than 2), and in the new array it will not contain the same number twice. So for example, in the above statement, it would return an array consisting of the numbers 1, 4 and 5. I have been trying to approach this a number of ways but can't get my head round it.
def thisIsAFunction(a: Array[Int]) =
a.distinct.take(a.length/2)
The distinct method removes any duplicates from the Array so that it does not contain the same number twice.
The take methods reads the first n elements of the result, so take(a.length/2) gives an Array that is half the length of the original.

Two arrays one with time in, another with time out of something

Lets say we have 2 arrays, one of them (i.e. A) contains the time an object i will come into a room, and the other (i.e. B) contains the time i will leave. Neither of these are in any way sorted and their contents are Real numbers.
For example, object 3 has: A[3]=0.785 and B[3]=4.829.
How would you in O(nlogn) find the max objects in the room at any given time t?
You can try this:
initialize number of objects as zero
sort both arrays
while there are elements left in either array
determine which array's first value is smaller
if the first value in "enter" is smaller, increment number of objects and pop that value
if the first value in "leave" is smaller, decrement number of objects and pop that value
check whether you found a new maximum number of objects
If you can not "pop" elements from the arrays, you can use two index variables instead; also, you will have to add cases for when one of the arrays is already empty.
Sorting has O(nlogn), and the following loop has O(2*n), thus O(nlogn) in total.
Get all times from both arrays and make pairs {time from A or from B; f = +1 for A/ -1 for B}
Sort array of all pairs by time key (in case of tie +1 goes before -1)
Make count = 0
Traverse array of pairs, adding f value to count.
Max value of count is " the max objects in the room"
Example:
A = [2, 5], B = [7, 9]
pairs = (2,1),(5,1),(7,-1),(9,-1)
count = 1, 2, 1, 0
maxcount=2 at interval 5..7

Given an array of sorted integers and find the closest value to the given number in c. Array may contain duplicate values

For example we have 5 values(a,b,c,e,d) in an array which may contain duplicate values.The values of a,b,c,d,e are calculated by another function and got assigned with some values.
let the user input value be x.
we need to find the closest value to x in the array and the output must be in a,b,c,e,d. if the closest number is one of the duplicates the alphabetical order must be considered.
for example:
Array: a,b,c,e,d
a=6,b=5,c=3,d=9,e=9 are the values assigned to them by a function.
for x : 5,
output : b
for x :11,
output : d
for x : 4,
output :c
Try and implement the following algorithm:
get the element at the middle of the array ;
if this element has the value x, the array contains it, you know what to print ;
if the element is larger, look in the first half of the array ;
otherwise look in the second half of the array ;
Once the array portion you search into has a size of 0, you have found the place where x would be inserted to preserve the ordering. The closest value is either the one of the left or the one on the right, if any. Compute the differences to determine what to print.

Maximize sum of weights with constraints given on left and right indices in array

I recently came through an interesting coding problem, which is as follows:
There are n boxes, let us assume this is an array of n boxes.
For each index i of this array, three values are given -
1.) Weight(i)
2.) Left(i)
3.) Right(i)
left(i) means - if weight[i] is chosen, we are not allowed to choose left[i] elements from the left of this ith element.
Similarly, right[i] means if arr[i] is chosen, we are not allowed to choose right[i] elements from the right of it.
Example :
Weight[2] = 5
Left[2] = 1
Right[2] = 3
Then, if I pick element at position 2, I get weight of 5 units. But, I cannot pick elements at position {1} (due to left constraint). And cannot pick elements at position {3,4,5} (due to right constraint).
Objective - We need to calculate the maximum sum of the weights we can pick.
Sample Test Case :-
**Input: **
5
2 0 3
4 0 0
3 2 0
7 2 1
9 2 0
**Output: **
13
Note - First column is weights, Second column is left constraints, Third column is right constraints
I used Dynamic Programming approach(similar to Longest Increasing Subsequence) to reach a O(n^2) solution. But, not able to think of a O(n*logn) solution. (n can be up to 10^5.)
I also tried to use priority queue, in which elements with lower value of (right[i] + i) are given higher priority(assigned higher priority to element with lower value of "i", in case primary key value is equal). But, it is also giving timeout error.
Any other approach for this? or any optimization in priority queue method? I can post both of my codes if needed.
Thanks.
One approach is to use a binary indexed tree to create a data structure that makes it easy to do two operations in O(logn) time each:
Insert number into an array
Find maximum in a given range
We will use this data structure to hold the maximum weight that can be achieved by selecting box i along with an optimal selection of boxes to the left.
The key is that we will only insert values into this data structure when we reach a point where the right constraint has been met.
To find the best value for box i, we need to find the maximum value in the data structure for all points up to location i-left[i], which can be done in O(logn).
The final algorithm is to loop over i=0..n-1 and for each i:
Compute result for box i by finding maximum in range 0..(i-left[i])
Schedule the result to be added when we reach location i+right[i]
Add any previously scheduled results into our data structure
The final result is the maximum value in the whole data structure.
Overall, the complexity is o(nlogn) because each value of i results in one lookup and one update operation.

Splitting an array into n parts and then joining them again forming a histogram

I am new to Matlab.
Lets say I have an array a = [1:1:1000]
I have to divide this into 50 parts 1-20; 21-40 .... 981-1000.
I am trying to do it this way.
E=1000X
a=[1:E]
n=50
d=E/n
b=[]
for i=0:n
b(i)=a[i:d]
end
But I am unable to get the result.
And the second part I am working on is, depending on another result, say if my answer is 3, the first split array should have a counter and that should be +1, if the answer is 45 the 3rd split array's counter should be +1 and so on and in the end I have to make a histogram of all the counters.
You can do all of this with one function: histc. In your situation:
X = (1:1:1000)';
Edges = (1:20:1000)';
Count = histc(X, Edges);
Essentially, Count contains the number of elements in X that fall into the categories defined in Edges, where Edges is a monotonically increasing vector whose elements define the boundaries of sequential categories. A more common example might be to construct X using a probability density, say, the uniform distribution, eg:
X = 1000 * rand(1000, 1);
Play around with specifications for X and Edges and you should get the idea. If you want the actual histogram plot, look into the hist function.
As for the second part of your question, I'm not really sure what you're asking.

Resources