You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|[Leetcode-2179](https://leetcode.com/problems/count-good-triplets-in-an-array/)| Count Good Triplets In An Array |[c++](./leetcode/2179.count-good-triplets-in-an-array.cpp), [python3](./leetcode/2179.count-good-triplets-in-an-array.py)| Binary Search | O\(NlogN\)| O\(N\)| - |
455
+
|[Leetcode-2824](https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/)| Count Pairs Whose Sum Is Less Than Target |[c++](./leetcode/2824.count-pairs-whose-sum-is-less-than-target.cpp), [python3](./leetcode/2824.count-pairs-whose-sum-is-less-than-target.py)| Binary Search | O\(NlogN\)| O\(1\)| - |
455
456
|[Leetcode-2302](https://leetcode.com/problems/count-subarrays-with-score-less-than-k/)| Count Subarrays With Score Less Than K |[c++](./leetcode/2302.count-subarrays-with-score-less-than-k.cpp), [python3](./leetcode/2302.count-subarrays-with-score-less-than-k.py)| Binary Search | O\(N\)| O\(1\)| - |
456
457
|[Leetcode-3261](https://leetcode.com/problems/count-substrings-that-satisfy-k-constraint-ii/)| Count Substrings That Satisfy K Constraint II |[c++](./leetcode/3261.count-substrings-that-satisfy-k-constraint-ii.cpp), [python3](./leetcode/3261.count-substrings-that-satisfy-k-constraint-ii.py)| Binary Search | O\(N \+ Q\)| O\(N\)| - |
457
458
|[Leetcode-2563](https://leetcode.com/problems/count-the-number-of-fair-pairs/)| Count The Number Of Fair Pairs |[c++](./leetcode/2563.count-the-number-of-fair-pairs.cpp), [python3](./leetcode/2563.count-the-number-of-fair-pairs.py)| Binary Search | O\(NlogN\)| O\(1\)| - |
|[Leetcode-2460](https://leetcode.com/problems/apply-operations-to-an-array/)| Apply Operations To An Array |[c++](./leetcode/2460.apply-operations-to-an-array.cpp), [python3](./leetcode/2460.apply-operations-to-an-array.py)| Two Pointers | O\(N\)| O\(1\)| - |
857
858
|[Leetcode-455](https://leetcode.com/problems/assign-cookies/)| Assign Cookies |[c++](./leetcode/455.assign-cookies.cpp), [python3](./leetcode/455.assign-cookies.py)| Two Pointers | O\(NlogN\)| O\(1\)| - |
|[Leetcode-2824](https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/)| Count Pairs Whose Sum Is Less Than Target |[c++](./leetcode/2824.count-pairs-whose-sum-is-less-than-target.cpp), [python3](./leetcode/2824.count-pairs-whose-sum-is-less-than-target.py)| Two Pointers | O\(NlogN\)| O\(1\)| - |
859
861
|[Leetcode-2563](https://leetcode.com/problems/count-the-number-of-fair-pairs/)| Count The Number Of Fair Pairs |[c++](./leetcode/2563.count-the-number-of-fair-pairs.cpp), [python3](./leetcode/2563.count-the-number-of-fair-pairs.py)| Two Pointers | O\(NlogN\)| O\(1\)| - |
860
862
|[Leetcode-295](https://leetcode.com/problems/find-median-from-data-stream/)| Find Median From Data Stream |[c++](./leetcode/295.find-median-from-data-stream.cpp), [python3](./leetcode/295.find-median-from-data-stream.py)| Two Pointers | O\(logN\)| O\(N\)| - |
861
863
|[Leetcode-287](https://leetcode.com/problems/find-the-duplicate-number/)| Find The Duplicate Number |[c++](./leetcode/287.find-the-duplicate-number.cpp), [python3](./leetcode/287.find-the-duplicate-number.py)| Two Pointers | O\(N\)| O\(1\)| - |
|[Leetcode-148](https://leetcode.com/problems/sort-list/)| Sort List |[c++](./leetcode/148.sort-list.cpp), [python3](./leetcode/148.sort-list.py)| Two Pointers | O\(NlogN\)| O\(logN\)| - |
885
887
|[Leetcode-977](https://leetcode.com/problems/squares-of-a-sorted-array/)| Squares Of A Sorted Array |[c++](./leetcode/977.squares-of-a-sorted-array.cpp), [python3](./leetcode/977.squares-of-a-sorted-array.py)| Two Pointers | O\(N\)| O\(1\)| - |
886
888
|[Leetcode-633](https://leetcode.com/problems/sum-of-square-numbers/)| Sum Of Square Numbers |[c++](./leetcode/633.sum-of-square-numbers.cpp), [python3](./leetcode/633.sum-of-square-numbers.py)| Two Pointers | O\(N\)| O\(1\)| - |
889
+
|[Leetcode-1471](https://leetcode.com/problems/the-k-strongest-values-in-an-array/)| The K Strongest Values In An Array |[c++](./leetcode/1471.the-k-strongest-values-in-an-array.cpp), [python3](./leetcode/1471.the-k-strongest-values-in-an-array.py)| Two Pointers | O\(NlogN\)| O\(1\)| - |
887
890
|[Leetcode-42](https://leetcode.com/problems/trapping-rain-water/)| Trapping Rain Water |[c++](./leetcode/42.trapping-rain-water.cpp), [python3](./leetcode/42.trapping-rain-water.py)| Two Pointers | O\(N\)| O\(N\)| - |
888
891
|[Leetcode-167](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)| Two Sum Ii Input Array Is Sorted |[c++](./leetcode/167.two-sum-ii-input-array-is-sorted.cpp), [python3](./leetcode/167.two-sum-ii-input-array-is-sorted.py)| Two Pointers | O\(N\)| O\(1\)| - |
889
892
|[Leetcode-653](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)| Two Sum Iv Input Is A Bst |[c++](./leetcode/653.two-sum-iv-input-is-a-bst.cpp), [python3](./leetcode/653.two-sum-iv-input-is-a-bst.py)| Two Pointers | O\(NH\)| O\(H\)| - |
|[Leetcode-3169](https://leetcode.com/problems/count-days-without-meetings/)| Count Days Without Meetings |[c++](./leetcode/3169.count-days-without-meetings.cpp), [python3](./leetcode/3169.count-days-without-meetings.py)| Sorting | O\(NlogN\)| O\(N\)| - |
1012
1015
|[Leetcode-2179](https://leetcode.com/problems/count-good-triplets-in-an-array/)| Count Good Triplets In An Array |[c++](./leetcode/2179.count-good-triplets-in-an-array.cpp), [python3](./leetcode/2179.count-good-triplets-in-an-array.py)| Sorting | O\(NlogN\)| O\(N\)| - |
1016
+
|[Leetcode-2824](https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/)| Count Pairs Whose Sum Is Less Than Target |[c++](./leetcode/2824.count-pairs-whose-sum-is-less-than-target.cpp), [python3](./leetcode/2824.count-pairs-whose-sum-is-less-than-target.py)| Sorting | O\(NlogN\)| O\(1\)| - |
1013
1017
|[Leetcode-2563](https://leetcode.com/problems/count-the-number-of-fair-pairs/)| Count The Number Of Fair Pairs |[c++](./leetcode/2563.count-the-number-of-fair-pairs.cpp), [python3](./leetcode/2563.count-the-number-of-fair-pairs.py)| Sorting | O\(NlogN\)| O\(1\)| - |
|[Leetcode-977](https://leetcode.com/problems/squares-of-a-sorted-array/)| Squares Of A Sorted Array |[c++](./leetcode/977.squares-of-a-sorted-array.cpp), [python3](./leetcode/977.squares-of-a-sorted-array.py)| Sorting | O\(N\)| O\(1\)| - |
1069
+
|[Leetcode-1471](https://leetcode.com/problems/the-k-strongest-values-in-an-array/)| The K Strongest Values In An Array |[c++](./leetcode/1471.the-k-strongest-values-in-an-array.cpp), [python3](./leetcode/1471.the-k-strongest-values-in-an-array.py)| Sorting | O\(NlogN\)| O\(1\)| - |
// Given an array of integers arr and an integer k.
8
+
// A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the centre of the array.
9
+
// If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j].
10
+
// Return a list of the strongest k values in the array. return the answer in any arbitrary order.
11
+
// The centre is the middle value in an ordered integer list. More formally, if the length of the list is n, the centre is the element in position ((n - 1) / 2) in the sorted list (0-indexed).
12
+
//
13
+
// For arr = [6, -3, 7, 2, 11], n = 5 and the centre is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the centre is arr[m] where m = ((5 - 1) / 2) = 2. The centre is 6.
14
+
// For arr = [-7, 22, 17, 3], n = 4 and the centre is obtained by sorting the array arr = [-7, 3, 17, 22] and the centre is arr[m] where m = ((4 - 1) / 2) = 1. The centre is 3.
15
+
//
16
+
//
17
+
//
18
+
//
19
+
//
20
+
//
21
+
//
22
+
//
23
+
//
24
+
//
25
+
//
26
+
//
27
+
//
28
+
//
29
+
//
30
+
//
31
+
// Example 1:
32
+
//
33
+
// Input: arr = [1,2,3,4,5], k = 2
34
+
// Output: [5,1]
35
+
// Explanation: Centre is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer.
36
+
// Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1.
37
+
//
38
+
// Example 2:
39
+
//
40
+
// Input: arr = [1,1,3,5,5], k = 2
41
+
// Output: [5,5]
42
+
// Explanation: Centre is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5].
43
+
//
44
+
// Example 3:
45
+
//
46
+
// Input: arr = [6,7,11,7,6,8], k = 5
47
+
// Output: [11,8,6,6,7]
48
+
// Explanation: Centre is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7].
49
+
// Any permutation of [11,8,6,6,7] is accepted.
50
+
//
51
+
//
52
+
// Constraints:
53
+
//
54
+
// 1 <= arr.length <= 105
55
+
// -105 <= arr[i] <= 105
56
+
// 1 <= k <= arr.length
57
+
//
58
+
//
59
+
60
+
classSolution {
61
+
public:
62
+
vector<int> getStrongest(vector<int>& arr, int k) {
# Given an array of integers arr and an integer k.
8
+
# A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the centre of the array.
9
+
# If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j].
10
+
# Return a list of the strongest k values in the array. return the answer in any arbitrary order.
11
+
# The centre is the middle value in an ordered integer list. More formally, if the length of the list is n, the centre is the element in position ((n - 1) / 2) in the sorted list (0-indexed).
12
+
#
13
+
# For arr = [6, -3, 7, 2, 11], n = 5 and the centre is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the centre is arr[m] where m = ((5 - 1) / 2) = 2. The centre is 6.
14
+
# For arr = [-7, 22, 17, 3], n = 4 and the centre is obtained by sorting the array arr = [-7, 3, 17, 22] and the centre is arr[m] where m = ((4 - 1) / 2) = 1. The centre is 3.
15
+
#
16
+
#
17
+
#
18
+
#
19
+
#
20
+
#
21
+
#
22
+
#
23
+
#
24
+
#
25
+
#
26
+
#
27
+
#
28
+
#
29
+
#
30
+
#
31
+
# Example 1:
32
+
#
33
+
# Input: arr = [1,2,3,4,5], k = 2
34
+
# Output: [5,1]
35
+
# Explanation: Centre is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer.
36
+
# Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1.
37
+
#
38
+
# Example 2:
39
+
#
40
+
# Input: arr = [1,1,3,5,5], k = 2
41
+
# Output: [5,5]
42
+
# Explanation: Centre is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5].
43
+
#
44
+
# Example 3:
45
+
#
46
+
# Input: arr = [6,7,11,7,6,8], k = 5
47
+
# Output: [11,8,6,6,7]
48
+
# Explanation: Centre is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7].
0 commit comments