Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A stack of fake coins There are n stacks of n identical-looking coins. All of th

ID: 3804552 • Letter: A

Question

A stack of fake coins There are n stacks of n identical-looking coins. All of
the coins in one of these stacks are counterfeit, while all the coins in the other
stacks are genuine. Every genuine coin weighs 10 grams; every fake weighs
11 grams. You have an analytical scale that can determine the exact weight of
any number of coins.
a. Devise a brute-force algorithm to identify the stack with the fake coins and
determine its worst-case efficiency class.
b. What is the minimum number of weighings needed to identify the stack
with the fake coins?

Explanation / Answer

a) If the amount of coin stacks from one to ten.
Take one coin from the primary stack, two coins from the second, and so on, till all ten coins square measure taken from the last stack.
Weigh of these coins along.
The distinction between this weight and 550, the load of (1 + two + ... + 10) = fifty five real coins, indicates the numberof the pretend coins weighted, that is capable the amount of the stack with the pretend coins.
For example, if the chosen coins weigh 553 grams, three coins square measure pretend and therefore it's the third stack that contains the pretend coins.

b) here the code for the requiremt was given please follow the code you will get clear picture of the above metioned method

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

/**
* @author Victor
*/
public category BruteForce amendment this to false to output in pretty-print mode.
non-public static final Boolean COMPACT = true;

non-public final int coins;
non-public final int maxDepth;
non-public final int possibilities;

public final category Node {
non-public final boolean[] combs = new boolean[possibilities];
non-public final boolean[] shouldWeight = new boolean[possibilities];
non-public Node[] kids = null;
non-public int counts = 0;
non-public final int level;
non-public int lastCount = -1;
non-public final Node parent;
non-public Set<Integer> coinRenames;
non-public int whichCoinsToWeight = -1;
non-public int tag; // For writing it in some human legible format. Not used for making and process the tree.

public Node()

non-public Node(int level, Node parent)
}

public void count(int c) you wish a solution in a few seconds instead of
// waiting for some hundreds of years, keep this check here.
if (coinRenames.add(renamed))
}

// Convert an int to an array of boolean.
private boolean[] asArray(int coinSet)
return array;
}

private void summarizeCoinsUsedForWeighting(boolean[][] weightingCoinsPerLevel)

private int renameCoins(int thisLevelCoins) = 1 << (level - i);
  
}

// Rename the coins to ensure that the most firstly-used coins are the first ones.
// Coin-renaming serves to avoid recalculating combinations that are simply permutations to some
// previously calculated position. Further, this coin renaming provides a single canonical representation
// for each group of equivalent permutations. To ensure that, we simply sort the array, so the most used
// coins with higher bits sets will be the LAST ones, I.E, the resulting array would still be in the reverse order.
Arrays.sort(usingLevelsPerCoin);

// We could untranspose the sorted array, so we have again coins per level, but with the coins are renamed.
// However, now we are interested only in the last level, so we just get the last bit from each row on the array.
// Further, we have a tendency to reverse the order of the bits that we have a tendency to get as a result of the array is sorted in reverse order. this manner it
// are going to be within the right order once more.
int sortedCoinsOnLastLevel = 0;
for (int j = 0; j < coins; j++) = (usingLevelsPerCoin[j] & 1) << (coins - j - 1);
  

come sortedCoinsOnLastLevel;
}

public Boolean separate() {
if (counts == 1) come true; // only one chance, it's within the lastCount variable.

// If is recursing too deply and there's still a minimum of two coins left during this chance,
// therefore there was a foul alternative somewhere up within the call tree.
if (level >= maxDepth) come false;

// The coins choosen to weight square measure the one bits. begin count with one as a result of there's no sense to weight zero coins.
// this suggests that it'll opt for each attainable combination of coins to weight, till it founds an honest one or resigns.
// reserve it in associate instance field, therefore it are often remembered once the answer is found.
a: for (whichCoinsToWeight = 1; whichCoinsToWeight < possibilities; whichCoinsToWeight++) this can be simply a permutation of some antecedently tested combination, simply skip it.
if (!shouldWeight[whichCoinsToWeight]) continue;

kids = null;
int childCount = 0;

// tell each attainable valuation for the coins.
b: for (int selectedCoinsCombination = 0; selectedCoinsCombination < possibilities; selectedCoinsCombination++) ne'er happens during this place, skip it.
if (!combs[selectedCoinsCombination]) continue;

// Weight the coins. This works by setting zero to coins that
// don't seem to be being weighted, departure zero for lightweight coins and one for significant ones.
int bits = number.bitCount(whichCoinsToWeight & selectedCoinsCombination);

// currently begin to populate the youngsters. If it doesn't have any, produce the array.
if (children == null) kids = new Node[coins + 1];

// opt for the suitable kids node within the call tree.
Node kid = children[bits];

// If the chosen node still don't exist, create it.
if (child == null) {
kid = new Node(level + one, this);
children[bits] = child;
childCount++;
}

// Add the chosen coins combination as attainable to the kid node.
child.count(selectedCoinsCombination);
}

// If we've only one kid, then the chosen coins to weight failed to gave any info,
// therefore discard this alternative and proceed to succeeding one.
if (childCount <= 1) continue;

// Now, perform the separation recursively in every kids.
// If a number of them fails, then the chosen coins to weight combination is useless and may
// be discarded, even though some of the nodes were tested.
for (Node n : kids.clone()) {
if (n == null) continue;
Boolean r = n.separate();
if (!r) continue a;
}

// Yeah, found some helpful combination!
come true;
}

// we have a tendency to tried each combination of coins to weight and none of them was helpful.
come false;
}

// Output a JSON!
public void reportResult(StringBuilder sb) {
Boolean 1st = true;
sb.append("{");
if (counts == 1) {
sb.append(""HEAVY": [");
for (int i = 0; i < coins; i++) {
if (((lastCount >> i) & 1) != 0) {
if (first) {
1st = false;
} else

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote