Tuesday, March 22, 2011

Solution to find Middle Element in a Lengthy Linked list

To find the middle element in a very long list of elements N can be done by having two pointers tempFirst,tempSecond.The pointers tempFirst,tempSecond initially point to head or the first element .ompFirst starts moving one node at a time whereas tempSecond starts moving twice the number of nodes that tempFirst  visited.For example if tempFirst is currently pointing to second node tempSecond points to fourth node and if tempFirst points to thirdnode then tempSecond points to sixth.By having the pointers move in this way exactly when half of the list is reached by tempFirst,tempSecond would have reached the end of list and thereby tempFirst points to middle element in a long linked list of data.

LinkList
package com.datastructure;

public class LinkList {
   
    private Link first;
    public LinkList()
    {
        first=null;
    }
    public boolean isEmpty()
    {
        return (first==null);
    }
    public void insertElement(int data,int id)
    {
       
        Link newLink=new Link(data,id);
       
        newLink.next=first;
        first=newLink;
       
       
    }
   
    public Link findElement(int data)
    {
        Link temp=first;
        if(temp.getiData()==data)
        {
            return first;
        }
        while(temp.next!=null)
        {
            temp=temp.next;
            if(temp.getiData()==data)
            {
                return temp;
            }
        }
        return null;
    }
   
    public Link findMiddleElement()
    {
        Link tempFirst=first;
        Link tempSecond=first;
        int skipCnt=0;
       
        while(tempFirst.next!=null)
        {
            tempFirst=tempFirst.next;
            skipCnt++;
           
            for(int j=1;j<=(skipCnt*2);j++)
            {
                tempSecond=tempSecond.next;
                if(tempSecond.next==null)
                {
                   
                    break;
                }
            }
           
            if(tempSecond.next==null)
            {
                break;
            }
           
           
        }
       
        return tempFirst;
    }
   
    public static void main(String[] args)
    {
        LinkList linkList=new LinkList();
        linkList.insertElement(10, 1);
        linkList.insertElement(30, 2);
        linkList.insertElement(40, 3);
        linkList.insertElement(50, 4);
        linkList.insertElement(60, 5);
        linkList.insertElement(70, 6);
        Link middleElement=linkList.findMiddleElement();
       
        System.out.println("Middle Element"+middleElement.getiData());
       
       
    }
   

}




Link

package com.datastructure;

public class Link {
   
    public int iData;
    public int id;
    public Link next;
   
    public int getiData() {
        return iData;
    }

    public void setiData(int iData) {
        this.iData = iData;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public Link(int data,int id)
    {
        iData=data;
        id=id;
    }
   
    public void displayLink()
    {
        System.out.println("Linked List node value is"+iData);
    }

}




Monday, March 14, 2011

Solution to Find Maximum Subsequence

import java.util.Iterator;
import java.util.TreeMap;


public class MaximumContiguousSum {
   
    private TreeMap tm=null;
   
    private int[] inputArr={-1,-2,-3,6,-4,7,8};
   
    public static void main(String[] args)
    {
        MaximumContiguousSum maxSum=new MaximumContiguousSum();
        maxSum.populateSum(new TreeMap<Integer,String>());
    }

    private void populateSum(TreeMap<Integer, String> treeMap) {
        // TODO Auto-generated method stub
        tm=treeMap;
       
        for(int i=0;i<inputArr.length;i++)
        {
            int sum=0;
            StringBuffer combinations=new StringBuffer();
            combinations.append(inputArr[i]);
            for(int j=i+1;j<inputArr.length;j++)
            {
                combinations.append(inputArr[j]);
                sum=sum+inputArr[j];
                tm.put(sum,combinations);
               
            }
        }
       
        Iterator sumIter=tm.descendingKeySet().iterator();
       
        while(sumIter.hasNext())
        {
            int key=(Integer)sumIter.next();
           
            System.out.println("The Maximum key is =="+key+"===Contiguous Sequence is==="+tm.get(key));
            break;
        }
       
    }
   
   

}