[Solved] How can we find number of distinct elements in a given submatrix? [closed]

It depends how many queries you can expect, and the number of identical queries you can expect. One approach is to “memoize” queries, simply to store each query and result, and look that up before doing more serious work. A more problem-specific approach – probably what your teacher is after – is to compute distinct … Read more

[Solved] Get closest (but higher) number in an array

Here’s a method using Array.forEach(): const number = 165; const candidates = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362]; //looking for the lowest valid candidate, so start at infinity and work down let bestCandidate = Infinity; //only consider numbers higher than the target const higherCandidates = candidates.filter(candidate => candidate > number); //loop … Read more

[Solved] Java – how do I iterate through this map so method my method returns a string [closed]

A loop won’t return the String you want this way, you need to create the String and, at every loop, concatenate a new one to it: public static String m(Map<String, String> mp) { Iterator<?> it = mp.entrySet().iterator(); String str = “{” + ” \”SomeAttribute\”: ” + “{“; while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); … Read more

[Solved] Quickest Algorithm to write for this?

I would start small, and build up. The smallest (n=1) is simply: * that clearly doesn’t work since there are 0 neighbors (and even number). So no solution exists for n=1. Next, try n=2. Only one choice: ** This works. Now what about n=3? Doesn’t work, no solution for n=3. Now, how can you add … Read more

[Solved] Given N number display M*M format [closed]

You still haven’t asked any question, but I assume that your program gives wrong answer (as it really do) and you don’t know why. For example: for n=16 it prints: 1234 1234 1234 1234 The following code fragment is responsible: array[i][j]=j+1; System.out.print(array[i][j]); Firstly, you don’t print spaces, so numbers aren’t separated. Secondly, you have to … Read more

[Solved] Do we really have case in this algorithm [closed]

The comment you wrote in @OBu’s answer is about only a quarter right: 1*n + 2*(n-1) + 3*(n-2) + … +n*1 That equals to: Sum(i=1..n, i*(n-i+1)) = n*Sum(i) – Sum(i*i) + n = n*[n(n+1)/2] – [n(n+1)(2n+1)/6] + n If you want, feel free to compute the exact formula, but the overall complexity is O(n^3). As … Read more