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

[ad_1] 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); … Read more

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

[ad_1] 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) … Read more

[Solved] Quickest Algorithm to write for this?

[ad_1] 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 … Read more

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

[ad_1] 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 … Read more

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

[ad_1] 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). … Read more