public boolean group(int input[],int m){
        Map<Integer,Integer> count = new HashMap<Integer,Integer>();
        for(Integer i : input){
            int c = 1;
            if(count.containsKey(i)){
                c = count.get(i);
                c++;
            }
            count.put(i, c);
        }
        
        PriorityQueue<Pair> maxHeap = new PriorityQueue<Pair>(count.size(),new Comparators());
        for(Integer s : count.keySet()){
            int c = count.get(s);
            //if any count is greater than len/m then this arrangement is not possible
            if(c > Math.ceil(input.length*1.0/m)){
                return false;
            }
            maxHeap.offer(new Pair(s,c));
        }
        int current = 0;
        int start = current;
        while(maxHeap.size() > 0){
            Pair p = maxHeap.poll();
            int i =0;
            while(i < p.count){
                input[start] = p.num;
                start = start + m;
                if(start >= input.length){
                    current++;
                    start = current;
                }
                i++;
            }
        }
        return true;
    }
http://www.careercup.com/question?id=6026101998485504
This answer two questions.
Group elements in size m such that in every group only unique elements are possible.
It also answers question where rearrange array such that same elements are exactly m
distance from each other
src/com/interview/array/GroupElementsInSizeM.java