Ruby Complexity / Time explaination - ruby

I am using HackerRank to improve my ruby skills. I am on the Data Structures - Array Manipulation problem: https://www.hackerrank.com/challenges/crush/problem
My solution is listed first, a supposedly "correct" solution I haven't been able to implement is listed last. My solution works for all test cases except **VERY ** large numbers, it fails due to timeout. I know this is because of a loop, but can someone offer a different solution, and explain why theirs works?
EXPLAINATION OF PROBLEM:
Sample Input
5 3
1 2 100
2 5 100
3 4 100
#n=5 m=3, then following numbers leftIndex=1 rightIndex=3 sum=100 and so on
Sample Output
200
Explanation
After the first update list will be 100 100 0 0 0.
After the second update list will be 100 200 100 100 100.
After the third update list will be 100 200 200 200 100.
The required answer will be 200.
OR
Explain how the why the bottom solution can work without looping?
#Problem Data Set, my solution times out for the listed data set
10000000 100000
[
[1400906,9889280,90378],
[6581237,9872072,87106],
[4386373,9779851,52422],
[198648,4373818,5289]
]
# For this problem, variables will look like:
n = 10000000
m = 100000
q = [[1400906,9889280,90378],[6581237,9872072,87106],[4386373,9779851,52422],[198648,4373818,5289]]
def arrayManipulation(n, q)
arr = Array.new(n,0)
max = 0
q.size.times do |i|
left, right, value = q[i][0],q[i][1],q[i][2]
left -= 1
(left...right).each do |j|
arr[j] += value
max = arr[j] if max < arr[j]
end
end
return max
end
# this times out on HackerRank, please explain better solution,
#or why the bottom solution works!
result = arrayManipulation n, queries
Working SOlution on Discussion board
N, M = gets.chomp.split(' ').map(&:to_i)
# create array of zeros of length N + 1
arr = Array.new(N + 1, 0)
M.times do
# cycle through and get the inputs
start, finish, value = gets.chomp.split(' ').map(&:to_i)
# increment value at start of sequence
arr[start - 1] += value
# decrement value at first position after sequence
arr[finish] -= value
end
tmp = 0
max = 0
arr.each do |value|
# step through summing array
tmp += value
# capture the max value of tmp
max = tmp if max < tmp
end
puts max

Your code is O(m * n) as you have a nested loop to update the array for each entry.
The working code is O(m + n): it never really stores the array, it stores the delta array, the array of changes that makes it possible to reconstruct the array. In your first example, it would store:
0 0 0 0 0 0
+100 0 -100 0 0 0
+100 +100 -100 0 0 -100
+100 +100 0 0 -100 -100
This requires only a loop of m iterations, no nested loops; then you need another loop of n iterations to run through the array, keep running total, and identify the maximum of it.

Related

Counting Sort - Why go in reverse order during the insertion?

