Saturday, September 7, 2013
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)
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
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.
Subscribe to:
Comments (Atom)