Money Collection

Mr.savani is collecting a money from n students for a trip,  like collect $1 from 1st student, $3 from 3rd student and so on.. but it should not include k (unlucky number) into sum. if it includes k into sum, then ignore that student

 

Example:

n = number of student, k = unlucky number
i/p: 2,2, o/p: 3,
i/p: 3,3, o/p: 5, (could be 4 as well but find max sum)

Solution: Using Discriminant maths  D = b2 – 4ac

def maxMoney(n, k)
    sum = (n * (n + 1)) / 2
    sum -= 1 if Math.sqrt(1+8*k) % 1 == 0
    return sum
end

puts maxMoney(2,2)   # o/p 3
puts maxMoney(2,1)   # o/p 2
puts maxMoney(3,3)   # o/p 5
puts maxMoney(2000000000,4000000000000000)

2 thoughts on “Money Collection”

Leave a Reply

Your email address will not be published. Required fields are marked *