suvrokroy
BAN USERpublic void findLongestSubsequence() {
String str="",max="";
String str1="lkjhgfds",str2="oiuyhgftr";
char [] arr1 = str1.toCharArray();
char [] arr2 = str2.toCharArray();
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if(arr1[i]==arr2[j]){
str="";
int k=0;
while((i+k)<arr1.length && (j+k)<arr2.length){
if(arr1[i+k]!=arr2[j+k])
break;
str=str+arr1[i+k];
k++;
}
if(max.length()<str.length())
max=str;
}
}
}
System.out.println("===="+max);
}
//use two arrays.
//binArr will hold bit pattern
int[] bitArr = {8,4,2,1};
int[] binArr = new int[4];
int num=8;
//create bit pattern
for (int i = 0; i < bitArr.length; i++) {
if(bitArr[i]<=num && num>=0){
num=num-bitArr[i];
binArr[i]=1;
}else{
binArr[i]=0;
//i++;
}
}
//reverse bit
int temp=binArr[0];
binArr[0]=binArr[1];
binArr[1]=temp;
temp=binArr[2];
binArr[2]=binArr[3];
binArr[3]=temp;
int revNum=0;
for (int i = 0; i < binArr.length; i++) {
if(binArr[i]==1)
revNum=revNum+bitArr[i];
}
System.out.println("rev num:"+revNum);
private void deleteEveryNthNode(int i) {
System.out.println("####Deleting every "+i+"th Node......");
LinkedList temp=head;
int count=1;
if(i<=0)
return;
else if(head==null)
return;
else if(i==1){
head=null;
return;
}else{
while(temp!=null){
LinkedList p=temp;
if((count+1)%i==0){
if(p.next==null)
p=null;
else if(p.next.next==null)
p.next=null;
else if(p.next.next!=null)
p.next=p.next.next;
count++;
}
temp=temp.next;
count++;
}
}
}
public void sortList(){
//first = points to the head,last=points to the beginning of second half
LinkedList first=head,last=head;
while(last.next.value>last.value){
last=last.next;
}
LinkedList temp=last;
last=last.next;
//iterate over the second half of the list
LinkedList count =last;
while(count.next!=null){
//if element of second half of the list lies between the first value and its next
//then create a new node and add it in between them
if(count.value<first.next.value){
LinkedList temp1 = new LinkedList(count.value);
temp1.next=first.next;
first.next=temp1;
}else{
//increment first pointer till you get a value greater than the count pointer value
while(first.next.value<count.value)
first=first.next;
}
count=count.next;
}
//this part for the last node
if(count.next==null){
if(count.value<first.next.value){
LinkedList temp1 = new LinkedList(count.value);
temp1.next=first.next;
first.next=temp1;
}else{
while(first.next.value<count.value)
first=first.next;
LinkedList temp1 = new LinkedList(count.value);
temp1.next=first.next;
first.next=temp1;
}
}
temp.next=null;
}
public void sortList(){
//first = points to the head,last=points to the beginning of second half
LinkedList first=head,last=head;
while(last.next.value>last.value){
last=last.next;
}
LinkedList temp=last;
last=last.next;
//iterate over the second half of the list
LinkedList count =last;
while(count.next!=null){
//if element of second half of the list lies between the first value and its next
//then create a new node and add it in between them
if(count.value<first.next.value){
LinkedList temp1 = new LinkedList(count.value);
temp1.next=first.next;
first.next=temp1;
}else{
//increment first pointer till you get a value greater than the count pointer value
while(first.next.value<count.value)
first=first.next;
}
count=count.next;
}
//this part for the last node
if(count.next==null){
if(count.value<first.next.value){
LinkedList temp1 = new LinkedList(count.value);
temp1.next=first.next;
first.next=temp1;
}else{
while(first.next.value<count.value)
first=first.next;
LinkedList temp1 = new LinkedList(count.value);
temp1.next=first.next;
first.next=temp1;
}
}
temp.next=null;
}
initialize an array of size 255.
set index =-1 if char is found
private void removeDuplicate() {
// TODO Auto-generated method stub
int [] temp = new int[255];
String str="iuytrertyui";
String t="";
char [] arr = str.toCharArray();
for (int i = 0; i < str.toCharArray().length; i++) {
if(temp[(int)arr[i]]!=-1){
t+=arr[i];
temp[(int)arr[i]]=-1;
}
}
System.out.println("==="+t);
}
Distance from (0,0) is min if |x| + |y| is min for any cor-ordinates.
I use two stacks for this algo.
1. if main stack is empty insert insert (x,y)
else
2.if |x|+|y|<Main Stack.top(x,y) then pop it and put in second stack.Keep popping unless you get a value less than eq to |x|+|y|
3.Insert x,y, into main stack
4.Pop all elements from second stack and put it back into main stack.
At any time the main stack contains points at min distance for (0,0)
Let me know if it can be improved
simple bubble sort.
public void sortLL(LinkedList head){
LinkedList p = head;
if(head==null)
return;
else{
while(p.next!=null){
LinkedList q = head;
while(q.next!=null){
if(p!=q){
if(p.value<q.value){
int temp = p.value;
p.value=q.value;
q.value=temp;
}
}
q=q.next;
}
p=p.next;
}
}
}
private int getSum(int[]arr){
int sum=0;
for (int i = 0; i < arr.length; i++) {
sum=sum+arr[i];
}
return sum;
}
private void knapsack() {
// given an array of positive integers and that you are allowed to
//change the sign of any of the integers whenever you require.
//write a program to calculate the minimum sum of this array.
int [] arr = new int[]{0,9,8,7,6,5,4,3};
//sort the array
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if(i!=j){
if(arr[i]<arr[j]){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
for (int j = 1; j < arr.length; j++) {
//check total sum considering number as +ve and then -ve
//depending on the min sum consider the sign of the number
int temp = arr[j];
arr[j]=-arr[j]; //negative
int k = getSum(arr);
arr[j]=-arr[j];//positive
int l=getSum(arr);
//compare min sum
if(k<l){
if(k>0)
arr[j]=-temp;
}
else
if(l>0)
arr[j]=temp;
}
//print array
for (int j = 0; j < arr.length; j++) {
System.out.println("="+arr[j]);
}
}
private void removeDuplicates() {
// TODO Auto-generated method stub
String str="oiuytrddd";
boolean flag = false;
char[] temp = str.toCharArray();
char[] temp1 = new char[temp.length];
int k=0;
for (int i = 0; i < temp.length; i++) {
flag=false;
for (int j = 0; j < temp1.length; j++) {
if(temp[i]==temp1[j]){
//System.out.println("iiii==="+temp[i]);
flag=true;
break;
}
}
if(flag==false){
//System.out.println("kkk==="+temp1[k]);
temp1[k]=temp[i];
k++;
}
}
//System.out.println("=========="+tempStr);
for (int i = 0; i < k; i++) {
System.out.print("===="+temp1[i]);
}
}
Use two pointers.Select any node as head node and let the head pointer(h) point it.Use another pointer to traverse the list let it be t.
let value x needs to be inserted.
1.Check if h.value is less than X.If yes then insert x just before head.
2.else traverse linked list unless t.next.value>x or t.next==h.Insert x after t.
int[] arr = {9,2,10,7,6,11,12,3,5,8};
for (int i = 0; i < arr.length; i++) {
int j=0;
while(j<arr.length && j!=i){
int k=0;
while(k<arr.length && k!=i && k!=j){
int l=0;
while(l<arr.length && l!=i && l!=j && l!=k){
if(arr[i]+arr[j]+arr[k]+arr[l]==23)
System.out.println(arr[i]+" , "+arr[j]+" , "+arr[k]+" , "+arr[l]);
l++;
}
k++;
}
j++;
}
}
private void formatString() {
// TODO Auto-generated method stub
String s= "AAAABBCCCCCCCBBCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",temp="";
char[] a = s.toCharArray();
int count=0;
for (int i = 0; i < a.length-1; i++) {
if(a[i]==a[i+1]){
count++;
if(i==a.length-2){
count++;
temp=temp+count+"{"+a[i]+"}";
}
}else{
count++;
temp=temp+count+"{"+a[i]+"}";
count=0;
}
}
System.out.println("=========="+temp);
}
- suvrokroy May 16, 2012private void getBiggestSumSubset(int []arr){
int sum = 0;
int max =0;
for (int i = 0; i < arr.length-1; i++) {
for(int j=i+1;j<arr.length;j++){
sum=0;
for(int k=i;k<j;k++){
sum = sum+arr[k];
}
if(sum>max){
max=sum;
System.out.println("i="+i+"=j="+j);
System.out.println("arr[i]="+arr[i]+"=arr[j]="+arr[j]+"=max="+max);
}
}
}
}
- suvrokroy May 13, 2012private void getBiggestSumSubset(int []arr){
int sum = 0;
int max =0;
for (int i = 0; i < arr.length-1; i++) {
for(int j=i+1;j<arr.length;j++){
sum=0;
for(int k=i;k<j;k++){
sum = sum+arr[k];
}
if(sum>max){
max=sum;
System.out.println("i="+i+"=j="+j);
System.out.println("arr[i]="+arr[i]+"=arr[j]="+arr[j]+"=max="+max);
}
}
}
}
- suvrokroy May 13, 2012private void getSecondHighestNumber(int [] arr) {
// TODO Auto-generated method stub
int []temp = new int[2];
temp[0] = arr[0];
temp[1]=arr[1];
for (int i = 2; i < arr.length; i++) {
if(arr[i]>temp[0] && arr[i]>temp[1])
{
int k = arr[i] - temp[0];
int l = arr[i] - temp[1];
System.out.println("l="+l+"=k="+k);
if(k>l)
temp[0] = arr[i];
if(k<l)
temp[1] = arr[i];
}else if(arr[i]>temp[0] && arr[i]<temp[1]){
temp[1] = arr[i];
}else if(arr[i]<temp[0] && arr[i]>temp[1]){
temp[0] = arr[i];
}
}
System.out.println("=========="+temp[0]+"======"+temp[1]);
}
private void splitNode(LLNode head){
LLNode temp = head;
int count = 0;
//count total nodes
while(temp.next!=head){
count++;
temp = temp.next;
}
temp = head;
int i=0;
while(i!=count/2){
temp=temp.next;
i++;
}
//go to middle and ground the node also mark the next node as
//head of second LL
LLNode newHead = temp.next;
temp.next = null;
temp = newHead;
while(temp.next!=head){
temp=temp.next;
}
//ground the last node of second list
temp.next=null;
}
what about my solution? - " suvrokroy "
- suvrokroy March 04, 2013