Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, 25 August 2011

Date with Design Patterns and Java

This looked promising enough to keep it in my To Do list. have to read it in coming days :)

http://www.devdaily.com/java/java-design-patterns-in-java-examples-tutorials

Given a 2D array, print it in spiral form


/**
*
*/

package com.test.p4;

/**
*
@author Thejas
*
*Print a given matrix in spiral form
Given a 2D array, print it in spiral form. See the following examples.

Input:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

Input:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
Output:
1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11
*/

public class print2DSpiral {

/**
*
@param args
*/

int row=3,column=6;
int rowThreshold = row;//number of rows to traverse to print a fixed column
int columnThreshold = column; //number of columns to traverse to print elements of a fixed row
//int array[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
int array[][]={{1,2,3,4,5,6},{7,8,9,10,11,12},{13,14,15,16,17,18}};
/*
* once right reduce rowthreshold by 1
* once down reduce columnthreshold by 1
* once left reduce rowthreshold by 1
* once up reduce columnthreshold by 1
*/

public void printLeft(int i,int j,int value)
{
for(int k=0;k<columnThreshold;k++)
{
if(value > 0)
{
System.out.print(array[i][j]+" ");
value--;j--;
}
}
rowThreshold--;
if(value>0)
{
printUp(i-1, ++j, value);
}
return;
}
public void printRight(int i,int j,int value)
{
for(int k=0;k<columnThreshold;k++)
{
if(value > 0)
{
System.out.print(array[i][j]+" ");
value--;j++;
}
}
rowThreshold--;
if(value>0)
{
printDown(i+1, --j, value);
}
return;
}
public void printUp(int i,int j,int value)
{
for(int k=0;k<rowThreshold;k++)
{
if(value > 0)
{
System.out.print(array[i][j]+" ");
value--;i--;
}
}
columnThreshold--;
if(value>0)
{
printRight(++i, j+1, value);
}
return;
}
public void printDown(int i,int j,int value)
{
for(int k=0;k<rowThreshold;k++)
{
if(value > 0)
{
System.out.print(array[i][j]+" ");
value--;i++;
}
}
columnThreshold--;
if(value>0)
{
printLeft(--i, j-1, value);
}
return;
}
public static void main(String[] args) {
// TODO Auto-generated method stub

print2DSpiral printIt = new print2DSpiral();
printIt.printRight(0,0,printIt.row*printIt.column);
return;
}

}

Wednesday, 24 August 2011

String Puzzle


package com.test.p3;

/**
*
@author light
*
* Suppose we are given a string 000011112222.
* Make it 012012012012 in O(n) time and O(1) space.
*/

public class FindPattern {

public static void main(String args[])
{
String str="000011112222";
for(int i=0;i<str.length();i++)
{
System.out.print(i%(3));
}
}
}

Find the repeating and the missing in 1..n array of size n


/**
*
*/

package com.test.p2;

/**
*
@author Thejas
*Find the repeating and the missing
*Given an unsorted array of size n. Array elements are in range from 1 to n.
*One number from set {1, 2, …n} is missing and one number occurs twice in array.
*Find these two numbers.
*
* Examples:
*arr[] = {3, 1, 3}
*Output: 2, 3 // 2 is missing and 3 occurs twice
*arr[] = {4, 3, 6, 2, 1, 1}
*Output: 1, 5 // 5 is missing and 1 occurs twice
*
******************************************************************************************
*Solution:
*Traverse array from 0th index, take the value at the current index in the array as the index and
*negate the value at this new index.While Traversing check if the value of the value of current index is -ve,
*if yes we have got the Repeated element at array of current index.
*
*Now traverse the same array and break when you find a positive value in the array for current index.
*current index+1 is ur missing element
******************************************************************************************/