I was looking at the code for Counting Sort on GeeksForGeeks and during the final stage of the algorithm where the elements from the original array are inserted into their final locations in the sorted array (the second-to-last for loop), the input array is traversed in reverse order.
I can't seem to understand why you can't just go from the beginning of the input array to the end, like so :
for i in range(len(arr)):
output_arr[count_arr[arr[i] - min_element] - 1] = arr[i]
count_arr[arr[i] - min_element] -= 1
Is there some subtle reason for going in reverse order that I'm missing? Apologies if this is a very obvious question. I saw Counting Sort implemented in the same style here as well.
Any comments would be helpful, thank you!
Stability. With your way, the order of equal-valued elements gets reversed instead of preserved. Going over the input backwards cancels out the backwards copying (that -= 1 thing).
To process an array in forward order, the count / index array either needs to be one element larger so that the starting index is 0 or two local variables can be used. Example for integer array:
def countSort(arr):
output = [0 for i in range(len(arr))]
count = [0 for i in range(257)] # change
for i in arr:
count[i+1] += 1 # change
for i in range(256):
count[i+1] += count[i] # change
for i in range(len(arr)):
output[count[arr[i]]] = arr[i] # change
count[arr[i]] += 1 # change
return output
arr = [4,3,0,1,3,7,0,2,6,3,5]
ans = countSort(arr)
print(ans)
or using two variables, s to hold the running sum, c to hold the current count:
def countSort(arr):
output = [0 for i in range(len(arr))]
count = [0 for i in range(256)]
for i in arr:
count[i] += 1
s = 0
for i in range(256):
c = count[i]
count[i] = s
s = s + c
for i in range(len(arr)):
output[count[arr[i]]] = arr[i]
count[arr[i]] += 1
return output
arr = [4,3,0,1,3,7,0,2,6,3,5]
ans = countSort(arr)
print(ans)
Here We are Considering Stable Sort --> which is actually considering the Elements position by position.
For eg if we have array like
arr--> 5 ,8 ,3, 1, 1, 2, 6
0 1 2 3 4 5 6 7 8
count-> 0 2 1 1 0 1 1 0 1
Now we take cummulative sum of all frequencies
0 1 2 3 4 5 6 7 8
count-> 0 2 3 4 4 5 6 6 7
After Traversing the Original array , we prefer from last Since
we want to add Elements on their proper position so when we subtract the index , the Element will be added to lateral position.
But if we start traversing from beginning , then there will be no meaning for taking the cummulative sum since we are not adding according to the Elements placed. We are adding hap -hazardly which can be done even if we not take their cummulative sum.

Why in this factorial code (no recursion) the step is -1 instead of +1?

