1
\$\begingroup\$
def add(a,b):
    for i in xrange(a-1,b):
        box[i]+=1
def count(k):
    if box.count(k)!=0:
        box.index(k)
        return len(box)-box.index(k)
    else:
        return 0
from sys import stdin,stdout
box=[0]*int(stdin.readline()) #input the number of boxes of chocolate
for i in xrange(int(stdin.readline())):  #loops for M days
    a,b = stdin.readline().split()  #gets the starting and ending index.
    add(int(a),int(b)) #adding 1 each time to box for I,R
box.sort() 
for i in xrange(int(stdin.readline())): 
    print count(int(stdin.readline()))  #prints the number of boxes of chocolates greater than equal to the input number

For small values the program works but fails for large value.Suggest any faster method/algorithm.

Constraints: 1 <= N <= 100000

1 <= M <= 1000

1 <= l <= r <= N

1 <= Q <= 100000

1 <= K <= N

\$\endgroup\$
  • 1
    \$\begingroup\$ Hello, and welcome to code review. Could I please check what you mean by "fails for large values?" Is it giving the wrong answer, or just taking too long to give any answer at all? \$\endgroup\$ – Josiah Jul 18 '18 at 6:46
  • 1
    \$\begingroup\$ fails for large values means Time Limit is Exceeded \$\endgroup\$ – AYUSH SINGHAL Jul 18 '18 at 7:12
  • 1
    \$\begingroup\$ The comments in the code feels like this is a coding challenge. Do you mind adding the description of the challenge into the question, please? \$\endgroup\$ – Mathias Ettinger Jul 18 '18 at 8:32
  • 1
    \$\begingroup\$ I also removed the python-3.x tag as xrange doesn't exist anymore and print as a keyword is not valid syntax. \$\endgroup\$ – Mathias Ettinger Jul 18 '18 at 8:37
  • \$\begingroup\$ I've removed the Python 2.6 tag. You'd initially tagged this question with all Python tags but the code is written in Python 2.7. If this code HAS to run on Python 2.6, please state so explicitly in the question. The 2.7 tag will eventually be renamed to 2.x. \$\endgroup\$ – Mast Jul 30 '18 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.