Memory error Using intertool in python -


i write programme input 10 example , find odd number first , number , put them in list [1,3,5,7,9,2,4,6,8,10] , input number choose number in list example 3 print 5 , on write code

from itertools import count n,y=map(int, raw_input().split()) # input 2 numbers  listaa=[0,] # list save  x in count(1,1):    if x%2!=0:       listaa.append(x)    if x==n:        break h in count(1,1):    if h%2==0:       listaa.append(h)    if h==n:       break res=listaa[y] print res # print number in array or list  

but when submit code on online judge try number 1000000000000 500000000001 runtime_error try on eclipse memory error note first try xrange error when search found generator try , use count instead of xrange note run time limit each test case 1 second

you cannot solve problem using approach because list big time limit test case suggested 1000000000000 500000000001 contains approximately 4*10^12 numbers needs more terabyte of ram. don't use list use math formula instead.

sample code

n,y=map(int, raw_input().split()) first_odd = 1 last_odd = n if n%2 == 1 else n-1 n_odd = (last_odd-first_odd)/2 + 1 if y <= n_odd:     print first_odd + 2*(y-1) else:     y -= n_odd     print 2+2*(y-1) 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -