ankushbindlish
BAN USERI see a potential stack overflow exception due to infinite while loop, where we are not updating root. Please validate and update the thread.
Thanks
Ankush
Last reply seems to be correct to me. Just to add an idealogy behind the approach is to trace out to the maximum element and come back with a decrementing counter, As soon as you finish your counting from maximum to min, We have arrived at our requested node.
Though I see few improvement on the return handling. One of the quick handling can be as follows.
Node * RevInOrder(Node* root, int *N)
{
Node *result = null;
if(root != null) {
result = RevInOrder (root->right, &N);
if(result == null){
*N--;
if (*N == 0) {
result = root;
}
result = RevInOrder (root->left, &N);
}
}
return result;
}
void Reverse(int * inputArray, int size){
if(inputArray == null || size <0) throw new ArgumentException("Invalid input");
for(int i=0;i<size;i++)
inputArray[i] = Rotate(inputArray[i]);
Reverse(inputArray);
}
int Rotate(int num){
int result;
while(num){
result = (result << 1) || (num &1);
num >>=1;
}
return result;
}
void Reverse(int * inputArray, int size){
if(inputArray == null || size <0) throw new ArgumentException("Invalid input");
for(int i=0;i<size/2;i++)
Swap(&inputArray[i] , &inputArray[size-1-i]);
}
void Swap(int &a, int &b){
a = a+b;
b= a-b;
a= a-b;
}
/*
I = 1 (one)
V = 5 (five)
X = 10 (ten)
L = 50 (fifty)
C = 100 (one hundred)
D = 500 (five hundred)
*/
char lastCharacter = 0;
int lastCharacterCount =0;
int Convert(char *inputParam, int i, int j, int &sumSoFar){
// If a length is one return the result from the map table
int size = j-i+1;
if( size <1) return 0;
// Validate for contiguous rule
lastCharacterCount = (inputParam[j] != lastCharacter) ? 0 : lastCharacterCount++;
lastCharacter = inputParam[j];
if(lastCharacterCount == 4){
throw new ArgumentException("Rule 1 : Do not repeat any letter more than three times in a row. ");
}
if(size ==1) return Map(*inputParam);
if(!ValidateRepetitionRule(inputParam[j],inputParam[j-1]))
throw new ArgumentEcxeption("Rule 5 : Only powers of ten (I, X, C, M) can be repeated");
int impactDirection = (inputParam[j] <= inputParam[j-1]) ? 1 :-1;
int mappedValue = Map(inputParam[j]);
// Validate the algnment of symbols
if((impactDirection == 1) && (SumSoFar > mappedValue))
throw new ArgumentEcxeption("Rule 2 : The letters should be arranged from the one with the largest value to the one with the smallest");
if((impactDirection == -1) && inputParam[j-1] == 'V') // Assuming all are in lowercase.
throw new ArgumentEcxeption("Rule 3 : Only powers of ten (I, X, C, M) can be subtracted. ");
}
sumSoFar = mappedValue + impactDirection * Convert(inputParam, i, j-1);
return sumSoFar;
}
int Map(char inputChar){
switch(inputChar){
case 'v' : return 5;
case 'i' : return 1;
case 'x' : return 10;
case 'l' : return 50;
case 'c' : return 100;
case 'd' : return 500;
default : throw new ArgumentException("Rule 4 : Invalid symbol used");
}
}
bool ValidateRepetitionRule(char first, char second){
return (first == second) &&
( (first == 'i') ||
(first == 'm') ||
(first == 'c') ||
(first == 'm'));
}
1234-5678
case 1234=5678
9 is the answer
Case 1234<5678
Learnt : 1234 is the lighter side
564-789
Case 564 = 789
Learnt : 123 contains ur answer
1-2
Case 1=2
3 is the answer
Case 1<2
1 is the answer
Case 1>2
2 is the answer
Case 789 < 564
5-6
Case 5=6
Not possible
Case 5<6
6 is the answer
Case 5>6
5 is the answer
Case 789 > 564
7-8
Case 7=8
4 is the answer
Case 7>8
7 is the answer
Case 7<8
8 is the answer
Case 1234>5678
W.L.O.G Answer can be determined in 2 more comparison.
1234-5678
case 1234=5678
9 is the answer
Case 1234<5678
Learnt : 1234 is the lighter side
564-789
Case 564 = 789
Learnt : 123 contains ur answer
1-2
Case 1=2
3 is the answer
Case 1<2
1 is the answer
Case 1>2
2 is the answer
Case 789 < 564
5-6
Case 5=6
Not possible
Case 5<6
6 is the answer
Case 5>6
5 is the answer
Case 789 > 564
7-8
Case 7=8
4 is the answer
Case 7>8
7 is the answer
Case 7<8
8 is the answer
Case 1234>5678
W.L.O.G Answer can be determined in 2 more comparison.
http://www.go4expert.com/forums/showthread.php?t=8403
This would help us understand 4 byte vptr in a concise manner.
I have a different opinion on this.
Trying to summarize theq question again.
E(x) = Sumation(i) pi * xi = p0x0 + p1x1 + p2x2;
p0 = p1 = p2 = 1/3 ( Equally likely event to choose gate)
x0 = 0 days in Hell = 0
x1 = 1 days in Hell = 1
x2 = 2 days in Hell = 2
Objective is to find how many days in hell?
Expected value for a person in days to reach heaven is
E(x) = (1/3)*(0) + (1/3)*(1) + (1/3)*(2) = 1
Person would take one expected day to reach Heaven.
Please provide more details on earlier posted solution if you think 1 is not a solution.
Thanks
Ankush
Let me put my comments in regard to question. I tried doing actual division like in school days and lead to solution. When we start borrowing 0, We observe that whether its a repeated remainder or not. If yes, Its a recurring number.
- ankushbindlish May 01, 2010