Protected: IMIMobile telephonic

This content is password protected. To view it please enter your password below:

Advertisements

if input as 121213131414 i need output as 111111223344 how can u write code?

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AscendingOrderNumbers {
    public static void main(String[] args) {
        ascendingOrder("121213131414");
    }

    private static void ascendingOrder(String numbers){
        List<Character> list = new ArrayList<>();
        for(char c:numbers.toCharArray())
            list.add(c);
        Collections.sort(list);
        StringBuilder builder = new StringBuilder();
        for (char c:list){
            builder.append(c);
        }
        System.out.println(builder.toString());
    }
}

Method Hiding In Java

Method hiding can be achived by overriding static metthod.

public class MethodHiding extends SuperMethod{
    static void foo(){
        System.out.println("I am sub class method");
    }

    public static void main(String[] args) {
        MethodHiding.foo();
        SuperMethod.foo();
        SuperMethod superMethod = new SuperMethod();
        superMethod.foo();
        SuperMethod superSub = new MethodHiding();
        superSub.foo();
        MethodHiding methodHiding = new MethodHiding();
        methodHiding.foo();
        SuperMethod superNull = null;
        superNull.foo();
        MethodHiding subNull = null;
        subNull.foo();
    }

}

class SuperMethod {
    static void foo(){
        System.out.println("I am super class method");
    }
}

In above code as 2 foo methods(from sub class and super class) are static methods. So we can call those method with class name as well as with references of it. Even reference are null still we didn’t get nullpointer exception.

Binary Search

Binary Search uses Divide and Conquer algorithm to search given element in a sorted array.

Procedure:

  1. Divide array into half
  2. Compare given value with middle element, if matches return index
  3. If the value is greater then the middle element then values is in the left array then repeat the process with the left array.
  4. If the value is less than the middle element then the value is in the right array then repeat the process with the right array.

Time complexity for binary search is O(logn).

Example : https://github.com/santhoshgudla/DSTraining