Why in this code for calculating factorial the step is -1, not +1?
How it calculates n = 0, if in the program we have only n < 0 and n > 0?
def factorial(n)
if n < 0
return nil
end
result = 1
while n > 0
result = result * n
n -= 1
end
return result
end
It calculates n=0 because result is set to 1 by default. When n is 0, the loop will not run (because n is not > 0), so the default value of result (which is 1) will be returned.
It subtracts one each time so that it can count downward over all the numbers, so
5! = 5 * 4 * 3 * 2 * 1
If it added one each time it would be
5! = 5 * 6 * 7 * 8...
and so on. So not only would it be wrong but it would also be an infinite loop.
The step is -1 so that you multiply together all the values of n * (n -1) * (n - 2) * ... * 1. Since the multiplication starts at n and goes down, you want the step to be negative. (Alternatively, you could start at 1 and go up to n. In that case you would want the step to be +1. That's just not how this implementation works.)
The program still works for an input of 0 because the default is set to result = 1 before the while loop. The body of the loop is skipped in that case.
1 * 2 * 3 == 3 * 2 * 1, so arithmetically it doesn't matter whether you work your way up or down. However, decrementing the argument n allows it to be used directly in calculating result, as opposed to basing the calculation on an additional separate variable which gets incremented until it hits n.
That said, it's still not a very ruby way of doing things. I'd probably go with something like:
def factorial(n)
n == 0 ? 1 : (1..n).inject(:*)
end
You also asked how your implementation calculates factorial(0) as 1. If n == 0 the while n > 0 loop is never activated, so from your initialization result = 1 you skip down to the return statement.
The reason the step is -1 and not +1 is because the while loop is going while n > 0. So if you were to do n += 1, you would have an infinite loop on your hands.
If you were to set up a for loop with an iterator, you could start the iterator and go until iterator <= n. In that case, you would want to do iterator += 1. But in this situation, we just need to multiply all the values from 1-n together, which is not sensitive to order. Because of that, it is simpler and less code to start at n and decrement it until it reaches zero. That is the reason for n -= 1.

All possible N choose K WITHOUT recusion

I'm trying to create a function that is able to go through a row vector and output the possible combinations of an n choose k without recursion.
For example: 3 choose 2 on [a,b,c] outputs [a,b; a,c; b,c]
I found this: How to loop through all the combinations of e.g. 48 choose 5 which shows how to do it for a fixed n choose k and this: https://codereview.stackexchange.com/questions/7001/generating-all-combinations-of-an-array which shows how to get all possible combinations. Using the latter code, I managed to make a very simple and inefficient function in matlab which returned the result:
function [ combi ] = NCK(x,k)
%x - row vector of inputs
%k - number of elements in the combinations
combi = [];
letLen = 2^length(x);
for i = 0:letLen-1
temp=[0];
a=1;
for j=0:length(x)-1
if (bitand(i,2^j))
temp(k) = x(j+1);
a=a+1;
end
end
if (nnz(temp) == k)
combi=[combi; derp];
end
end
combi = sortrows(combi);
end
This works well for very small vectors, but I need this to be able to work with vectors of at least 50 in length. I've found many examples of how to do this recursively, but is there an efficient way to do this without recursion and still be able to do variable sized vectors and ks?
Here's a simple function that will take a permutation of k ones and n-k zeros and return the next combination of nchoosek. It's completely independent of the values of n and k, taking the values directly from the input array.
function [nextc] = nextComb(oldc)
nextc = [];
o = find(oldc, 1); %// find the first one
z = find(~oldc(o+1:end), 1) + o; %// find the first zero *after* the first one
if length(z) > 0
nextc = oldc;
nextc(1:z-1) = 0;
nextc(z) = 1; %// make the first zero a one
nextc(1:nnz(oldc(1:z-2))) = 1; %// move previous ones to the beginning
else
nextc = zeros(size(oldc));
nextc(1:nnz(oldc)) = 1; %// start over
end
end
(Note that the else clause is only necessary if you want the combinations to wrap around from the last combination to the first.)
If you call this function with, for example:
A = [1 1 1 1 1 0 1 0 0 1 1]
nextCombination = nextComb(A)
the output will be:
A =
1 1 1 1 1 0 1 0 0 1 1
nextCombination =
1 1 1 1 0 1 1 0 0 1 1
You can then use this as a mask into your alphabet (or whatever elements you want combinations of).
C = ['a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k']
C(find(nextCombination))
ans = abcdegjk
The first combination in this ordering is
1 1 1 1 1 1 1 1 0 0 0
and the last is
0 0 0 1 1 1 1 1 1 1 1
To generate the first combination programatically,
n = 11; k = 8;
nextCombination = zeros(1,n);
nextCombination(1:k) = 1;
Now you can iterate through the combinations (or however many you're willing to wait for):
for c = 2:nchoosek(n,k) %// start from 2; we already have 1
nextCombination = nextComb(A);
%// do something with the combination...
end
For your example above:
nextCombination = [1 1 0];
C(find(nextCombination))
for c = 2:nchoosek(3,2)
nextCombination = nextComb(nextCombination);
C(find(nextCombination))
end
ans = ab
ans = ac
ans = bc
Note: I've updated the code; I had forgotten to include the line to move all of the 1's that occur prior to the swapped digits to the beginning of the array. The current code (in addition to being corrected above) is on ideone here. Output for 4 choose 2 is:
allCombs =
1 2
1 3
2 3
1 4
2 4
3 4

script for calculating boundaries between elements of matrix

The following script gives me sum of boundaries of each element of matrix. boundaries are calculated about elements with value 1 that are next to each other. product of this summation named contact perimeter.
but Is there any other way that I can summarize or vectorize my raw and simple script?
I ask this request because my real matrix is very large and use of "for" increase the time of calculation.
Thank you.
a1=[1 1 0 1 0 1;
0 1 1 0 0 1;
1 1 0 1 0 1;
1 1 0 0 1 0;
0 0 0 1 1 1]
m=5
n=6
cmp=zeros(m,n)
cmp1=zeros(m,n)
for i=1:m-1
for j=1:n
if a1(i,j)==a1(i+1,j) && a1(i,j)==1
cmp(i,j)=1
end
end
for i=1:m
for j=1:n-1
if a1(i,j)==a1(i,j+1) && a1(i,j)==1
cmp1(i,j)=1
end
end
end
end
cmtotal=cmp+cmp1
pc=sum(sum(cmtotal))
This should be pretty efficient one -
%// Case1 and case 2 matches
case1_matches = a1(1:end-1,:) == a1(2:end,:) & a1(1:end-1,:)==1
case2_matches = a1(:,1:end-1) == a1(:,2:end) & a1(:,1:end-1)==1
%// Get sum of those matches for the final output, equivalent to your pc
out = sum(case1_matches(:)) + sum(case2_matches(:))
You can replace the sum(..(:)) with nnz(), but I doubt that it will be any better than sum in terms of runtime performance in accordance with benchmarks of sum against nnz.

Is there an infinite loop in my code for solving Collatz sequence?

My code is trying to find the answer to this problem: The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
n → 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million.
And here is my code:
step_count = 1
score = {}
largest_score = 1
(1..1000000).map do |n|
while n >= 1 do
if n%2 == 0 then
n/2
step_count += 1
else
(3*n)+1
step_count += 1
end
end
score = {n => step_count}
end
score.each {|n, step_count| largest_score = step_count if largest_score < step_count}
puts score.key(largest_score)
I ran it for over an hour and still no answer. Is there an infinite loop in my code, or maybe some different problem, and if so what is it?
I am using Ruby 1.8.7
Yes, you've got an infinite loop. It's here:
while n >= 1 do
if n%2 == 0 then
n/2
step_count += 1
else
(3*n)+1
step_count += 1
end
end
The condition in your while loop is testing n, but nothing within the loop is changing its value. What you probably meant to do is this:
while n >= 1 do
if n % 2 == 0
n = n / 2
step_count += 1
else
n = (3 * n) + 1
step_count += 1
end
end
A few sidenotes:
It looks like you mean to be updating the score hash with new key-value pairs, but as written, score = { n => step_count } will replace it entirely on each iteration. To add new pairs to the existing Hash, use score[n] = step_count.
It's much more efficient to look up a value in a Hash by its key than the other way around, so you might want to reverse your Hash storage: score[step_count] = n, finding the largest score with score.each { |step_count, n| #... and reading it out with score[largest_score]. This has the added advantage that you won't have to store all million results; it'll only store the last number you reach that results in a chain of a given length. Of course, it also means that you'll only see one number that results in the largest chain, even if there are multiple numbers that have the same, highest chain length! The problem is worded as though the answer is unique, but if it isn't, you won't find out.
To debug problems like this in the future, it's handy to drop your loop iterations to something tiny (ten, say) and sprinkle some puts statements within your loops to watch what's happening and get a feel for the execution flow.
Try the following solution for your problem:
def solve(n)
max_collatz = 0; max_steps = 0
(1..n).each do |k|
next if k % 2 == 0
next if k % 3 != 1
steps = collatz_sequence_count(k)
if steps > max_steps
max_steps = steps
max_collatz = k
end
end
max_collatz
# answer: 837799 with 525 steps, in nearly 2.2 seconds on my machine
end
def collatz_sequence_count(k)
counter = 1
while true
return counter if k == 1
k = k % 2 == 0 ? k/2 : 3 * k + 1
counter += 1
end
end
# You can then use the above methods to get your answer, like this:
answer = solve 1000000
puts "answer is: #{answer}"
Results (uses a custom home-brewed gem to solve ProjectEuler problems):
nikhgupta at MacBookPro in ~/Code/git/ProjectEuler [ master: ✗ ] 48d
± time euler solve 14 +next: 2 | total: 22 | ▸▸▸▹▹▹▹▹▹▹
0014 | Longest Collatz sequence | It took me: 2.166033 seconds. | Solution: 837799
euler solve 14 3.30s user 0.13s system 99% cpu 3.454 total

Resources