kuber
BAN USERHere is the optimized code that will take less time to compute the combination... please comment if I missed anything..
void PrintAllCombination(int iNumber)
{
//To reduce the number of iterations.....
int iMaxLimit = pow((double)iNumber, (double)1/3.) + 0.5 ;
int iA, iB;
int iLoopCounter = 0;
int iFirstVal;
//If the cube of the value is equal to number then again we are adding some value in to it, that will be waste of iteration & time.
for( iA = 0; iA <= iMaxLimit; iA ++ )
{
//Calculating to reduce the multiplication process again & again.
iFirstVal = iA * iA * iA;
for( iB = 0; iB <= iMaxLimit; iB ++ )
{
if( iFirstVal + iB * iB * iB == iNumber )
printf("A := %d B := %d\n", iA, iB);
iLoopCounter ++;
}
}
printf("Total Iteration:%d", iLoopCounter);
}
Here is the solution for this problem using c/cpp. I checked this for few condition, please let me know if it is wrong...
int CheckString(const char *pcString)
{
if( pcString == NULL )
return 1;
char cCurr;
char cPrev;
int iCounter = 0;
int iRepeatCount = 1;
while( pcString[iCounter] != '\0' )
{
cPrev = pcString[iCounter];
iRepeatCount = 1;
for(int iCharTraverse = iCounter + 1; pcString[iCharTraverse] == cPrev; iCharTraverse ++)
{
cCurr = pcString[iCharTraverse];
if( cPrev == cCurr )
{
iRepeatCount ++;
iCounter++;
}
}
if( iRepeatCount > 1)
printf("%c%d", cPrev, iRepeatCount);
else
printf("%c", cPrev);
iCounter++;
}
return 0;
}
int main(int argc, CHAR** argv)
{
CheckString("AAACCCBBD");
//output will be : A3C3B2D
return 0;
}
Create the library and provide this library along with header files(which contains all the API's signature ).
- kuber September 13, 2014