 ```! Find Kth largest of a set. Given a set of values, X( I), Find THETA = Kth largest value in a set of values X( I). Also, compute sum of K largest. The X( I) may be variables, they need not be constants. Only the upper and lower bounds, XU and XL, must be known constants; ! Keywords: Median, P median, Sorting; SETS: SERIES: X, XS, Z; ENDSETSDATA: N = 3000; ! Length of series; K = 20; ! Desired K median; SERIES = 1..N; ! Just for illustration purposes, Generate a quasi-random series with arbitrary starting seed; X = @QRAND(9387145); XL = 0; ! Lower bound for X( i); XU = 1; ! Upper bound for X( i); ENDDATA; @FOR( SERIES( I): @BIN( Z( I)); ! Z(I) = 1 if X(I) is one of K largest; ! If Z( I) = 1 then X( I) >= THETA; X( I) >= THETA - ( XU - XL)*(1- Z( I)); ! If Z( I) = 0: X( I) <= THETA; X( I) <= THETA + ( XU - XL)* Z( I); ! If want to know the sum of the X(I) >= THETA; @FREE( XS( I)); ! In case L or U < 0; ! If Z( i) = 1 then XS( I) = X( I); XS( I) >= X( I) - XU*(1 - Z( I)); XS( I) <= X( i) - XL*(1 - Z( I)); ! If Z( I) = 0 then XS( I) = 0; XS( I) <= XU* Z( I); XS( I) >= XL* Z( I); ); ! How many we can select; @SUM( SERIES( I): Z( I)) = K; ! Sum of K largest; SUMTOPK = @SUM( SERIES( I): XS( I)); ```