Bond
BAN USERpublic class LLToTree{
public static void main(String args[]){
DLL ll = new DLL();
ll.add(new Node(18));
ll.add(new Node(15));
ll.add(new Node(14));
ll.add(new Node(12));
ll.add(new Node(9));
ll.add(new Node(8));
ll.add(new Node(4));
ll.add(new Node(2));
//System.out.println(ll.getEnd().getVal());
ll.traverseTree(ll.change(ll.getRoot().getNext(),ll.getEnd()));
}
}
class DLL{
Node root = new Node(-1);
Node end;
public void setRoot(Node root){
this.root.setNext(root);
}
public Node getRoot(){
return root;
}
public void setEnd(Node n){
this.end = n;
}
public Node getEnd(){
return end;
}
public void add(Node n){
if(getRoot().getNext()== null){
setRoot(n);
setEnd(n);
}else{
n.setNext(getRoot().getNext());
getRoot().getNext().setPrev(n);
n.setPrev(getRoot());
getRoot().setNext(n);
}
}
public void traverse(){
Node temp = root.getNext();
while(temp!=null){
System.out.println(temp.getVal());
temp = temp.getNext();
}
}
public Node change(Node start,Node end){
System.out.println("start is "+start.getVal()+" and end is "+end.getVal());
if(start == end)
return start;
if(start.getNext() ==end){
end.setNext(null);
start.setNext(null);
start.setPrev(null);
return end;
}
if(end.getVal()< start.getVal())
return null;
Node s,d;
s = d = start;
while(d.getNext()!=null){
s = s.getNext();
if(d.getNext().getNext() != null)
d = d.getNext().getNext();
else
d = d.getNext();
}
s.getPrev().setNext(null);
s.getNext().setPrev(null);
//if((start != s.getPrev())
s.setPrev(change(start,s.getPrev()));
//if(change(s.getNext() != end))
s.setNext(change(s.getNext(),end));
return s;
}
public void traverseTree(Node root){
System.out.println(root.getVal());
if(root.getPrev()!=null){
System.out.println("left is "+root.getPrev().getVal());
}else{
System.out.println("left is null");
}
if(root.getNext()!=null){
System.out.println("right is "+root.getNext().getVal());
}else{
System.out.println("right is null");
}
if(root.getPrev()!=null)
traverseTree(root.getPrev());
if(root.getNext()!=null)
traverseTree(root.getNext());
}
}
class Node{
int val;
Node prev,next;
public Node(int val){
this.val = val;
}
public void setNext(Node next){
this.next = next;
}
public Node getNext(){
return next;
}
public void setPrev(Node prev){
this.prev = prev;
}
public Node getPrev(){
return prev;
}
public int getVal(){
return val;
}
}
The code below has complexity O(n)
Main idea is to first get the sum of the entire array.Then traverse the entire array and and each element = sum + Not(element at that position +1)
public class IntArray{
public static void main(String args[]){
int arr[] ={5,7,2,9,3,1,6};
getArr(arr);
}
public static void getArr(int []arr){
int sum = 0;
for(int i =0;i<arr.length;i++)
sum += arr[i];
for(int i =0;i<arr.length;i++)
arr[i] = sum + ~arr[i] +1;
for(int i =0;i<arr.length;i++)
System.out.println(arr[i]);
}
}
Not exactly one function but it is working
public class WordGame{
public static void main(String args[]){
char [][]mat = {{'r','a','z','f'},{'m','p','r','g'},{'k','a','t','a'},{'w','z','a','k'}};
String s = "prrg";
boolean isIt = false;
for(int i =0;i<4;i++){
for(int j =0;j<4;j++){
if(s.charAt(0) == mat[i][j]){
int visited[][] = new int[4][4];
visited[i][j] = 1;
if(check(mat,visited,s,mat[i][j]+"",i,j))
isIt = true;
}
}
}
System.out.println(isIt);
}
public static boolean check(char[][]mat,int[][] visited,String toCheck,String formed,int i ,int j){
int left,up,right,down;
left = j-1>=0 ? j-1:j;
right =j+1<4?j+1:j;
up = i-1>=0?i-1:i;
down =i+1 <4?i+1:i;
for(int k=left;k<=right;k++){
for(int l = up;l<=down;l++){
if(visited[k][l] ==0){
String n = formed+mat[k][l];
//System.out.println("new string is "+n);
if(n.equals(toCheck))
return true;
if(toCheck.contains(n)){
visited[k][l] =1;
if(check(mat,visited,toCheck,n,k,l))
return true;
}
}
}
}
return false;
}
}
- Bond January 18, 2015public static void inplace_reverse(String s,int l){
if(l<= 1 || s.trim().indexOf(" ")<=0){
System.out.print(s);
return;
}else{
int i =0,j=l-1;
while(s.charAt(i) != ' ' && i<s.length()-1)
i++;
while(s.charAt(j) != ' ' && j>=0)
j--;
System.out.print(s.substring(j+1,l)+" ");
if(i<j)
reverse(s.substring(i+1,j),s.substring(i+1,j).length());
System.out.print(" "+s.substring(0,i));
}
}
}
- Bond February 01, 2015