Pages

Showing posts with label Algorithm. Show all posts
Showing posts with label Algorithm. Show all posts

Saturday, March 21, 2015

Electing Prime Cabinet Members of BAAP party!!!

Problem Statement -
Electing Prime Cabinet Members of BAAP party!!!
BAAP   party president Mr. Panna decided to choose the Prime Cabinet Members for his party from each state of the country Youngistaan.
The criterion decided by Mr. Panna is that from each state having a population of N, N votes will be casted.  If a candidate gets more than N/2 votes, he/ she will be elected as Prime Cabinet Member of BAAP party.
Given that population of each state be N, where N should be maximum 10, 00000 for each state. Assume that each individual will cast the vote.
Help Mr. Panna design an automated system to get the Prime Cabinet Members from each state, if any. If a member contesting for the cabinet member post from a state gets more than half (> N/2) of the total population votes, he/ she will be appointed as Prime Cabinet Member of BAAP party else the post will be vacant.
Write an algorithm to help Mr. Panna appoint the cabinet member for the BAAP party.
Each of the contesting members will be allotted a positive integer number.
Given the population of each state N, write a method int primeMember (int P []); which should return the prime member of array P, where P will be the population array of a given state of size N.  The method must return −1, if array P does not have a prime member.
Input Specification:
A population array P, of size N (a state population) in random order where each array index denotes a member’s vote of the given state for his/her candidate denoted by a positive integer (candidate number).
Output Specification:
Single integer value if the prime member exists, else -1(minus one).
Complexity:
Expected worst-case time complexity is O (N).
Expected worst-case space complexity is O (1).




Example 1:
For example, given population array P having candidate 2, 3, 4, 6 contesting from a state having the total population of N=12, such that each person from the state casted his/ her vote for one of the above candidate as below:
{4, 3, 3, 4, 2, 4, 3, 3, 6, 3, 3, 4}
The method should return −1, because the candidate 3 got the maximum vote 6 out of all the candidates contesting and 6 votes is not more than half of total population 12.
Example 2:
For example, given population array P having candidate 2, 3, 7, 10 contesting from a state having the total population of N=7, such that each person from the state casted his/ her vote for one of the above candidate as below:
{7, 3, 3, 10, 3, 3, 2}

The method should return 3, because the candidate 3 got the maximum vote 4 out of all the candidates contesting and 4 votes is more than half of total population 7.

Tuesday, June 10, 2014

Sort array containing only 0, 1 & 2 in Complexity O(n)

Java Implementation -
public class SortArrayContaining012Only {

    public static void main(String[] args) {
        int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 };
        int arr_size = arr.length;
        sort012(arr, arr_size);
        System.out.println("Sorting done");
    }

    private static void sort012(int a[], int arr_size) {
        int lo = 0;
        int hi = arr_size - 1;
        int mid = 0;

        while (mid <= hi) {
            switch (a[mid]) {
            // swap lo and mid
            case 0:
                int temp = a[mid];
                a[mid] = a[lo];
                a[lo] = temp;
                lo++;
                mid++;
                break;
            case 1:
                mid++;
                break;
            // swap mid and high
            case 2:

                temp = a[mid];
                a[mid] = a[hi];
                a[hi] = temp;
                hi--;
                break;
            }
        }
    }
}

Sunday, May 25, 2014

Find the next higher number with same digits

Problem :  Given a number, write an algorithm to find the next higher number with the same digits.

For example : If the input is 45156, then the next higher number with the same digit is 45516.

Solution :

Algorithm -
1. Scan the digits of the given number from right to left starting from tenth digit (current index).
2. At each iteration we check,
    if the digit at the right is greater than the current index, then
   {we stop - Follow step 3 }
    else
   {continue left}
3. The current index value is the pivot element.
4. Find the smallest digit (x) higher than the pivot element.
5. Swap the pivot element with x.
6. Now the pivot digit is x.
7. Sort all the digits to the right of pivot digit in increasing  order.

Java Implementation -
import java.util.Arrays;

public class NextHigherNoWithSameDigits {

    public static void main(String[] args) {
        int input[] ={4,5,1,5,6};
        System.out.println("Input No. ======>" + intArrToString(input));
        int[] output = higherNo(input);
        System.out.println("Output No. ======>" + intArrToString(output));
    }

    private static int[] higherNo(int[] arr) {
        for (int i = arr.length - 1; i >= 0; i--) {
            // scan the no. from right starting at 10's place, if the element at
            // the right is greater then left is the pivot element
            if (arr[i] > arr[i - 1]) {
                // get the pivot element index
                int j = i;
                // sort the element to the right of the pivot element
                Arrays.sort(arr, i, arr.length);
                while (true) {
                    if (arr[i - 1] < arr[j]) {
                        // swap the pivot element with the smallest digit higher
                        // than the pivot element on the right
                        swap(arr, i, j);
                        break;
                    }
                    j++;
                }
                break;
            }
        }
        return arr;
    }

    /**
     * Helper method to swap two elements of the array
     * 
     */
    private static void swap(int arr[], int i, int j) {
        int temp = arr[i - 1];
        arr[i - 1] = arr[j];
        arr[j] = temp;

    }

    /**
     * Helper method to print the input/ output as numbers
     * 
     */
    private static StringBuilder intArrToString(int a[]) {
        StringBuilder input = new StringBuilder();
        for (int i = 0; i < a.length; i++) {
            input = input.append(a[i]);
        }
        return input;
    }
}