Code Monkey home page Code Monkey logo

competitive-programming-solutions's People

Contributors

ramo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

competitive-programming-solutions's Issues

Bug in Minimize cost hackerearth problem

problem link: https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/minimise-cost-89b54cb9/submissions/

`"""
https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/minimise-cost-89b54cb9/
"""

def main():
# Get the inputs
n, k = [int(x) for x in input().split()]
array = [int(x) for x in input().split()]

# Initialize index 'j' which will be used to point to negative numbers
j = 0
# loop through the array
for i in range(0, n):
    
    # index 'i' will be used to point to positive numbers.
    # So, if the value 'i' pointing is negative one, let's skip it.
    if array[i] <= 0:
        continue

    # increment j if needed to make the distance between
    # i and j as k
    while i - j > k:
        j += 1
    
    # This inner while loop is to redeem the negative numbers with positive ones.
    # We can loop till number pointing by 'i' become 0 (or)
    # 'j' index crossed the distance k from 'i' index (or)
    # 'j' index crossed the array itself (n-1)
    while (array[i] > 0) and (min(n-1, j) <= i+k):
        
        # 'j' index should point to negative number
        # So, if it's not pointing negative, let's skip it
        # and increment 'j' index
        if array[j] >= 0:
            j += 1
            continue
        
        # We can redeem the 'i'th positive number with the 
        # 'j'th negative number
        v = min(array[i], abs(array[j]))
        array[i] -= v
        array[j] += v
        
        # If the negative number pointed by 'j' is redeemed 
        # and become zero or positive, let's move the 'j' index
        if array[j] >= 0:
            j += 1
        # Repeat
    # Repeat
    
# Print the sum 
# As we have neutralized negative ones by positive ones within
# distance 'k', we can take the absolute sum of the array
print(sum(abs(x) for x in array))

if name == 'main':
main()`

For the input : n = 5, k = 4, arr = -1 -2 98 -2 -1
It will give out of bound error because of the condition min(n-1,j) <= i+k
correct condition will be j <= min(n-1, i+k)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.