Posts

Showing posts from June, 2018

insert, delete and search operations on list

package linkedlist; public class List {             Node head = null;         public  void insert(int data){         Node new_node = new Node(data);                 new_node.next = head;         head = new_node;                 return;             }         public void insertAtEnd(int data){                 Node new_node = new Node(data);                 if(head == null){             head = new_node;             return;         }                 Node last = head;                 while(last.next != null){             last = last.next;         }                 last.next = new_node;             }         public void insetAfter(Node prev_node, int data){         Node new_node = new Node(data);                 new_node.next = prev_node.next;                 prev_node.next = new_node;                 return;             }         public void delete(int data){                 Node temp = head;         Node prev = null;                 // if head holds data since head don't have previous

Remove duplicates recursively

Eliminate the character and it's pair when they are same. Repeat it until there is no adjacent duplicates in the string Sample input:   aabcddceffgge Sample output: bccee b Code: import java.util.*; public class Program3 {        public static void removeDuplicates(StringBuilder s){                int len = s.length();         int i=0;                while(i<len-1){             if(s.charAt(i) == s.charAt(i+1)){                 s.delete(i, i+2);                 len-=2;             }             else{                 i++;             }         }                System.out.println(s);                if( isDuplicatesPresent(s))             removeDuplicates(s);            }        public static boolean isDuplicatesPresent(StringBuilder s){                boolean present = false;                HashSet<Character> set = new HashSet<Character>();                int len = s.length();                for(int i=0; i<len; i++){             if(!set.add(s.charA

Rearrange the shuffled trip plan

Rearrange the shuffled sticky notes(trip plan-each note consists of starting place and place for night stay for that particular day) Sample Input: Vellore Bangalore Mysore Goa Bangalore Mysore Chennai Vellore Sample output: Chennai Vellore Vellore Bangalore Bangalore Mysore Mysore Goa   Steps: First we have to find the starting pair. Chennai is the starting place because no pair ends with chennai. We will find that in the first step. Having the first pair as Chennai to vellore, we can find the next pair by checking pair containing vellore as starting place. So next we have vellore, Bangalore. Similarly we search for bangalore and then for mysore. Code: public class Program2 {         public static void main(String[] args){                 String[][] places = {{"Vellore","Bangalore"},{"Mysore", "Goa"},{"Bangalore","Mysore"},{"Chennai","Vellore"}};               

Upadate array elements with next greatest element on its right

Update the array elements by the next greatest number in the given array…Since there is no successive element for last array element print “-1”.. Sample Input:{4,5,16,10,7,5,3} Sample output:{16,16,10,7,5,3,-1} Code: import java.util.Arrays; public class Program1 {         public static void main (String[] args){         int a[] = {4,5,16,10,7,5,3};         for(int i=0; i<a.length-1; i++){                 int max = a[i+1];                 for(int j=i+2; j<a.length; j++){             if(a[j]> max){                 max = a[j];             }                             }         a[i] = max;             }          a[a.length-1] = -1;          System.out.print(Arrays.toString(a));         } } Output: [16, 16, 10, 7, 5, 3, -1]