Monday, August 5, 2013

Making Larger number from Given Array of Integer.

Ques: You have given an array of integers, using that form largest Number Possible.
Algorithm:

Source Code:(In java)
package com.array.exercise;

import java.util.Arrays;
import java.util.Comparator;

class MyCompare implements Comparator{

 public int compare(Integer i1,Integer i2){  
  String x =Integer.toString(i1);
  String y =Integer.toString(i2);
  
  String xy= x.concat(y);
  String yx= y.concat(x);
  return Integer.valueOf(xy)-Integer.valueOf(yx) > 0 ? 0:1;
 
 }
 
}
public class MakeBiggerNumber { 
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
 
 
   Integer[] input = new Integer[]{54,546,548,60,77};
 
    MyCompare mc = new MyCompare();
    Arrays.sort(input,mc);
 
     for (int i = 0; i < input.length; i++) {
  System.out.print(input[i]);
 }
 }

}

Sunday, February 17, 2013

Printing Array of 0,1s in a fibonacci sequence



Thursday, March 29, 2012

Print Character Repetition In SameArray


Q. Program for any given string a   aaaabbbbcc, convert the given string in to a4b4c2 without using extra memory. ( Note that every character appears more than once in input string and the repeated characters are contiguous)

Source Code:
import java.util.Scanner;

public class PrintArraywithRepetation {

    /**
     * @param args
     * @author chandan
     * Given String aaaaabbbccccaaad print as a5b3c4a3d1 (Inplace without using Extra array)
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner s = new Scanner(System.in);
       
        String input = s.next();

         int count=1;
        
        for(int i=0 ;i < input.length();i++){
  
            if(i < input.length()-1){
                if(input.charAt(i)!=input.charAt(i+1)){
                    System.out.print(input.charAt(i));
                    System.out.print(count);
                    count=1;
                }
                else
                    count++;
               
            }
            else if(i==input.length())
            {
                if(input.charAt(i)!=input.charAt(i-1)){
                    System.out.print(input.charAt(i));
                    System.out.print(count);
                    count=1;
                    }
                    else
                        count++;
               
            }
            else {
               
                System.out.print(input.charAt(i));
                 System.out.print(count);
                 count++;
            }
           
        }
   
    }

}
Note : Suggestion  are always welcome for better way to implement.



Anagram Checking


Anagram : WAP to Check 2nd String is anagram of Given String or Not.(Amazon FAQ)
Solution:

import java.util.HashSet;
import java.util.Scanner;

public class AnagramChecking {

 /**
  * @param args
  * @author mchandan
  * Program to check  String is Anagram or not i.e string2 is anagram of string1.
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner s = new Scanner(System.in);
  System.out.print("Enter The Original String :");
  String str1 =s.next();
  System.out.print("Enter String to be matched as Anagram:");
  String str2 =s.next();
 
  HashSet h = new HashSet();
  for(int i=0;i < str1.length();i++)
  {
   h.add(str1.charAt(i));
  }
        /
  
  for(int i=0;i < str2.length();i++){< p="">
 
   if(!h.contains(str2.charAt(i)))
   {
    System.out.println("Not Anagram");
    System.exit(0);
   }
  
   }
  
           if(str2.length()==h.size())
            System.out.println("Anagram");
           else
            System.out.println("Not Anagram");
 }
 
 
}