N
N
nimbus2142021-10-13 19:20:09
C++ / C#
nimbus214, 2021-10-13 19:20:09

How to reduce used memory?

The program does not pass the memory limit of 1 MB.

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
    int count, maximum = 0, n;
    unsigned int arr_size;
    cin >> arr_size;
    n = arr_size;
    int* arr = new int[arr_size];
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    for (int i = 0; i < n; i++) {
        count = 0;
        for (int j = i + 1; j < n; j++) {
            if (arr[i] == arr[j]) {
                count += 1;
                maximum = max(count, maximum);
            }
        }
    }
    cout << maximum+1 << endl;
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
15432, 2021-10-13
@15432

If I understood the condition correctly, then start a map, count the number of each number in it at the input, while remembering who has the maximum.
You do not need to store all the entered elements, and even more so quadratically over them

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question