---
# System prepended metadata

title: '[LeeteCode - Weekly Contest 235](https://leetcode.com/contest/weekly-contest-235)'
tags: [LeetCode Contest]

---

---
titile: Weekly Contest 235
tags: LeetCode Contest
---

[LeeteCode - Weekly Contest 235](https://leetcode.com/contest/weekly-contest-235)
===

Q1 - 1816. Truncate Sentence
---

```cpp=
class Solution {
public:
    string truncateSentence(string s, int k) {
        for(int i = 1; i < s.size(); i++){
            if(s[i] == ' '){
                if(--k == 0)
                    return s.substr(0, i);
            }
        }
        
        return s;
    }
};
```

Q2 - 1817. Finding the Users Active Minutes
---

```cpp=
class Solution {
public:
    vector<int> findingUsersActiveMinutes(vector<vector<int>>& logs, int k) {
        
        vector<int> ret(k);
        unordered_map<int, unordered_set<int>> uams;
        for(auto& log: logs){
            uams[log[0]].insert(log[1]);
        }
        
        for(auto [user, uam]: uams){
            ret[uam.size()-1]++;
        }
        
        
        return ret;
    }
};
```

Q3 - 1818. Minimum Absolute Sum Difference
---

```cpp=
class Solution {
public:
    int minAbsoluteSumDiff(vector<int>& nums1, vector<int>& nums2) {
        
        set<int> set(nums1.begin(), nums1.end());
        
        long int sum = 0;
        for(int i = 0; i < nums1.size(); i++){
            sum += abs(nums1[i] - nums2[i]);
        }
        
        long int ret = sum;
        for(int i = 0; i < nums1.size(); i++){
            int dif = abs(nums1[i] - nums2[i]);
            
            auto it = set.lower_bound(nums2[i]);
            if(it != set.end()){
                ret = min(ret, sum - dif + abs(*it - nums2[i]));
            }
            
            if(it != set.end()){
                it--;
                ret = min(ret, sum - dif + abs(*it - nums2[i]));
            }
        }
        
        const long int MOD = 1e9 + 7;
        return ret % MOD;
    }
};
```

Q4 - 1819. Number of Different Subsequences GCDs
---

```cpp=
int gcd(int a, int b){
    
    while(a){
        int tmp = a;
        a = b % a;
        b = tmp;
    }
    
    
    return b;
}

class Solution {
public:
    int countDifferentSubsequenceGCDs(vector<int>& nums) {
        
        const int n = 2e5 + 1;
        vector<bool> hasNums(n);
        for(int num: nums){
            hasNums[num] = true;
        }
        
        
        int ret = 0;
        for(int i = 1; i < n; i++){
            int g = 0;
            for(int j = i; j < n; j += i){
                if(hasNums[j])
                    g = gcd(g, j);
            }
            
            ret += i == g;
        }
        
        
        return ret;
    }
};
```
