Output in order all the numbers with the highest frequency in the integer Array

Source: Internet
Author: User

The first construction started after the year of the snake. I will discuss a simple Amazon pen exam this time. I will first release my own solution. You are welcome to reply to better algorithms. I will update them to my post. Learn together!

The question is from GeeksForGeeks. It was the memoir of a ox who successfully applied for Amazon after 10 rounds of written tests and interviews and gave back to the public. There are several interesting questions. Let's take a look. It may have been a long time ago, but it is not Amazon's invention.

The general idea of this question is as follows:
Find maximum frequent numbers in an array. If there are more than one numbers with maximum frequency, they display all numbers in ascending order. Ascending order is important.

It looks really simple. If the number size is limited, simply open a new array a. Each element I of a represents the number of times the number I appears, and traverse the integer array to update. What if the number is very big? After thinking for a long time, I only wanted to come up with the following two half-hanging algorithms:

Algorithm 1:
1. Fast sorting of Arrays
2. traverse the array and record the number of occurrences of each number to the structure array frequency. By the way, record the maximum number of occurrences. Frequency each element records [integer, frequency].
3. Traverse frequency in step 1 and output the maximum number of times
Time complexity O (nlgn), space complexity O (n)

Algorithm 2:
1. Open a new array frequency to record the number of occurrences
2. Insert and sort the array. If you want to insert an array in the search step, this number is recorded as 1. If the number is equal, it is not inserted, instead, add 1 to the number of equal occurrence; insert and update the frequency array to ensure that the elements correspond one to one with the elements in the sorted subarray.
3. traverse the frequency array to find the maximum number of times
4. traverse the frequency array again to output the maximum number of times
Time complexity O (n ^ n), space complexity O (n)

The following describes the implementation of algorithm 2.

View Code

/* * most-frequent-number.c * * by Wang Guibao * * Amazon tech interview written test problem. * http://www.geeksforgeeks.org/amazon-interview-set-21/ * * Problem: * Find maximum frequent numbers in an array. If there are more numbers with * maximum frequency, they display all numbers in ascending order. Ascending * order is important. */#include <stdio.h>#define MAX_ELE 32int elements[MAX_ELE + 1];int frequency[MAX_ELE + 1];/* * Find most frequently appeared number in an array. If there're more than one * such numbers, output them in ascending order * @param elements: integer array, integers are elements[1...n] * @param n       : number of integers in array */void most_frequent_number(int *elements, int n){    int sorted_begin;    int sorted_end;    int begin;    int end;    int middle;    int i;    int j;    int max_freq = 0;    sorted_begin = sorted_end = 1;    frequency[sorted_begin] = 1;    /* Insert sort the array */    for (i = 2; i <= n; i++) {        begin = sorted_begin;        end = sorted_end;        while (begin <= end) {            middle = (begin + end) / 2;            if (elements[middle] > elements[i]) {                end = middle - 1;            }            else if (elements[middle] < elements[i]) {                begin = middle + 1;            }            else if (elements[middle] == elements[i]) {                frequency[middle]++;                break;            }        }        if (end < begin) {            for (j = sorted_end; j >= begin; j--) {                elements[j + 1] = elements[j];                frequency[j + 1] = frequency[j];            }            elements[begin] = elements[i];            frequency[begin] = 1;            sorted_end++;        }    }    /* Traverse the sorted subarray */    for (j = 1; j <= sorted_end; j++) {        if (max_freq < frequency[j]) {            max_freq = frequency[j];        }    }    /* Output the integer(s) with maximum frequency */    for (j = 1; j <= sorted_end; j++) {        if (frequency[j] == max_freq) {            printf(" %d", elements[j]);        }    }    printf("\n");    return;}int main(){    int i = 1;    int n;    printf("Number of integers to process: ");    scanf("%d", &n);    printf("Input %d integers: ", n);    for (i = 1; i <= n; i++) {        scanf("%d", &elements[i]);    }    most_frequent_number(elements, n);    return 0;}

I wonder whether the time and space complexity of this question can be further reduced? You are welcome to discuss it together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.