ramo / competitive-programming-solutions Goto Github PK
View Code? Open in Web Editor NEWSolutios for competitive problems in different sites
License: MIT License
Solutios for competitive problems in different sites
License: MIT License
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)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.