remove_min
operations
were performed. Show the resulting min-heap.
int x = 1; for (int i = 0; i < n; i++) { for (int j = 1; j <= x; j++) { System.out.println("*"); } x = x + x; }
for (int i = 0; i < n; i++) { for (int j = 0; j < i; i *= 2) { System.out.println("*"); } }
for (int i = 1; i * i <= n; i++) { for (int j = 1; j <= n; j += j) { System.out.println("*"); } }
for (int i = 1; i <= n; i *= 2) for (int j = 1; j <= n; j++) System.out.println("*"); } }
for (int i = 1; i <= n; i *= 2) for (int j = 1; j <= i; j++) System.out.println("*"); } }
for (int i = 1; i <= n * n; i++) { for (int j = 1; j <= n; j *= 2) { System.out.println("*"); } }
for (int i = 1; i <= n; i++) { for (int j = 1; j <= i * i; j *= 2) { System.out.println("*"); } }
for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j *= 2) { System.out.println("*"); } }
for (int i = n; n >= 1; n /= 4) { for (int j = 0; j < i; j++) { System.out.println("*"); } }
public static long power(int x, int n) { return n == 0 ? 1 : n % 2 == 0 ? power(x * x, n / 2) : x * power(x * x, n / 2) }
public static long power(int x, int n) { return n == 0 ? 1 : n % 2 == 0 ? power(x * x, n / 2) : x * power(x * x, n / 2) }
Assuming you were kind and helpful, what would you tell this poor student? And, by the way, since kindness does not involve flat-out telling the student the answer, tell me, instead, how do you express $T$ in $\Theta$-notation?
public static long f(List a) { if (a.size() > 1) { f(a.subList(0, a.size()/2)); f(a.subList(3*a.size()/8, 7*a.size()/8)); f(a.subList(a.size()/4, 3*a.size()/4)); f(a.subList(a.size()/8, 5*a.size()/8)); f(a.subList(a.size()/2, a.size())); for (int i = 0; i < a.size(); i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } } } }
public static long f(List a) { if (a.size() > 1) { f(a.subList(0, a.size()/3)); f(a.subList(a.size()/3, 2*a.size()/3)); f(a.subList(2*a.size()/3, a.size())); for (int i = 1; i <= a.size(); i *= 2) for (int j = 1; j <= i; j++) System.out.println("*"); } } } }
Explain the structure of the algorithm and how this “obvious” value was obtained.
public static boolean isProbablePrime(BigInteger n, int certainty) { boolean negativeOneFound = false; BigInteger negativeOne = BigInteger.ZERO.subtract(BigInteger.ONE); BigInteger half = n.subtract(BigInteger.ONE).divide(new BigInteger("2")); for (int i = 0; i < certainty; i++) { BigInteger a; do a = new BigInteger(n.bitCount(), random); while (!(BigInteger.ONE.compareTo(a) <= 0 && a.compareTo(n) < 0)); BigInteger x = a.modPow(half, n); if (x.equals(negativeOne)) { negativeOneFound = true; } else if (x.equals(BigInteger.ONE)) { } else { return false; } } return negativeOneFound; }
[45 21 3 99 14 17 88]
. "Show the progress" means
show the array after each pass.
[245 221 103 99 214 117 288]
.
public static boolean hasCycles(Graph g) { return g.numberOfEdges() >= g.numberOfNodes(); }Why or why not?
SimpleUndirectedGraph
class that returns its
complement.
A — B — C — D — E — F — G — H — I — J
(x1 or x3 or not x2) and (x2 or not x3)is satisfiable under the assignment x1 = True, x2 = False, x3 = False.
(x1 or x3 or x5) and (x1 or not x2 or x4) and (not x3 or x4 or x5) and (x2 or not x3 or x5)
C(n, k) = 1, if k = 0 or k = n = C(n-1, k-1) + C(n-1,k) if 0 < k < n = undefined, otherwiseIt can be shown that C can also be defined non-recursively:
C(n,k) = n! / (k!(n-k)!)
Give the
worst-case complexity of (a) the naïve recursive, (b) the
non-recursive algorithms for computing C($n$, $k$).