March 7, 2010, 11:17 pm
public static void printSpirally(int[][] a) {
int N = a[0].length;
for (int i = N - 1, j = 0; i > 0; i--, j++) {
for (int k = j; k < i; k++)System.out.println(a[j][k]);
for (int k = j; k < i; k++)System.out.println(a[k][i]);
for (int k = i; k > j; k--)System.out.println(a[i][k]);
for (int k = i; k > j; k--)System.out.println(a[k][j]);
}
int middle = (N / 2);
if ((N % 2) == 1) {
System.out.println(a[middle][middle]);
}
}
March 7, 2010, 10:59 am
public static boolean isVowel(char inp) {
char c = Character.toUpperCase(inp);
if ((c == 'A') || (c == 'E') || (c == 'I') || (c == 'O') || (c == 'U')) {
return true;
}
return false;
}
public static void reverseVowels(char[] a) {
int start = 0;
int end = a.length - 1;
while (start < end) {
while ((start < a.length) && !isVowel(a[start])) {
start++;
}
while ((end > 0) && !isVowel(a[end])) {
end--;
}
if ((start > = end) || (start == a.length) || (end == -1)) {
return;
}
//swap start and end;
char t = a[start];
a[start] = a[end];
a[end] = t;
start++;
end--;
}
}
March 7, 2010, 8:45 am
Print all nodes level by level
public static void printlevelByLevel(Node root)
{
Node current = root;
Queue q = new Queue();
q.add(root);
while(!q.isEmpty())
{
Node c =(Node) q.remove();
System.out.print(c.val+",");
if(c.left!=null)q.add(c.left);
if(c.right!=null)q.add(c.right);
}
}
Print all nodes level by level and priting also when each level finishes
public static void printlevelByLevel2Queues(Node root)
{
Node current = root;
Queue q[] = new Queue[]{new Queue(),new Queue()};
int currentQ=0;
q[currentQ].add(root);
while(!q[currentQ].isEmpty() || !q[1-currentQ].isEmpty() )
{
if(q[currentQ].isEmpty())
{
System.out.println("One level Finished... Switching queue...");
currentQ = 1- currentQ;
}
Node c =(Node) q[currentQ].remove();
System.out.print(c.val+",");
if(c.left!=null)q[1-currentQ].add(c.left);
if(c.right!=null)q[1-currentQ].add(c.right);
}
}
March 7, 2010, 12:05 am
public static void shuffle(int[] a)
{
int N = a.length;
Random r = new Random(new Date().getTime());
for (int i = 0; i < a.length-1; i++)
{
int tmp=0;
// Find a random element from i to N-1
int randElem = i + r.nextInt(N-i);
//Replace randElem with current element
tmp = a[i];a[i]=a[randElem];a[randElem]=tmp;
}
}
March 6, 2010, 9:34 pm
Find if the sum of two elements in a sorted array sum up to b (a number) with complexity of O(n).
public static void find2ElementsOfSum(int[] a,int S)
{
int left = 0;
int right = a.length-1;
while(left < right)
{
int sum = a[left]+a[right];
System.out.println("current Sum="+sum);
if(sum==S){
System.out.println("FOUND left="+left+" rigt="+right);
return;
}
if(sum < S)
{
left++;
}else
{
right--;
}
}
System.out.println("NOT FOUND");
}
March 6, 2010, 6:43 am
Given an array of positive and negative numbers. Find the contiguous sub array of maximum sum.
public static void subArraySum(int a[])
{
int i=0;
int j=0;
int start=0;
int end=0;
int maxSum = -99999;
int cSum = 0;
while(j < a.length)
{
cSum +=a[j];
if(cSum>maxSum)// Set this as the new result
{
start=i;
end = j;
maxSum=cSum;
}else if(cSum < 0)
{
cSum=0;
i=j+1;
}
j++;
}
System.out.println("start="+start+" end="+end+" maxSum="+maxSum);
}
March 5, 2010, 10:28 pm
public static int binarySearch(int a[],int s)
{
int N = a.length;
int start=0;
int end = N-1;
while(start <= end)
{
int mid = start+(end-start)/2;
if(a[mid] > s)
{
end=mid-1;
}else if(a[mid] < s)
{
start=mid+1;
}else
{
return mid;
}
}
return -(start+1);
}
February 21, 2010, 11:17 am
public static void sort(int[] a)
{
for (int i = 1; i < a.length; i++)
{
int current_val= a[i];
int j= i;
while(j > 0 && a[j-1] > current_val)
{
a[j]=a[j-1];
j--;
}
a[j]=current_val;
}
}
February 21, 2010, 12:30 am
Creating an invitation/request page from an iframe application page is fairly easy, but I could not find a single place with all the required steps. So thought of noting it down for my own and others use.
1. From the applications settings page(Advanced Settings) disable the Sandbox mode
2. You need to enable Communication Between Your Site and Facebook as per the following document :
http://wiki.developers.facebook.com/index.php/Connect/Setting_Up_Your_Site
3. You need to set Connect URL. This is an unnecessary looking but a very important step, even if you do not have a facebook connect site.
4. Finally the code :
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<body>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
<script type="text/javascript">
FB.init("<APPLICATION KEY>", "fb/xd_receiver.htm");
</script>
<fb:serverFbml style="width: 755px;">
<script type="text/fbml">
<fb:fbml>
<fb:request-form
action="http://www.edostee.com/fb/"
method="POST"
invite="false"
type="<APPLICATION NAME>"
content="REQUEST CONTENT <a href='http://apps.facebook.com/edostee/'>eDostee</a> - fastest growing and brand new Indian Dating application
<fb:req-choice url='http://apps.facebook.com/edostee'
label='Join eDostee' />
"
>
<fb:multi-friend-selector
showborder="false"
actiontext="Invite your friends to join eDostee.">
</fb:request-form>
</fb:fbml>
</script>
</fb:serverFbml>
</body>
</html>
Above page will be rendered as below

February 12, 2010, 1:48 am
To find the definition of view SYS.ALL_TABLES use the following SQL code
select TEXT
from DBA_VIEWS
where OWNER = 'SYS'
and VIEW_NAME = 'ALL_TABLES'