How do I check if a number is a palindrome?
Posted on August 3, 2013
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Testing the coment
Kinshuk Chandra - Aug 6, 2013
Testing the coment
How do I check if a number is a palindrome?
Posted on August 3, 2013
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
Note:
First, the problem statement did not specify if negative integers qualify as palindromes. Does negative integer such as -1 qualify as a palindrome? Finding out the full requirements of a problem before coding is what every programmer must do. For the purpose of discussion here, we define negative integers as non-palindromes.
Approaches to solve the problem
Approach 1:
The most intuitive approach is to first represent the integer as a string, since it is more convenient to manipulate.
[Read More]Convert integers to roman numbers
Posted on January 5, 2012
(Last modified on August 7, 2020)
| 3 minutes
| Kinshuk Chandra
Question: write an algorithm to convert an integer into roman number. For example, 1 -> I, 2 -> II, or 4 -> IV.
Solution: in order to solve this problem, we must know the rules of writing roman numerals. For example, I is 1 and V is 5 but IV is not 6 but 4 (5 -1). Moreover, there is no 0 number in roman numeral system. Here is the link to an article about roman numerals if you are unfamiliar with the system.
[Read More]Convert a Number (unknown base) to a Base 10 Number
Posted on October 9, 2011
(Last modified on August 7, 2020)
| 3 minutes
| Kinshuk Chandra
Problem Given an integer, write a program that converts the given number to a number (in base 10). The base of the given number is unknown.
Solution The problem statement states that the base of the given number is unknown. Thus to proceed one must need to assume a base for the number. It is practically safe to assume that the digit with the maximum value in the number denotes the maximum that can be accounted in the unknown base.
[Read More]itoa() in Java
Posted on October 9, 2011
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
Problem: Write a function to convert an integer value to its ASCII equivalent and return as a character array, similar to itoa() function of C++.
Solution: The solution may seem tricky at first because the array is constructed left-to-right, whereas the number is read right-to-left. The easiest approach is to construct an array with digits from right-to-left and then reverse the array itself. The result being the number representation in character array.
[Read More]External Sorting - How do you sort a million 32-bit integers in 2MB RAM?
Posted on April 26, 2010
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
A million 32-bit integers is 4MB. We cannot fit them all in the given RAM size and we have to do external sorting. It is an open ended question which is interactive.
Let’s assume that -
The list of numbers is on disk (as we cannot fit them all in RAM).
There is a sort function which sorts the given list effectively i.e. we do not have to worry about a particular sorting algorithm.
[Read More]Missing integers
Posted on April 26, 2010
(Last modified on August 7, 2020)
| 3 minutes
| Kinshuk Chandra
If you have a cheap mainstream commodity hardware consisting of a decent processor and 1GB RAM, how would you find a few (more than one) missing integers in a list saved on disk containing all of the unique 2^32 integers?
One approach is to use an array of boolean flags and marking them as true for present integers (index of the array denotes flag for the integer). Traversing the flags array again would give the missing integers.
[Read More]how to chk if a no. is integer or not.
Posted on April 10, 2010
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Source : http://stackoverflow.com/questions/784563/c-check-whether-is-number-is-int-float/784825
#include
using namespace std;
int main(){
double num1;
cin»num1;
if(static_cast(num1) == num1)//casts num1 to type int
cout«"Integer"<
else
cout«"Not Integer"<
return 0;
}
Another method is
char *test = "asdf1234";
int i;
for (i = 0; i < strlen (test); i++)
{ if (isdigit (test[i]))fprintf (stdout, "test[%d] is a digit!\n", i);
}
Euclid's Algorithm : GCD of 2 integers
Posted on January 5, 2010
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
The following example solves a classic elementary problem: “Reduce a given fraction to its lowest terms”, e.g. we want to write 2/3, not 4/6. We solve this problem by finding the greatest common divisor (gcd) of the numerator and the denominator by using the famous approach called Euclid’s algorithm.
C Code
Iterative solution
// Iterative algorithm int gcd(int a, int b) { int temp; while(b) { temp = a % b; a = b; b = temp; } return(a); } Recursive solution
[Read More]Given an array of n integers from 1 to n with one integer repeated twice
Posted on January 5, 2010
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
Problem - Given an array of n integers from 1 to n with one integer repeated twice
This can be done via normal array traversal or hash table to find duplicates. But since we are given that the integers are from 1 to n, then there are better methods available.
Approach 1 - Arithmetic sum approach
So, the efficient method to solve it calculate the sum of actual input array, and expected sum of the array.
[Read More]