You should not alter the list while iterating through it.
Serializable and Externalizable both are used to persist the state of object into a file or byte stream.serializable is marker interface having no methods in it.while Externalizable have two methods readExternal(),writeExternal() to control the processing of seriliazation.externazable extends serializable interface.
Implementation of sockets is in Session Layer and JVM is in application Layer.
To get the details of the threads running use getAllStackTraces
The sole purpose of hashcode() is to come up with efficient way of finding Hash buckets. Once a bucket is determined the overridden equals method can be used to retrieve that element.
Without hashcode method, all the elements can be inside a single bucket and hence result in inefficient hashes.
A transient variable is a variable that cannot be serialized. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the val ue of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null
http://technologiquepanorama.wordpress.com/2009/02/12/use-of-hashcode-and-equals/
Use Inheritance if its a "is-a" relationship. Ex : Apple is a Fruit.
Use Composition if its a "has-a" relationship. Ex : Customer has an Address.
Thursday, February 24, 2011
Friday, February 11, 2011
My own shuffle array code
#include "iostream"
#include
#include
#include
#include
#include
using namespace std;
void shuffle(int A[],int size)
{
int tmp;
for(int i = 0; i
{
srand ( time(NULL) );
int ran = rand()%(size-i);
tmp = A[size - i];
A[size - i] = A[ran];
A[ran] = tmp;
}
}
int main()
{
int A[10] = {1,2,3,4,5,6,7,8,9,10};
shuffle(A,9);
int j = 0;
while(j<9)
{
cout << A[j++] << " ";
}
getch();
return 0;
}
#include
#include
#include
#include
#include
using namespace std;
void shuffle(int A[],int size)
{
int tmp;
for(int i = 0; i
srand ( time(NULL) );
int ran = rand()%(size-i);
tmp = A[size - i];
A[size - i] = A[ran];
A[ran] = tmp;
}
}
int main()
{
int A[10] = {1,2,3,4,5,6,7,8,9,10};
shuffle(A,9);
int j = 0;
while(j<9)
{
cout << A[j++] << " ";
}
getch();
return 0;
}
Really gud code for Most occuring number in the array.
map m;
int currentMax = -999;
int maxCount = 0;
for(int i=0;i<10;i++)
{
int updated = m[a[i]]++; //Increment the value of key for counting occurances
updated++; // due to post increment
if (maxCount < updated) {
maxCount = updated;
currentMax = i;
}
}
int currentMax = -999;
int maxCount = 0;
for(int i=0;i<10;i++)
{
int updated = m[a[i]]++; //Increment the value of key for counting occurances
updated++; // due to post increment
if (maxCount < updated) {
maxCount = updated;
currentMax = i;
}
}
Thursday, February 10, 2011
My own finding all palendromes in a string
#include "iostream"
#include
using namespace std;
void prntstr(char *str,int j, int i)
{
while(j<=i) { cout << str[j]; j++; } cout << "\n"; } void isPalen(char* str, int pos) { int j =pos; int i = pos,count = 0; int k,l; for(i,j;(i<=11)&&(j>=0);i++,j--)
{
if(str[j] == str[i+1])
{
// cout << str[j] << " " << str[i+1] << "\n"; count++; k = j;l=i+1; } } // cout << count; if(count>0){
count = 0;
prntstr(str,k,l);
// cout << k << " " << l;
}
}
int main()
{
char* str = "heehasdiooi";
for(int i = 1; i< 11; i++)
{
isPalen(str,i);
// cout << str[i] << "\n";
}
getch();
}
#include
using namespace std;
void prntstr(char *str,int j, int i)
{
while(j<=i) { cout << str[j]; j++; } cout << "\n"; } void isPalen(char* str, int pos) { int j =pos; int i = pos,count = 0; int k,l; for(i,j;(i<=11)&&(j>=0);i++,j--)
{
if(str[j] == str[i+1])
{
// cout << str[j] << " " << str[i+1] << "\n"; count++; k = j;l=i+1; } } // cout << count; if(count>0){
count = 0;
prntstr(str,k,l);
// cout << k << " " << l;
}
}
int main()
{
char* str = "heehasdiooi";
for(int i = 1; i< 11; i++)
{
isPalen(str,i);
// cout << str[i] << "\n";
}
getch();
}
My first dynamic programming code
courtesy Mr Kadane.
#include "iostream"
#include
using namespace std;
int maxsumdp(int A[],int n)
{
int sum = 0, maxsum = 0, start = 0, end = 0, fstart, fend;
for(int i = 0;i maxsum)
{
fstart = start;
maxsum = sum;
end=i;
}
if(sum < 0)
{
sum = 0;
start = end + 1;
}
}
cout << maxsum << " " << fstart << " " << end;
}
int main()
{
int A[] = {1,-3,1,3,-1,3,-5};
maxsumdp(A,7);
getch();
}
#include "iostream"
#include
using namespace std;
int maxsumdp(int A[],int n)
{
int sum = 0, maxsum = 0, start = 0, end = 0, fstart, fend;
for(int i = 0;i
{
fstart = start;
maxsum = sum;
end=i;
}
if(sum < 0)
{
sum = 0;
start = end + 1;
}
}
cout << maxsum << " " << fstart << " " << end;
}
int main()
{
int A[] = {1,-3,1,3,-1,3,-5};
maxsumdp(A,7);
getch();
}
Sunday, January 30, 2011
Finding all palindromes in a string (BTW Half Century)
#include
#include
#include
#include
void substring(char *stng, int from, int to){
char sstr[to-from+2];
for(int i=0; i<=(to-from+1); i++){ sstr[i] = stng[i+from]; } sstr[to-from+1] = '\0'; printf("%s\n", sstr); } void palin(char *string){ int len; len = strlen(string); for(int i=1; i=0;j--,k++){
if(string[j] == string[k]){
substring(string, j, k);
}
else
break;
}
}
for(int i=0; i=0; j--,k++){
if(string[j] == string[k]){
substring(string,j ,k);
}
else
break;
}
}
}
}
int main(){
char str[] = "ABRARBABACDDCABABRARBA";
//printf("Length = %d\n", strlen(str));
palin(str);
getch();
return 0;
}
#include
#include
#include
void substring(char *stng, int from, int to){
char sstr[to-from+2];
for(int i=0; i<=(to-from+1); i++){ sstr[i] = stng[i+from]; } sstr[to-from+1] = '\0'; printf("%s\n", sstr); } void palin(char *string){ int len; len = strlen(string); for(int i=1; i
if(string[j] == string[k]){
substring(string, j, k);
}
else
break;
}
}
for(int i=0; i
if(string[j] == string[k]){
substring(string,j ,k);
}
else
break;
}
}
}
}
int main(){
char str[] = "ABRARBABACDDCABABRARBA";
//printf("Length = %d\n", strlen(str));
palin(str);
getch();
return 0;
}
Thursday, January 27, 2011
Algorithm: to find the median of two sorted arrays
1) Calculate the medians m1 and m2 of the input arrays ar1[]
and ar2[] respectively.
2) If m1 and m2 both are equal then we are done.
return m1 (or m2)
3) If m1 is greater than m2, then median is present in one
of the below two subarrays.
a) From first element of ar1 to m1 (ar1[0...|_n/2_|])
b) From m2 to last element of ar2 (ar2[|_n/2_|...n-1])
4) If m2 is greater than m1, then median is present in one
of the below two subarrays.
a) From m1 to last element of ar1 (ar1[|_n/2_|...n-1])
b) From first element of ar2 to m2 (ar2[0...|_n/2_|])
4) Repeat the above process until size of both the subarrays
becomes 2.
5) If size of the two arrays is 2 then use below formula to get
the median.
Median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
Example:
ar1[] = {1, 12, 15, 26, 38}
ar2[] = {2, 13, 17, 30, 45}
For above two arrays m1 = 15 and m2 = 17
For the above ar1[] and ar2[], m1 is smaller than m2. So median is present in one of the following two subarrays.
[15, 26, 38] and [2, 13, 17]
Let us repeat the process for above two subarrays:
m1 = 26 m2 = 13.
m1 is greater than m2. So the subarrays become
[15, 26] and [13, 17]
Now size is 2, so median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
= (max(15, 13) + min(26, 17))/2
= (15 + 17)/2
= 16
Implementation:
?
#include
int max(int, int); /* to get maximum of two integers */
int min(int, int); /* to get minimum of two integeres */
int median(int [], int); /* to get median of a single array */
/* This function returns median of ar1[] and ar2[].
Assumptions in this function:
Both ar1[] and ar2[] are sorted arrays
Both have n elements */
int getMedian(int ar1[], int ar2[], int n)
{
int m1; /* For median of ar1 */
int m2; /* For median of ar2 */
/* return -1 for invalid input */
if(n <= 0) return -1; if(n == 1) return (ar1[0] + ar2[0])/2; if (n == 2) return (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1])) / 2; m1 = median(ar1, n); /* get the median of the first array */ m2 = median(ar2, n); /* get the median of the second array */ /* If medians are equal then return either m1 or m2 */ if(m1 == m2) return m1; /* if m1 < m2 then median must exist in ar1[m1....] and ar2[....m2] */ if (m1 < m2) return getMedian(ar1 + n/2, ar2, n - n/2); /* if m1 > m2 then median must exist in ar1[....m1] and ar2[m2...] */
return getMedian(ar2 + n/2, ar1, n - n/2);
}
/* Driver program to test above function */
int main()
{
int ar1[] = {1, 12, 15, 26, 38};
int ar2[] = {2, 13, 17, 30, 45};
printf("%d", getMedian(ar1, ar2, 5)) ;
getchar();
return 0;
}
/* Utility functions */
int max(int x, int y)
{
return x > y? x : y;
}
int min(int x, int y)
{
return x > y? y : x;
}
/* Function to get median of a single array */
int median(int arr[], int n)
{
if(n%2 == 0)
return (arr[n/2] + arr[n/2-1])/2;
else
return arr[n/2];
}
Time Complexity: O(logn)
Algorithmic Paradigm: Divide and Conquer
and ar2[] respectively.
2) If m1 and m2 both are equal then we are done.
return m1 (or m2)
3) If m1 is greater than m2, then median is present in one
of the below two subarrays.
a) From first element of ar1 to m1 (ar1[0...|_n/2_|])
b) From m2 to last element of ar2 (ar2[|_n/2_|...n-1])
4) If m2 is greater than m1, then median is present in one
of the below two subarrays.
a) From m1 to last element of ar1 (ar1[|_n/2_|...n-1])
b) From first element of ar2 to m2 (ar2[0...|_n/2_|])
4) Repeat the above process until size of both the subarrays
becomes 2.
5) If size of the two arrays is 2 then use below formula to get
the median.
Median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
Example:
ar1[] = {1, 12, 15, 26, 38}
ar2[] = {2, 13, 17, 30, 45}
For above two arrays m1 = 15 and m2 = 17
For the above ar1[] and ar2[], m1 is smaller than m2. So median is present in one of the following two subarrays.
[15, 26, 38] and [2, 13, 17]
Let us repeat the process for above two subarrays:
m1 = 26 m2 = 13.
m1 is greater than m2. So the subarrays become
[15, 26] and [13, 17]
Now size is 2, so median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
= (max(15, 13) + min(26, 17))/2
= (15 + 17)/2
= 16
Implementation:
?
#include
int max(int, int); /* to get maximum of two integers */
int min(int, int); /* to get minimum of two integeres */
int median(int [], int); /* to get median of a single array */
/* This function returns median of ar1[] and ar2[].
Assumptions in this function:
Both ar1[] and ar2[] are sorted arrays
Both have n elements */
int getMedian(int ar1[], int ar2[], int n)
{
int m1; /* For median of ar1 */
int m2; /* For median of ar2 */
/* return -1 for invalid input */
if(n <= 0) return -1; if(n == 1) return (ar1[0] + ar2[0])/2; if (n == 2) return (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1])) / 2; m1 = median(ar1, n); /* get the median of the first array */ m2 = median(ar2, n); /* get the median of the second array */ /* If medians are equal then return either m1 or m2 */ if(m1 == m2) return m1; /* if m1 < m2 then median must exist in ar1[m1....] and ar2[....m2] */ if (m1 < m2) return getMedian(ar1 + n/2, ar2, n - n/2); /* if m1 > m2 then median must exist in ar1[....m1] and ar2[m2...] */
return getMedian(ar2 + n/2, ar1, n - n/2);
}
/* Driver program to test above function */
int main()
{
int ar1[] = {1, 12, 15, 26, 38};
int ar2[] = {2, 13, 17, 30, 45};
printf("%d", getMedian(ar1, ar2, 5)) ;
getchar();
return 0;
}
/* Utility functions */
int max(int x, int y)
{
return x > y? x : y;
}
int min(int x, int y)
{
return x > y? y : x;
}
/* Function to get median of a single array */
int median(int arr[], int n)
{
if(n%2 == 0)
return (arr[n/2] + arr[n/2-1])/2;
else
return arr[n/2];
}
Time Complexity: O(logn)
Algorithmic Paradigm: Divide and Conquer
Subscribe to:
Posts (Atom)