naveenhooda2004
BAN USERhow do you do with ArrayList in o(n+m) ? Its not about key value pairs its about time complexity.
- naveenhooda2004 February 22, 2013Just curious ? Is it a homework Problem ?
- naveenhooda2004 February 22, 2013public class ModifiedBinarySearch {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
int array[]={5,6,7,8,9,1,2,3};
//
int left=0;
int right=array.length-1;
int mid=(left+right)/2;
int element=1;
while(left<=right){
mid=(left+right)/2;
System.out.println("Mid is "+mid);
if(array[mid]==element){
System.out.println("Element found at "+mid);
break;
}
else if(element<array[mid]){
if(element<array[left]){
left=mid+1;
}
else{
right=mid-1;
}
}
else if(element>array[mid]){
if(element>array[right]){
right=mid-1;
}
else{
left=mid+1;
}
}
}
}catch(Exception E){
}
}
}
For which testcase ?
- naveenhooda2004 February 20, 2013public class CompressString {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//AAACCCBBD to A3C3B2D
String str="ABC";
StringBuffer sb=new StringBuffer();
int charcounter=1;
char previouschar=str.charAt(0);
if(str.length()==1){
sb.append(previouschar);
}
if(str.length()>1){
for(int icounter=1;icounter<str.length();icounter++){
if(str.charAt(icounter)==previouschar){
charcounter++;
}
else{
sb.append(previouschar);
if(charcounter!=1){
sb.append(charcounter);
}
charcounter=1;
previouschar=str.charAt(icounter);
}
}
sb.append(previouschar);
if(charcounter!=1){
sb.append(charcounter);
}
}
System.out.println(sb);
}
}
import java.util.Stack;
class Cell{
int x;
int y;
int value;
boolean visited;
Cell(int x, int y, boolean visited, int value){
this.x=x;
this.y=y;
this.visited=visited;
this.value=value;
}
}
public class Shapes {
/**
* @param args
*/
static Cell cell[][]=null;
static void processCell(int icounter,int jcounter){
Stack<Cell> stack=new Stack<Cell>();
stack.push(cell[icounter][jcounter]);
while(!stack.isEmpty()){
Cell getcell=stack.pop();
getcell.visited=true;
System.out.println("Visited "+getcell.x+":"+getcell.y);
if((getcell.x+1)<=(cell.length-1)){
if(cell[getcell.x+1][getcell.y].value==1){
stack.push(cell[getcell.x+1][getcell.y]);
}
}
if((getcell.y+1)<=(cell.length-1)){
if(cell[getcell.x][getcell.y+1].value==1){
stack.push(cell[getcell.x][getcell.y+1]);
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int array[][] = { {1, 0, 1, 0, 1},
{1, 1, 0, 1, 0},
{1, 1, 0, 1, 0},
{1, 0, 1, 0, 0}};
cell=new Cell[array.length][array[0].length];
for(int icounter=0;icounter<array.length;icounter++){
for(int jcounter=0;jcounter<array[0].length;jcounter++){
cell[icounter][jcounter]=new Cell(icounter,jcounter,false, array[icounter][jcounter]);
}
}
int shapeCounter = 0;
for(int icounter=0;icounter<array.length;icounter++){
for(int jcounter=0;jcounter<array[0].length;jcounter++){
System.out.println(icounter+" # "+jcounter);
if(!cell[icounter][jcounter].visited){
System.out.println(icounter+","+jcounter);
if(cell[icounter][jcounter].value==1){
processCell(icounter,jcounter);
shapeCounter++;
}
}
}
}
System.out.println("Shape Counter: "+shapeCounter);
}
}
Sorry, my test case put might be wrong but its works fine!!! Have you tested my code..?
- naveenhooda2004 February 22, 2013