Đưa ra một loạt các số nguyên có độ dài n, chương trình phải tìm giá trị lặp lại với số lần tối đa và in số. Trong trường hợp cà vạt, chọn số nhỏ hơn và in nó.
Điều kiện biên: Độ dài của mảng N sẽ từ 2 đến 100
Length of array N will be from 2 to 100
Định dạng đầu vào: Dòng đầu tiên sẽ chứa mảng các số nguyên có độ dài n được phân tách bằng một hoặc nhiều khoảng trắng.
First line will contain the array of integers of length N separated by one or more spaces.
Định dạng đầu ra: Giá trị số nguyên lặp lại số lần tối đa.
The integer value which repeats the maximum number of times.
Mẫu đầu vào/đầu ra: Ví dụ 1: Đầu vào: 10 20 30 20 30 10 30 20 Nhưng 20 là số nhỏ hơn và do đó 20 được in dưới dạng đầu ra. Ví dụ 2:
Example 1:
Input:
10 20 30 20 30 10 30 20
Output:
20
Explanation:
Both 20 and 30 repeats three times. But 20 is the smaller number and hence 20 is printed as output. Example 2:
Đầu vào: 1 2 3 5 9 2 9 6 9Output: 9 Explanation: 9 Lặp lại ba lần nhiều hơn số lượng lặp lại của bất kỳ số nào khác.
1 2 3 5 9 2 9 6 9
Output:
9
Explanation:
9 repeats thrice which is more than the repetition count of any other number.
n=[int[i] for i in input[].split[]]
print[max[n,key=n.count]]
The maximum repeating number is 35
namespace
88int
7 The maximum repeating number is 371
Đầu ranaive approach is to run two loops, the outer loop picks an element one by one, and the inner loop counts a number of occurrences of the picked element. Finally, return the element with a maximum count. The time complexity of this approach is O[n^2].
A better approach is to create a count array of size k and initialize all elements of count[] as 0. Iterate through all elements of input array, and for every element arr[i], increment
count[arr[i]]. Finally, iterate through count[] and return the index with maximum value. This approach takes O[n] time, but requires O[k] space.
Sau đây là thời gian O [n] và O [1] Phương pháp tiếp cận không gian bổ sung. & Nbsp; Hãy cho chúng tôi hiểu cách tiếp cận với một ví dụ đơn giản trong đó ARR [] = {2, 3, 3, 5, 3, 4, 1, 7} , k = 8, n = 8 [số phần tử trong mảng []].O[n] time and O[1] extra space approach.
Let us understand the approach with a simple example where arr[] = {2, 3, 3, 5, 3, 4, 1, 7}, k = 8, n = 8 [number of elements in arr[]].
- Lặp mặc dù mảng đầu vào ARR [], cho mọi phần tử ARR [i], tăng arr [mảng [i]%k] bởi k [mảng [] trở thành {2, 11, 11, 29, 11, 12, 1, 15} ]
- Tìm giá trị tối đa trong mảng đã sửa đổi [giá trị tối đa là 29]. Chỉ số của giá trị tối đa là phần tử lặp lại tối đa [chỉ mục 29 là 3].
- Nếu chúng tôi muốn lấy lại mảng gốc, chúng tôi có thể lặp lại qua mảng một lần nữa và làm ARR [i] = mảng [i] % k trong đó tôi thay đổi từ 0 đến N-1.
Thuật toán trên hoạt động như thế nào? Vì chúng tôi sử dụng ARR [i]%K làm chỉ mục và thêm giá trị k tại chỉ số ARR [i]%k, chỉ số bằng phần tử lặp lại tối đa sẽ có giá trị tối đa cuối cùng. Lưu ý rằng K được thêm số lần tối đa tại chỉ số bằng phần tử lặp lại tối đa và tất cả các phần tử mảng nhỏ hơn K.following là triển khai C ++ của thuật toán trên. & NBSP;
Following is C++ implementation of the above algorithm.
C++
#include
using
namespace
std;
int
maxRepeating[
int
* arr,
int
The maximum repeating number is 31
int
The maximum repeating number is 33
The maximum repeating number is 34
The maximum repeating number is 35
The maximum repeating number is 36
The maximum repeating number is 37
int
The maximum repeating number is 39
#include
0#include
1
The maximum repeating number is 35
int
#include
4The maximum repeating number is 35
The maximum repeating number is 36
The maximum repeating number is 37
int
#include
9The maximum repeating number is 35
The maximum repeating number is 34
#include
0using
3 using
4
#include
0
The maximum repeating number is 34
using
7using
8
using
7namespace
0
#include
0namespace
2
The maximum repeating number is 35
namespace
2The maximum repeating number is 35
namespace
6 namespace
7namespace
2
int
std;
0
The maximum repeating number is 34
The maximum repeating number is 35
int
std;
4The maximum repeating number is 35
int
std;
7std;
8std;
9std;
8int
1The maximum repeating number is 35
int
int
4The maximum repeating number is 35
int
6int
7 int
8int
9maxRepeating[
0
The maximum repeating number is 35
namespace
6 maxRepeating[
3namespace
2
Java
maxRepeating[
5 maxRepeating[
6
maxRepeating[
7 maxRepeating[
8
The maximum repeating number is 35
int
0 int
maxRepeating[
int
int
4int
The maximum repeating number is 31
int
The maximum repeating number is 33
The maximum repeating number is 35
The maximum repeating number is 34
#include
0
The maximum repeating number is 36
The maximum repeating number is 37
int
* arr,
5* arr,
6* arr,
7using
7* arr,
9
#include
0____6
The maximum repeating number is 3022__96 ____1044 ____96
The maximum repeating number is 306
#include
0
The maximum repeating number is 36
The maximum repeating number is 37
int
* arr,
5The maximum repeating number is 312
The maximum repeating number is 313
#include
0
The maximum repeating number is 34
using
7using
3 using
4
using
7
The maximum repeating number is 34
The maximum repeating number is 321
using
8The maximum repeating number is 321
namespace
0using
7namespace
2
#include
0namespace
2
#include
0namespace
6 namespace
7
The maximum repeating number is 35
namespace
2The maximum repeating number is 35
The maximum repeating number is 335
int
0 The maximum repeating number is 337
The maximum repeating number is 338
The maximum repeating number is 35
The maximum repeating number is 34
#include
0
The maximum repeating number is 36
The maximum repeating number is 37
int
* arr,
5* arr,
6* arr,
7#include
0____6
The maximum repeating number is 3022__96 ____1044 ____96
The maximum repeating number is 306
#include
0
The maximum repeating number is 36
The maximum repeating number is 37
int
* arr,
5The maximum repeating number is 312
The maximum repeating number is 313
using
7using
3 using
4
The maximum repeating number is 372
The maximum repeating number is 373
The maximum repeating number is 35
namespace
2namespace
2
Python3
#include
0namespace
6 namespace
7
The maximum repeating number is 35
The maximum repeating number is 335
int
0 The maximum repeating number is 337
The maximum repeating number is 338
A
#include
0____6
The maximum repeating number is 362
#include
0____6
The maximum repeating number is 365
The maximum repeating number is 366
The maximum repeating number is 306
#include
0
The maximum repeating number is 369
The maximum repeating number is 370
The maximum repeating number is 371
The maximum repeating number is 377
The maximum repeating number is 378
The maximum repeating number is 35
The maximum repeating number is 36
The maximum repeating number is 381
The maximum repeating number is 382
The maximum repeating number is 383
The maximum repeating number is 37
int
____The maximum repeating number is 386
#include
0
The maximum repeating number is 388
The maximum repeating number is 389____190
The maximum repeating number is 371
The maximum repeating number is 392
The maximum repeating number is 393
The maximum repeating number is 35
The maximum repeating number is 395
The maximum repeating number is 392
The maximum repeating number is 397
* arr,
6The maximum repeating number is 399
The maximum repeating number is 35
#include
019192 * arr,
6The maximum repeating number is 35
The maximum repeating number is 36
The maximum repeating number is 381
The maximum repeating number is 382
The maximum repeating number is 383
The maximum repeating number is 37
The maximum repeating number is 312211
#include
0using
3 #include
14
The maximum repeating number is 395
#include
16#include
54
The maximum repeating number is 37
#include
56#include
57C#
using
7
The maximum repeating number is 395
The maximum repeating number is 392
#include
20using
7#include
01
The maximum repeating number is 392
#include
24The maximum repeating number is 35
namespace
6 #include
27#include
28
The maximum repeating number is 392
#include
30The maximum repeating number is 344
The maximum repeating number is 345
The maximum repeating number is 346__
The maximum repeating number is 35
The maximum repeating number is 34
#include
47
The maximum repeating number is 392
#include
49#include
50using
7* arr,
9
#include
51
The maximum repeating number is 392
The maximum repeating number is 366
using
#include
59
maxRepeating[
7 #include
61
using
7using
3 using
4
using
7
The maximum repeating number is 34
The maximum repeating number is 321
using
8The maximum repeating number is 321
namespace
0using
7namespace
2
#include
0namespace
2
#include
0namespace
6 namespace
7
The maximum repeating number is 35
namespace
2The maximum repeating number is 35
The maximum repeating number is 335
int
0 The maximum repeating number is 337
The maximum repeating number is 338
The maximum repeating number is 35
The maximum repeating number is 34
A
#include
0____6
The maximum repeating number is 362
#include
0____6
The maximum repeating number is 365
The maximum repeating number is 366
The maximum repeating number is 306
#include
0using
27using
28
using
29using
30using
31
using
32using
33
The maximum repeating number is 35
namespace
2namespace
2
PHP
using
37
Is
The maximum repeating number is 34
The maximum repeating number is 35
The maximum repeating number is 36
The maximum repeating number is 37
using
50 using
51using
50using
53using
42using
55using
50using
57#include
0using
40#include
30using
40#include
30using
50using
64using
44using
66using
44
The maximum repeating number is 306
#include
0using
70 using
71using
40using
73
#include
0using
75 using
76
The maximum repeating number is 35
The maximum repeating number is 36
The maximum repeating number is 37
using
50 using
81using
50The maximum repeating number is 35
The maximum repeating number is 34
#include
0using
3
The maximum repeating number is 37
using
40#include
30using
50using
96using
70using
45#include
0
The maximum repeating number is 34
using
7using
70 using
71using
40#include
30using
50namespace
07
using
7using
75 using
71using
50
The maximum repeating number is 306
#include
0namespace
2
The maximum repeating number is 35
namespace
2The maximum repeating number is 35
namespace
6 using
75The maximum repeating number is 306
namespace
2
The maximum repeating number is 35
using
40 using
71namespace
25namespace
26The maximum repeating number is 35
using
42 namespace
29using
40namespace
31The maximum repeating number is 35
using
44 namespace
34The maximum repeating number is 35
namespace
36 int
7namespace
38#include
0maxRepeating[
using
40
The maximum repeating number is 345
using
42The maximum repeating number is 345
using
44namespace
31namespace
47
JavaScript
namespace
48
using
38 namespace
50
The maximum repeating number is 34
The maximum repeating number is 35
The maximum repeating number is 36
namespace
54#include
0#include
1
The maximum repeating number is 35
namespace
58The maximum repeating number is 35
The maximum repeating number is 36
namespace
61The maximum repeating number is 35
The maximum repeating number is 34
#include
0using
3 using
4
#include
0
The maximum repeating number is 34
using
7using
8
using
7namespace
0
#include
0namespace
2
The maximum repeating number is 35
namespace
2The maximum repeating number is 35
namespace
6 namespace
7namespace
2
The maximum repeating number is 35
namespace
82The maximum repeating number is 35
namespace
84The maximum repeating number is 35
namespace
86The maximum repeating number is 35
namespace
88int
7 The maximum repeating number is 371
#include
0namespace
92namespace
93namespace
31
namespace
95
Đầu ra
The maximum repeating number is 3
Độ phức tạp về thời gian: O [n] & nbsp; không gian phụ trợ: O [1] O[n]
Auxiliary Space : O[1]
Bài tập: Giải pháp trên chỉ in một phần tử lặp lại và không hoạt động nếu chúng ta muốn in tất cả các phần tử lặp lại tối đa. Ví dụ: nếu mảng đầu vào là {2, 3, 2, 3}, giải pháp trên sẽ chỉ in 3. Điều gì sẽ xảy ra nếu chúng ta cần in cả 2 và 3 khi cả hai đều xảy ra số lần tối đa. Viết thời gian O [n] và O [1] Chức năng không gian bổ sung in tất cả các yếu tố lặp lại tối đa. . The above
solution prints only one repeating element and doesn’t work if we want to print all maximum repeating elements. For example, if the input array is {2, 3, 2, 3}, the above solution will print only 3. What if we need to print both of 2 and 3 as both of them occur maximum number of times. Write a O[n] time and O[1] extra space function that prints all maximum repeating elements. [Hint: We can use maximum quotient arr[i]/n instead of maximum value in step 2].
Note that the above solutions may
cause overflow if adding k repeatedly makes the value more than INT_MAX.