Algorithm Design: Efficient O(n) algorithm to get the ith to jth largest elements in an array

I am trying to design an efficient algorithm that retrieves the ith to jth largest elements in an array. For example, if the following array is the input:

[10, 14, 18, 3, 21, 25, 27] i = 2 j = 4 

The following array is returned:

[25, 21, 18] 

18 is the 4th largest element in the array and 25 is the 2nd smallest element in an array. I’ve done a problem where you retrieve a list of the K largest elements in an array: in such a problem, the solution is pretty trivial (using a fixed-size minimum heap of size K to keep track of the K largest elements). In this case however, a heap seems out of reach to use because the minimum heap can only remove the smallest element from the heap (removing the kth smallest element within the heap seems really inefficient).

I could do QuickSelect (j – i) times to get all the elements, but that means doing a linear O(n) walkthrough (j – i) times, which yields a total time complexity of O((j-i) * n), but I was thinking about if it’s possible to quicken the algorithm’s time complexity to just O(n). Is there an O(n) algorithm to solve this problem?

Finding Largest word in a given text file

So I am working on this problem involving finding the largest word in a given text file using python. I already Wrote a Code But want to find if there is a more efficient way to go by this problem. Help is appreciated!!

I’ve already achieved the goal i just want to find a more efficient way to do this.

Here’s my code:

def largestWord(filename): length = {} maxValue = 0 with open(filename, ‘r’) as infile: for line in infile.readlines(): listOfWords = line.split() for word in listOfWords: length[word] = len(word) //makes a dictionary where the key is the word and the value is the length of the word. for value in length.values(): //Iterating through the values of the if value > maxValue: maxValue = value dictionary to find the highest value and then comparing with values form the keys to return the word

    for k in length.keys():         if length[k] == maxValue:             print(k) 


Want a shorter piece of code.

Calculate the largest palindromic number from the product of two 6-digit numbers (100000 to 999999)

