Prashant R Kumar
BAN USERI have given you +1 despite the fact that you are supposed to return the output vector, and that too without extra space..
- Prashant R Kumar September 11, 2012void printVec(vector<int> sol, vector<char> sign)
{
for(int i = 0; i <sol.size(); i++)
{
cout <<sign[i]<<sol[i];
}
}
void PrintCombination(vector<int> numbers, vector<int> sol, vector<char> sign, int target1, int pos)
{
if(target1 == 0 )
{
cout << endl << "-------------Solution-------------"<<endl;
printVec(sol, sign);
cout << endl << "-------------Solution-------------"<<endl;
return;
}
for(int i = pos; i<numbers.size(); i++)
{
int temp1 = target1-numbers[i];
sol.push_back(numbers[i]);
sign.push_back('+');
PrintCombination(numbers, sol, sign, temp1, i+1);
//sol.pop_back();
//sol.push_back(numbers[i]);
sign.pop_back();
sign.push_back('-');
temp1 = target1+numbers[i];
PrintCombination(numbers, sol, sign, temp1, i+1);
sign.pop_back();
sol.pop_back();
}
}
int main()
{
vector<int> numbers;
vector<int> solu;
vector<char> sign;
numbers.push_back(1);
numbers.push_back(2);
numbers.push_back(3);
numbers.push_back(4);
numbers.push_back(5);
PrintCombination(numbers, solu, sign, 3, 0);
return 0;
}
O(nk) is not a problem...Problem is making it inplace..Which looks impossible..
- Prashant R Kumar September 09, 2012void printSoul(vector<int> sol)
{
cout<<endl;
for(vector<int>:: iterator it = sol.begin(); it != sol.end(); it++)
cout <<*it << ".";
}
void printValidIp(string ipn, vector<int> solution)
{
int val = 0;
for(int i = 0; i < ipn.size() && i <3 ; i++)
{
char c= ipn[i];
int dig = atoi(&c);
val = val*10+dig;
if( solution.size() <=2 )
{
if( val <= 255 )
{
solution.push_back(val);
printValidIp(ipn.substr(i+1), solution);
solution.pop_back();
}
}
else
{
if( ipn.size() >=4)
return;
if( ipn.size() == i+1 && val <= 255 )
{
solution.push_back(val);
printSoul(solution);
}
}
}
}
Compile time..At run time it just goes out and based on offset picks up the function pointer and makes a call..
- Prashant R Kumar September 08, 2012You have to find length of the linked list..Which is ofcourse an extra computation.. While this can be solved without that..
- Prashant R Kumar September 08, 2012What possibly you can do with void pointer unless you know the type before hand..Useless..Talking about the original question, it can easily be done in Java or C# as all the classes are derived from one super class..In C it's impossible..In C++ it can be done if all the types/classes are derived from one base class...
- Prashant R Kumar September 07, 2012Only problem is it runs O(2n)..While it can be done in O(n)..
- Prashant R Kumar September 07, 2012First there has to common repository for all the telephone number allocated... So a hosted ditributed cache server, a memcached can be used.. Memcached is a no-sql distributed in memory db.. Here a key is the telephone number value could be 0.
Now when a new telephone number has to be issued, an application will make HTTP / TCP/IP call. Will call the server asking for a permission.. Over HTTP or TCP/IP I dont care, HTTP is better as port and other crap you dont have to worry..and you dont have to write a client.. So your server is just a web server listining on 80 port.. Your web server will get the request, it will contact memcached servers for validation..If telephone number is avialable, it will return 200 OK else returns 201 OK. Its not as simple as it sounds as you will have to write an apache plugin...
First there has to common repository for all the telephone number allocated... So a hosted ditributed cache server, a memcached can be used.. Memcached is a no-sql distributed in memory db.. Here a key is the telephone number value could be 0.
Now when a new telephone number has to be issued, an application will make HTTP / TCP/IP call. Will call the server asking for a permission.. Over HTTP or TCP/IP I dont care, HTTP is better as port and other crap you dont have to worry..and you dont have to write a client.. So your server is just a web server listining on 80 port.. Your web server will get the request, it will contact memcached servers for validation..If telephone number is avialable, it will return 200 OK else returns 201 OK. Its not as simple as it sounds as you will have to write an apache plugin...
Better than other one..Just that memory management was not that good as rightly pointed by somebody above..Nontheless +1.
- Prashant R Kumar September 07, 2012Then its time for you to get a new compiler..
Are you mad of something...Do you think you are the smatest one around...
p->value = q->value;
q->value = temp;
p = q->link;
What is value in your struct..
Your struct defnition is..
struct node
{
char data;
struct node* link;
};
One constraint was not creating extra node..and you are doing exactly that..
Link LastNode = new Link();
Working code..
struct Link
{
int data;
Link * next;
};
Link* reversePairWise(Link* lnk)
{
if(lnk == NULL ) return lnk;
if(lnk->next == NULL ) return lnk;
Link* t1 = lnk->next;
lnk->next = lnk->next->next;
t1->next = lnk;
Link* prev = t1->next;
lnk = t1->next->next;
while(lnk != NULL && lnk->next != NULL )
{
Link* temp1 = lnk->next;
lnk->next = lnk->next->next;
temp1->next = lnk;
prev->next = temp1;
prev = prev->next->next;
lnk = lnk->next;
}
return t1;
}
Lots of flaw it will not swap first two nodes..
- Prashant R Kumar September 06, 2012if(!start || !start-> link)
return;
Will not even compile..
- Prashant R Kumar September 06, 2012Good question and equally good answer..
- Prashant R Kumar September 05, 2012WTF?
- Prashant R Kumar September 01, 2012Anonymous above did you by any chance forget to give him +1..?
- Prashant R Kumar September 01, 2012Keep three pointers, one for each string..Always move pointer of string C..Move pointer of either A or B if they match with element pointed by C..If they do not match return false..That means no interleaving..Move pointer of A or B whichever equal to char pointed by pointer of C..If pointer of C reached its end and pointer of A as well as B should reach its end..if they don't return false..else return true..
- Prashant R Kumar August 27, 2012Read his code correctly what he is doing is allright..He is just swapping all the positive number towards the end all negative number towrads the begining..In that sense he is swapping even positive with positive, that can be avoided.
I try to make it more logical..Consider the case when there are only positive and negative numbers and no zero..In that case we use two pointer from two ends and swap positive with negative..Here what we have to do is, swap positive with negative as well as zero..Swap negative with positive but swap with zero too..
What is most unique.? It's like saying coldest ice...:-)..
- Prashant R Kumar August 20, 2012Sort every word separately. Build a trie out of them.
- Prashant R Kumar August 18, 2012As soon as you find a loop open that loop..
It will look like this..
|-d-3-4-5-5
A-b-c-|
|-3-4-6-8 now intersection point C can be fond quite easily.
Abbé, bhai log Asok theek Bol raha hai..
- Prashant R Kumar August 14, 2012Can be done in logn..Keep binary searching on each row till you are encountering one..Remove those rows which got zero..Last row left is the solution..
- Prashant R Kumar August 14, 2012
I have given you +1 despite the fact that you are supposed to return the output vector, and that too without extra space..
- Prashant R Kumar September 11, 2012