Sum of Unique Elements

🏠 ⬅️ ➡️

You are given an integer array nums. The unique elements of an array are the elements that appear exactly once in the array.

Return the sum of all the unique elements of nums.

Example 1:

Input: nums = [1,2,3,2] Output: 4 Explanation: The unique elements are [1,3], and the sum is 4.

Example 2:

Input: nums = [1,1,1,1,1] Output: 0 Explanation: There are no unique elements, and the sum is 0.

Example 3:

Input: nums = [1,2,3,4,5] Output: 15 Explanation: The unique elements are [1,2,3,4,5], and the sum is 15.

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: nums(5)

    nums = [1, 2, 3, 2, 1]
    print '(I0)', sum_unique_elements(nums)

    nums = [1, 1, 1, 1, 1]
    print '(I0)', sum_unique_elements(nums)

    nums = [1, 2, 3, 4, 5]
    print '(I0)', sum_unique_elements(nums)

contains

    function sum_unique_elements(nums) result(sum)
        implicit none
        integer, intent(in) :: nums(:)
        integer :: sum, i, count

        sum = 0
        do i = 1, size(nums)
            count = 0
            do j = 1, size(nums)
                if (nums(i) == nums(j)) then
                    count = count + 1
                end if
            end do
            if (count == 1) then
                sum = sum + nums(i)
            end if
        end do
    end function sum_unique_elements

end program main
Compiled
Executed
Correct
module SumUniqueElements
    implicit none
    private
    public :: sumUniqueElements

contains

    function sumUniqueElements(nums) result(sum)
        integer, intent(in) :: nums(:)
        integer :: sum
        integer :: i, j, count
        logical :: found

        sum = 0
        do i = 1, size(nums)
            found = .false.
            do j = 1, i - 1
                if (nums(i) == nums(j)) then
                    found = .true.
                    exit
                end if
            end do
            if (.not. found) then
                count = 1
                do j = i + 1, size(nums)
                    if (nums(i) == nums(j)) then
                        count = count + 1
                    end if
                end do
                if (count == 1) then
                    sum = sum + nums(i)
                end if
            end if
        end do
    end function sumUniqueElements
end module SumUniqueElements

program test_sumUniqueElements
    use SumUniqueElements
    implicit none
    integer, parameter :: nums1(4) = [1, 2, 3, 2]
    integer, parameter :: nums2(5) = [1, 1, 1, 1, 1]
    integer, parameter :: nums3(5) = [1, 2, 3, 4, 5]
    integer :: sum

    sum = sumUniqueElements(nums1)
    write (*,*) "Sum of unique elements in nums1:", sum
    sum = sumUniqueElements(nums2)
    write (*,*) "Sum of unique elements in nums2:", sum
    sum = sumUniqueElements(nums3)
    write (*,*) "Sum of unique elements in nums3:", sum
end program test_sumUniqueElements
🌐 Data from online sources
def bestTeamScore(scores, ages):
    players = sorted(zip(ages, scores))
    n = len(players)
    dp = [0] * n
    best_score = 0

    for i in range(n):
        dp[i] = players[i][1]
        for j in range(i):
            if players[i][1] >= players[j][1]:
                dp[i] = max(dp[i], dp[j] + players[i][1])
        best_score = max(best_score, dp[i])

    return best_score
1. First, we create a new array/list (`players`) of pairs containing the players' ages and scores.
  1. Next, we sort the players array/list by age, followed by sorting by scores within the same age.
  2. We create a dp array/list of length n (number of players) initializing it with 0.
  3. Then, we iterate through each player in the sorted players array/list: a. Set dp[i] to the current player's score. b. Iterate through all previous players (0 to i-1) and check if the current player's score is greater or equal to the previous player's score. If true, set dp[i] to the maximum value between existing dp[i] and the sum of that previous player's DP value plus the current player's score. c. Update the best_score with the maximum of its current value and dp[i] for each player.
  4. Lastly, return the best_score which represents the highest overall score of all possible basketball teams without conflicts.
🌐 Data from online sources
#include <vector>
#include <algorithm>

using namespace std;

int bestTeamScore(vector<int>& scores, vector<int>& ages) {
    int n = scores.size();
    vector<pair<int, int>> players(n);

    for (int i = 0; i < n; ++i) {
        players[i] = {ages[i], scores[i]};
    }

    sort(players.begin(), players.end());

    vector<int> dp(n);
    int best_score = 0;

    for (int i = 0; i < n; ++i) {
        dp[i] = players[i].second;
        for (int j = 0; j < i; ++j) {
            if (players[i].second >= players[j].second) {
                dp[i] = max(dp[i], dp[j] + players[i].second);
            }
        }
        best_score = max(best_score, dp[i]);
    }

    return best_score;
}
1. First, we create a new array/list (`players`) of pairs containing the players' ages and scores.
  1. Next, we sort the players array/list by age, followed by sorting by scores within the same age.
  2. We create a dp array/list of length n (number of players) initializing it with 0.
  3. Then, we iterate through each player in the sorted players array/list: a. Set dp[i] to the current player's score. b. Iterate through all previous players (0 to i-1) and check if the current player's score is greater or equal to the previous player's score. If true, set dp[i] to the maximum value between existing dp[i] and the sum of that previous player's DP value plus the current player's score. c. Update the best_score with the maximum of its current value and dp[i] for each player.
  4. Lastly, return the best_score which represents the highest overall score of all possible basketball teams without conflicts.