public class FindRepeatingMissing {

/**
*
@param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub

int array[]={5,3,7,2,4,1,5};
int n=array.length;
int i=0,missing=0,repeat=0;
for(;i<n;i++)
{
if(array[(Math.abs(array[i]))-1] > 0)
{
array[(Math.abs(array[i]))-1] = array[(Math.abs(array[i]))-1]*(-1);
}else{
repeat=Math.abs(array[i]);
}
}
i=0;
for(;i<n;i++)
{
if(array[i]> 0)
{
missing=i+1;
}
}

System.out.println("The Missing ="+missing+" and Repeated ="+repeat+" !!");
return;
}

}

Programming Puzzles -Arrays




package com.test.progs;

/**
*
@author Thejas
*
*
* Given an unsorted array arr[] and two numbers x and y,
* find the minimum distance between x and y in arr[].
* The array might also contain duplicates.
* You may assume that both x and y are different and present in arr[].

Examples:
Input: arr[] = {1, 2}, x = 1, y = 2
Output: Minimum distance between 1 and 2 is 1.

Input: arr[] = {3, 4, 5}, x = 3, y = 5
Output: Minimum distance between 3 and 5 is 2.

Input: arr[] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}, x = 3, y = 6
Output: Minimum distance between 3 and 6 is 4.

Input: arr[] = {2, 5, 3, 5, 4, 4, 2, 3}, x = 3, y = 2
Output: Minimum distance between 3 and 2 is 1.

*/

public class MinDist2num {

/**
*
@param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
//test Data
int array[] = {1,4,22,55,2,3,6,7,0,22,3,55,22,0,2,4,5,10,55,2,6,7};
int x=6,y=7;
int i=0,prev=-1,dist=10000;//set a high value for initial distance Dist

for(i=0;i<array.length;i++)
{
if(array[i]==x){
prev=i;
}else if(array[i]==y && prev!=-1){
dist=(i-prev)<dist?(i-prev):dist;
int temp=x;
x=y;
y=temp;
prev=i;
}else if(prev==-1 && array[i]==y)
{
int temp=x;
x=y;
y=temp;
prev=i;
}
}
System.out.println("the minimum distance between "+x+" "+y+" = "+dist);
return;
}

}
/****************************************************************************************
Solution:
Travrse array from 0th index note down the first occurance of either of the elements X or Y in a variable say PREV.
when first occurance of either is found swap X and Y such that found element is X and the other Y.
traverse array further if element at index is X update PREV with index else
if it is Y update variable DIST with distance of PREV and index if lesser than current value of DIST
and swap X and Y set PREV value to that of the index
******************************************************************************************/

Thursday, 5 May 2011

If Java does not have pointers, why is there a "NullpointerException"?

Its a common statement we keep hearing that Java doesn't have pointers. Well its actually untrue, Java has pointers but is never referred to has pointers and we cannot manipulate them.


A simple example can explain how pointers are used in Java.


Object a = new Object(), b=a;


In the above statement a,b are pointers to a single object in memory. Operations performed on a or b effects the same object.


Java has a set of Primitive types for whom this behavior  doesn't apply. Consider


int i1 =10, i2= i1;
System.out.println(" i1 ="+i1+"i2="+i2) ;
i2=6;
System.out.println(" i1 ="+i1+"i2="+i2) ;

The primitive variable i2 initially holds the copy of the value held by variable i1. Changing value of i1 or i2 will not effect the values of each other. For a Java VM  i1 and i2 are two different 32 bit memory locations. One exception to this is arrays of primitive types. Behavior of arrays are similar to what I'll be explain in the below example


Now consider:


public class Value{int val1;}
Value v1=new Value(10), v2=v1;
System.out.println("v1 ="+v1.val1+"v2="+v2.val1) ;
v2.val1=100;
System.out.println("v1 ="+v1.val1+"v2="+v2.val1) ;


v2 has a copy of the address pointed by v1, hence v1 and v2 point to the same object. Changes made to object of class Value using v1 can also be accessed using v2.

Handling Multiple Return values in JAVA (C++ aswell :-)

After lot of Goggling and reading the best solution i could find for this problem was at http://javatuples.com/. The tuples implementation provides us utilities for handling scenarios where we would need to return multiple return types from a Java function (Basically its a kind of Wrapper implementation for the return objects). A similar implementation can be used in C++ by developing a template class to hold multiple types of data and return that class object, but C++ gives us total flexibility to use pointers hence we can achieve it even with out an additional Class implementation.

I'm still a newbie so people pour in your suggestions if there are better ways to achieve the same.