Pragalathan M
BAN USER
This can be solved in O(N), where N is the length of the word.
Lets assume,
- that the length of the word, N is given
- the machine returns 1 for each letter in test string matching the letter in dictionary word in the same index
private void findWord(int n) {
Character[] result = new Character[n];
char[] sample = new char[n];
int prevCount = 0;
for (int c = 'a'; c <= 'z'; c++) {
fillAll(sample, result, (char)c);
int count1 = checkWord(sample);
if (prevCount == count1) {
// the alphabet c never appeared in result; so skip and go to the next alphabet
continue;
}
c++; // go to next char
for (int i = 0; i < n; i++) {
char old = sample[i];
if (result[i] != null) {
// this index already has a valid char
continue;
} else {
sample[i] = (char) c;
}
int count2 = checkWord(sample);
if (count2 > count1) {
// c belongs to index i
count1++;
result[i] = (char)c;
} else if(count2<count1 ) {
// old char belongs to this index;
result[i] = old;
sample[i] = old;
}
}
prevCount = count1;
}
}
private void fillAll(char[] sample, Character[] result, char c) {
// file the index i of sample with char c where result[c] is null;
}
private int checkWord(char[] sample) {
// return the count of chars in sample, matching the corresponding index in the dictionary word
}
Here for each iteration of outer loop we complete two alphabets, hence it runs 13 time max.
The inner loop runs N times in worst case.
Hence the word can be guess in 13*N trials
There are two clues in the question.
1. its a 10 digit number
2. they are numbers
With these we can design a list of 10 bitsets where
- 'i'th bit of 0th bitset will be 1 if the 'i'th row contains zero
- 'i'th bit of 1st bitset will be 1 if the 'i'th row contains one
and so on.
when you get a number to search get the corresponding bitset for each digit and do an AND of collected sets. Now wherever you have one, that rows are the results.
Space: 10*n bits where n is the number of rows
Time for building: O(1) for finding the right bitset for each digit and O(1) for marking the row in the set. hence 10*n (~n)
Time for searching: O(1) for finding the right bitset for each digit and iterate over the bitset to find the final rows (i.e O(n))
A[i] newsets sets
-----------------------------------------------------------------------------
1 {1} {1}
2 {2}, {1,2} {2}, {1,2}
3 {3}, {2,3}, {1,2,3} {3}, {2,3}, {1,2,3}
4 {4}, {3,4}, {2,3,4}, {1,2,3,4} {4}, {3,4}, {2,3,4}, {1,2,3,4}
5 {5}, {4,5}, {3,4,5}, {2,3,4,5}, {1,2,3,4,5} {5}, {4,5}, {3,4,5}, {2,3,4,5}, {1,2,3,4,5}
Time: O(n(n+1)/2)=n*n
Space: n(n+1)/2 // to store the limited number of subsets
n=length(A)
sets=[ ]
for (i=0 to n-1)
newsets = [ ]
newsets.add( setof(A[i])
if(M == 1 && setof(A[i]) == SUM) print and break
foreach set in sets
s1 = sets[j].add(A[i])
if(s1.size == M && sum(s1)==SUM) print and break
newsets.add(s1); // we can also skip adding s1 if s1.size>M
sets.clear() // we dont need the old sets as we cant add next item to them
sets.addAll(newsets)
Lets say A= {1,2,3,4,5}
M=3
SUM=12
A[i] newsets sets
-----------------------------------------------------------------------------
1 {1} {1}
2 {2}, {1,2} {2}, {1,2}
3 {3}, {2,3}, {1,2,3} {3}, {2,3}, {1,2,3}
4 {4}, {3,4}, {2,3,4}, {1,2,3,4} {4}, {3,4}, {2,3,4}, {1,2,3,4}
5 {5}, {4,5}, {3,4,5}, {2,3,4,5}, {1,2,3,4,5} {5}, {4,5}, {3,4,5}, {2,3,4,5}, {1,2,3,4,5}
in last iteration we have {3,4,5} whose sum is 12 and size is 3.
Now we went through A only once, but for each elements we iterated over sets which is i times hence n(n+1)/2.
In the last iteration we stored n(n+1)/2 items which is the max
<pre lang="" line="1" title="CodeMonkey97168" class="run-this">import java.util.ArrayList;
import java.util.List;
/**
*
* @author Pragalathan
*/
class CoinDenomination {
static class Coin {
int value;
int count;
public Coin(int value, int count) {
this.value = value;
this.count = count;
}
@Override
public String toString() {
return "(" + value + "x" + count + ")";
}
}
Coin[] coins;
public CoinDenomination(Coin[] coins) {
this.coins = coins;
}
List<Coin> getChange(int amount) {
List<Coin> change = new ArrayList<Coin>();
getChange(amount, 0, change);
return change;
}
private boolean getChange(int balance, int coinIndex, List<Coin> change) {
if (coinIndex >= coins.length) {
return false;
}
Coin coin = coins[coinIndex];
int count = Math.min(coin.count, balance / coin.value);
for (int i = count; i >= 0; i--) {
int newBalance = balance - i * coin.value;
if (newBalance == 0) {
change.add(new Coin(coin.value, i));
return true;
}
if (getChange(newBalance, coinIndex + 1, change)) {
if (i > 0) {
change.add(new Coin(coin.value, i));
}
return true;
}
}
return false;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int[] values = {1, 5, 10, 25, 50, 100};
int[] counts = {3, 4, 2, 4, 1, 2};
Coin[] coins = new Coin[values.length];
for (int i = 0; i < values.length; i++) {
coins[i] = new Coin(values[i], counts[i]);
}
CoinDenomination cd = new CoinDenomination(coins);
int amount = 33;
System.out.println(amount + ": " + cd.getChange(amount));
values = new int[]{1, 10, 25, 50, 100};
counts = new int[]{3, 3, 4, 1, 2};
coins = new Coin[values.length];
for (int i = 0; i < values.length; i++) {
coins[i] = new Coin(values[i], counts[i]);
}
cd = new CoinDenomination(coins);
amount = 205;
System.out.println(amount + ": " + cd.getChange(amount));
}
}
</pre><pre title="CodeMonkey97168" input="yes">
</pre>
RepNitanaJulliana, Analyst at A9
Hello, I am Nitana and I am working as a project manager in the Softage company. I am very honest ...
Repcheyennejmartin8547, Android Engineer at 247quickbookshelp
Hey CheyenneMartin and I am working as a machine operator. Today I am doing a new research like vasiyam specialist ...
Repannadwilliams31, Applications Developer at National Instruments
I am Anthony, Human Resources specialist with a decade of successful experience in hiring and employee management.I am a ...
RepTheresaHudson, IC3 at Broadsoft
I am Theresa , a Packer with a strong determination to finish all assignments in a timely manner. Inspected and ensured ...
Repamieeriddler, Financial Software Developer at ABC TECH SUPPORT
I am Amiee , a Professional Insurance Agent providing valuable services to consumers in need of insurance coverage. As a part ...
RepBrianJones, Associate at ASAPInfosystemsPvtLtd
BrianJones a Secret Service special agent with 4 years experience . I am exploiting new tricks Black magic spells to break ...
RepRomilKazi, Member Technical Staff at AppNexus
Training Assistant with 6+ years of experience preparing flawless presentations, assembling progress reports, and providing support and training to secretarial ...
RepAchillesDiaz, Digital marketing Experinced at Jabong
I am working as a web administrator. I have a basic understanding of tools for testing HTML, CSS validation, Kerala ...
RepMacHeist, Data Scientist at British Telecom
Mac, a Desktop Publishing Specialist at VitaGrey, where I provide document formatting and publishing support to a wide variety of ...
Replindagwingard, Android test engineer at ABC TECH SUPPORT
Hello, my name is Larry and I am a commercial loan officer. We are commercial loan officers who specialize in ...
RepLaylaLee, abc at AMD
Creative and dedicated photo editor with experience in photojournalism and marketing material development. I like to explore Best tantrik in ...
Repdavisjerryh, Backend Developer at Abs india pvt. ltd.
For the last two years, I am Effectively managing the team’s performance, conducting regular performance reviews and appraisals, and ...
Repaliciaable183, Analyst at 247quickbookshelp
I am an agent contract clerk who is responsible for handling the recruitment process. I advertise the vacancies for agents ...
Repaleasebkern, Personnel
Plant operators manage all things in an industrial plant or a power plant . They are supervising all the work . For ...
RepAidenKim, business planner manager at GPU
Expert business strategist with a sound understanding of organizational development and sales. Persuasive negotiator who uses integrity and professionalism in ...
Reproseljannet75, Financial Software Developer at Abs india pvt. ltd.
Je suis jannet , une conseillère en voyages qui conseille les clients sur les options de voyage et les voyages organisés ...
Repkrishamoris, Area Sales Manager at Accolite software
I am Krisha , an organized Cosmetology Instructor with 4 years of experience. Successful at keeping detailed records and personalizing lesson ...
RepEthelRector, Animator at AMD
EthelRector is a clerk working at Egghead Software with lots of experience . exploring new things Black magic to break up ...
RepNiaNorna, Analyst at ADP
I am an established career journalist with 15 years of writing and copy editing experience in the newsroom. Seeking to ...
RepOliveDavin, abc at 8x8
Experienced forester working in the industry for over 3 years. Solid background in the management of private and public forest ...
RepJudeSandin, Associate at Absolute Softech Ltd
My name is JudeSandin . I am working at Bold Ideas as a Web designer. With 5 years experience . I am ...
Repmonamore609, Android test engineer at Cisco Systems
Data entry clerks are responsible for inputting a high volume of data from multiple sources into a database, ensuring that ...
RepVirginiaSlifer, Analyst at A9
In my professional life, I am a dedicated genetics nurse with a deep interest in understanding the intricacies of human ...
RepAngieGibson, Jr. Software Engineer at Agilent Technologies
I am Angie , a Power Plant Operator with in-depth knowledge of all aspects of operation and maintenance and 3 years ...
RepFannieRamirez, Accountant at A9
I am a technically skilled Payroll bookkeeper responsible for the full charge bookkeeping function. My expertise includes knowledge of accepted ...
Replorfalinda8, Travel Agent at Creative Wealth
Hello, I am Janice. I help people make travel arrangements, which include booking flights, hotels, sightseeing tours, and making dining ...
Repsos337837, Personnel assistant at Bountiful Harvest Health Food Store
I am a Risten Personnel Assistant . I am responsible for maintaining human resource records of Department employees . I have a ...
Reppfisterruiz, Accountant at 247quickbookshelp
Door-to-door is a canvassing technique that is generally used for sales, marketing, advertising, evangelism or campaigning, in which I walk ...
Repwastonlare, Java Developer at Broadsoft
Waston , an executive in the finance and accounting area of the industry with expertise in teaching, training and managing my ...
Repqueznister, Android test engineer at Aspire Systems
I supervise the day-to-day operations of the store. assign duties. Determine staffing requirements, oversee their hiring, and, when needed, dismiss ...
RepPrishaDavin, abc at 8x8
I promote and provide extraordinary internal external guest services, answer questions, offer information, and provide assistance in a courteous manner ...
RepJamieWilliams, Android Engineer at Email Customer Service
My name is Jamie.and i am a Translator It's been almost 2 years since I worked in this ...
RepEileenBrown, Android Engineer at ABC TECH SUPPORT
Eileen Brown and I am a Product designer.And nowadays I am doing new research like istikhara for love marriage ...
RepBertMusi, Consultant at Accenture
I am a service technician working at Parade of Shoes . It's been almost 5 years since I have been ...
RepEmilioOtten, Applications Developer at ASU
I am Emilio, and I am currently the Medical Interpreter at Suy bank Hospital in Ohio , where I confidentially interpret ...
Repopalphelan234, Associate at 247quickbookshelp
I am a specialized Cardiac and vascular nurse at the Circus World . Here I meet different people and observe their ...
Repchomeshgeles, Android Engineer at ABC TECH SUPPORT
ReevesMildr , a builder working at Independent Investors . It has been almost 5 years since I have been working here. I ...
Repshushichiku, Associate at ASU
PaulKenda is a Farmworker working at Jackhammer Technologies . I am a specialist in my work here. I manage different things ...
create an int array A, of size 26 and initialize to 0
- Pragalathan M September 24, 2015for each letter S[i], in given string, if it is alphabet, then A[lower(S[i])-'a']++
iterate over A and return false if you find any A[i] equal to 0