Are there any efficient ways to solve this problem, for example using bitwise operator?

    public static boolean isPal(long num)      {         String numStr = Long.toString(num);         String rNumStr = "";          boolean result = false;          for (int i = numStr.length() - 1; i >= 0 ; --i)             rNumStr += numStr.charAt(i);          //System.out.println(numStr + "," + rNumStr);         try          {             if (Long.parseLong(numStr) == Long.parseLong(rNumStr))                 result = true;             else                   result = false;         }catch (NumberFormatException e) {             //System.err.println("Unable to format. " + e);         }         return result;      }      public static void calcPal(int rangeMin, int rangeMax)     {         long maxp = 0, maxq = 0, maxP = 0;         for (int p = rangeMax; p > rangeMin; p--)             for (int q = rangeMax; q > rangeMin; q--)             {                 long P = p * q;                 if (isPal(P))                     if (P > maxP)                     {                         maxp = p; maxq = q; maxP = P;                     }             }         System.out.println(maxp + "*" + maxq + "=" + maxP);     }      public static void main(String[] args)      {         calcPal(10, 99);         calcPal(100, 999);         calcPal(9000, 9999);         calcPal(10000, 99999);         calcPal(990000, 999999);     } 

The largest palindrome which can be made from the product of two 2-digit (10 to 99) numbers is 9009 (91 × 99). Write a function to calculate the largest palindromic number from the product of two 6-digit numbers (100000 to 999999).

Algorithm for answering queries of the type “largest interval contained in the given interval”

I have been wondering over the following problem:

Given a set $ S$ of intervals on the number line. We can do two operations on them:

  1. Add a new interval $ [l,r]$ to $ S$
  2. Given an interval $ [ql, qr]$ , which is possibly not in $ S$ , find the longest interval from $ S$ which is contained entirely in $ [ql, qr]$

If I didn’t have operation $ 1$ I have found a solution using a persistent segment tree.

For the full version, there is the trivial solution which runs in $ O(QN)$ . I also found that using interval tree, I can directly find which intervals intersect the query interval and then I can check them manually. However, is there an even better solution? Maybe $ O(NlogN)$ or $ O(Nlog^2N)$

Find the largest product of an array

This task is taken from

Given an array of integers, return the highest product possible by multiplying 3 numbers from the array


array of integers e.g {1, 2, 3} 


[0, -1, 3, 100, 70, 50]  => 70*50*100 = 350000 

NOTE: Solution will fit in a 32-bit signed integer

My approach: First sort the array, then return the maximum the following numbers: The product of the first two smallest numbers and the last number (largest) or the product of the three largest numbers.

My solution as a runtime of $ O(nlogn)$ due to the sort and a space complexity of $ O(1)$ . I wonder whether there is a faster solution than $ O(nlogn)$ .

function highesProd(a) {   a.sort((a,b) => a - b);   return Math.max(a[0] * a[1] * a[a.length - 1], a[a.length - 3] * a[a.length - 2] * a[a.length - 1]); } 

Largest eigenvalue scaling in a certain Kac-Murdoch-Szegö matrix

I’m looking at $ N\times N$ matrices $ M_N$ with elements $ $ M_N=\left( \rho^{|i-j|} \right)_{i,j=1}^N,$ $ where $ \rho$ is a complex number of unit modulus. These matrices with $ \rho\in\mathbb R$ and $ |\rho|<1$ have been studied in detail before, with a nice exposition to be found here, which includes some more references.

In the cited article, there is an implicit form of the eigenvalues, given through $ $ \lambda_j = \frac{1-\rho^2}{1-2\rho\cos\theta_j+\rho^2}, $ $ where $ \theta_j$ are roots of the following function $ $ G(\theta) = \sin[(n+1)\theta]-2\rho\sin[n\theta]+\rho^2\sin[(n-1)\theta]. $ $ (Even though only real $ \rho$ were considered in the article, this works for complex $ \rho$ also.)

Intriguingly, if $ \rho=e^{i\phi}$ , the largest eigenvalue of $ M_N$ seems to be essentially independent of $ \phi$ (as long as $ \phi$ is sufficiently different from 0 (i.e., $ \mathcal O(1/N)$ ). Since the inverse of $ M_N$ is almost tridiagonal (see the article for its form), it can be efficiently diagonalized numerically. I’ve checked up to $ N=10^6$ and the largest eigenvalue (mostly real) seems to almost follow a power law (roughly $ N^{0.85}$ ), but not quite. In fact, it looks slightly curved, so perhaps it is approaching $ N$ .

Another important thing I’ve noticed is that the $ \theta$ corresponding to the largest eigenvalue is very close to $ \phi$ , and seems to approach $ \phi$ as $ N\to\infty$ . Indeed, it is the same scaling as the actual eigenvalue $ \lambda$ , which follows from the fact that $ \theta=\phi$ makes the denominator of the formula for $ \lambda$ vanish. Expanding $ \theta=\phi+\delta\phi$ it becomes clear that $ $ \ln\lambda\to -\ln(\theta-\phi) + \text{const.} $ $

So I’ve been trying for a long time to extract how $ \theta$ approaches $ \phi$ from $ G(\theta)$ , but failed. I’d appreciate any pointers to a solution.

Largest ideal in bounded linear maps on Schatten-$p$ class

Let $ 1\leq p<\infty.$ Denote $ S_p(\ell_2)$ be the set of all compact operator $ x$ on $ \ell_2$ such that $ Tr(|x|^p)<\infty.$ Define $ \|x\|_{S_p(\ell_2)}:=Tr(|x|^p)^{\frac{1}{p}}.$ This makes $ S_p(\ell_2)$ a Banach space. What is the largest closed two-sided ideal in the Banach algebra of set of all bounded linear maps on $ S_p(\ell_2)$ ?

What is the largest a player character can get using only official material and UA? [duplicate]

This question already has an answer here:

  • How large is it possible for a PC to get? 2 answers

I was presented a concept for a one shot session from one of my players. He comes up with some strange ideas, and I’d like to see if this one would be viable:

The scenario goes that the Tarrasque has a prophesied date of awakening, so an elite order was created for the sole purpose of combating the creature to put it back in the ground. Using a combination of training courses over the course of many years (multiclassing into very specific classes), each of the group can become as large, if not larger than the Tarrasque and, for the lack of a better term, wrestle it to death.

My question is: Is there actually a combination of class features and spells that can push a character to get larger than a Tarrasque?