kinshuk.ram
BAN USERYou can refer here for short details - k2code.blogspot.in/2014/04/differences-between-thread-and-process.html
- kinshuk.ram May 06, 2014Here is the code from k2code.blogspot.in/2009/12/write-program-to-add-two-long-positive.html:
node *long_add(mynode *h1, mynode *h2, mynode *h3) //h3 = h2+h1
{
node *c, *c1, *c2;
int sum, carry, digit;
carry = 0;
c1 = h1->next;
c2 = h2->next;
while(c1 != h1 && c2 != h2)
{
sum = c1->value + c2->value + carry;
digit = sum % 10;
carry = sum / 10;
h3 = insertNode(digit, h3);
c1 = c1->next;
c2 = c2->next;
}
if(c1 != h1)
{
c = c1;
h = h1;
}
else
{
c = c2;
h = h2;
}
while(c != h)
{
sum = c->value + carry;
digit = sum % 10;
carry = sum / 10;
h3 = insertNode(digit, h3);
c = c->next;
}
if(carry==1)
{
h3 = insertNode(carry, h3);
}
return(h3);
}
Here is my basic algo:
<b>1.</b> Break the number into 2 halfs "2133" = "21","33"
<b>2.</b> Now if first half is greater than 2nd half AND first half is same
size as 2nd half AND reverse(first) is greater than 2nd half, then
nextPalindrome = first + reverse(first), otherwise go to step 3.
Example 783322 = 783 + 387 = 783387. However 2133 wont hold. Nor will
1001, as first = 10, and last = 01 = 1
<b>3.</b> Increment the first half by 1,
first = first+1
nextPalindrome = first + reverse(first)
Example 2133, first = 21, first+1 = 22, Now concat first and reverse first = 2222
Here is my post - k2code.blogspot.in/2014/05/finding-next-palindrome-for-given-number.html
private static int getLengthOfInt(int x)
{
int length = String.valueOf(x).length();
return length;
}
private static int getReverseOfInt(int num){
String numStr = String.valueOf(num);
String reverse = new StringBuffer(numStr).reverse().toString();
return Integer.parseInt(reverse);
}
public static int getNextPalindrome(int number){
List<Integer> digitsArray = new ArrayList<Integer>();
List<Integer> digitsHalfArray = new ArrayList<Integer>();
digitsArray=NumberToDigits(number);
//if even number of digits, then consider middle two numbers as the middle
//convert digits from left to mid and mid-1 to a single number
//convert digits from right to mid to a single number
int count = digitsArray.size();
out.println(count);
int mid = count / 2;
int last = 0, first = 0,middle=0,i=0,j=0;
Boolean odd = false;
//if number of digits is even
if (count % 2 == 0)
{
last = DigitsToNumber(digitsArray, mid, count - 1);
first = DigitsToNumber(digitsArray, 0, mid - 1);
i = mid;
j = mid - 1;
}
else
{
odd=true;
middle = digitsArray.get(mid);
first = DigitsToNumber(digitsArray, 0, mid - 1);
last = DigitsToNumber(digitsArray, mid + 1, count - 1);
//mid is increased to adjust the midddle number in odd number of digits case
mid++;
i = mid;
j = mid - 2;
}
out.println(first);
out.println(last);
//also see if the size, first!=last in size for 1001
if (first > last
&& getLengthOfInt(first)==getLengthOfInt(last)
&& last < getReverseOfInt(first)
)
{
for (; i < count; ++i, j--)
{
digitsArray.add(i,digitsArray.get(j));
}
out.println(DigitsToNumber(digitsArray,0, count - 1));
}
else
{
//including the middle digit also
first = DigitsToNumber(digitsArray, 0, mid-1);
//adding 1 to middle value
first = first + 1;
digitsHalfArray = NumberToDigits(first);
digitsArray=digitsHalfArray;
int dHalfCount=digitsArray.size();
//adjustment for number with odd number of digits
if(odd==true)
{
mid--;
odd=false;
}
System.out.println(dHalfCount);
for (int k = mid-1; k >=0; --k)
{
digitsArray.add(dHalfCount++, digitsHalfArray.get(k));
}
out.println("Nearest Palindrome = " +DigitsToNumber(digitsArray, 0, digitsArray.size()-1));
}
return DigitsToNumber(digitsArray,0,digitsArray.size()-1);
}
private static List<Integer> NumberToDigits(int number)
{
List<Integer> digitsArray = new ArrayList<Integer>();
int[] tempArray = new int[51];
int tempCount = 0;
while (number > 0)
{
tempArray[tempCount++] = number % 10;
number = number / 10;
}
//reverse the digits and store in a list
for (int i = tempCount-1; i >= 0; --i)
{
digitsArray.add(tempArray[i]);
}
return digitsArray;
}
public static int DigitsToNumber(List<Integer> digitsArray,int start,int end)
{
int mulFactor = 10;
int num = 1;
int result = 0;
while (start <= end)
{
result += num * digitsArray.get(end--);
num *= mulFactor;
}
return result;
}
Here is my code in java (take from k2code.blogspot.in/2011/12/how-will-you-check-if-s1-is-rotated.html) :
boolean isRotation(String s1,String s2) {
return (s1.length() == s2.length()) && ((s1+s1).indexOf(s2) != -1);
}
Here is a solution without using extra space :
public static void rotate(int[][] matrix) {
int N = matrix.length;
for(int i = 0; i < N/2; ++i) {
for(int j = 0; j < (N+1)/2; ++j) {
int temp = matrix[i][j]; // save the top element;
matrix[i][j] = matrix[N-1-j][i]; // right -> top;
matrix[N-1-j][i] = matrix[N-1-i][N-1-j]; // bottom -> right;
matrix[N-1-i][N-1-j] = matrix[j][N-1-i];// left -> bottom;
matrix[j][N-1-i] = temp; // top -> left;
}
}
}
More mentioned here - k2code.blogspot.in/2014/03/rotate-n-n-matrix-by-90-degrees.html
if characters lies in the range a...z, then we can use following O(n) method without using extra space :
public static void removeDuplicates(char[] str) {
int map = 0;
for (int i = 0; i < str.length; i++) {
if ((map & (1 << (str[i] - 'a'))) > 0) // duplicate detected
str[i] = 0;
else // add unique char as a bit '1' to the map
map |= 1 << (str[i] - 'a');
}
}
(borrowed from k2code.blogspot.in/2014/01/remove-duplicate-characters-in-string.html)
Here is a solution using bitvector (borrowed from k2code.blogspot.in/2014/03/determine-if-string-has-all-unique.html) :
public static boolean isUniqueChars(String str) {
int checker = 0;
for (int i = 0; i < str.length(); ++i) {
int val = str.charAt(i) - 'a';
if ((checker & (1 << val)) > 0) return false;
checker |= (1 << val);
}
return true;
}
Here is the partition logic as pointed by others as well(from k2code.blogspot.in/2010/04/sorting-binary-array.html):
- kinshuk.ram May 07, 2014