teleren/devto huggingface dataset:
667. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:59:08

```
class Solution {
 public:
  vector<int> constructArray(int n, int k) {
    vector<int> ans;

    for (int i = 0; i < n - k; ++i)
      ans.push_back(i + 1);

    for (int i = 0; i < k; ++i)
      if (i % 2 == 0)
        ans.push_back(n - i / 2);
      else
        ans.push_back(n - k + (i + 1) / 2);

    return ans;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/beautiful-arrangement-ii/
661. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:58:20

```
class Solution {
 public:
  vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
    const int m = M.size();
    const int n = M[0].size();
    vector<vector<int>> ans(m, vector<int>(n));

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j) {
        int ones = 0;
        int count = 0;
        for (int x = max(0, i - 1); x < min(m, i + 2); ++x)
          for (int y = max(0, j - 1); y < min(n, j + 2); ++y) {
            ones += M[x][y];
            ++count;
          }
        ans[i][j] = ones / count;
      }

    return ans;
  }
};
```


#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/image-smoother/
652. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:57:28

```
class Solution {
 public:
  vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
    vector<TreeNode*> ans;
    unordered_map<string, int> count;
    encode(root, count, ans);
    return ans;
  }

 private:
  string encode(TreeNode* root, unordered_map<string, int>& count,
                vector<TreeNode*>& ans) {
    if (!root)
      return "";

    const string encoded = to_string(root->val) + "#" +
                           encode(root->left, count, ans) + "#" +
                           encode(root->right, count, ans);
    if (++count[encoded] == 2)
      ans.push_back(root);
    return encoded;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/find-duplicate-subtrees/
632. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:56:25

```
struct T {
  int i;
  int j;
  int num;  // nums[i][j]
  T(int i, int j, int num) : i(i), j(j), num(num) {}
};

class Solution {
 public:
  vector<int> smallestRange(vector<vector<int>>& nums) {
    auto compare = [&](const T& a, const T& b) { return a.num > b.num; };
    priority_queue<T, vector<T>, decltype(compare)> minHeap(compare);
    int mini = INT_MAX;
    int maxi = INT_MIN;

    for (int i = 0; i < nums.size(); ++i) {
      const int num = nums[i][0];
      minHeap.emplace(i, 0, num);
      mini = min(mini, num);
      maxi = max(maxi, num);
    }

    int minRange = mini;
    int maxRange = maxi;

    while (minHeap.size() == nums.size()) {
      const auto [i, j, _] = minHeap.top();
      minHeap.pop();
      if (j + 1 < nums[i].size()) {
        minHeap.emplace(i, j + 1, nums[i][j + 1]);
        maxi = max(maxi, nums[i][j + 1]);
        mini = minHeap.top().num;
        if (maxi - mini < maxRange - minRange) {
          minRange = mini;
          maxRange = maxi;
        }
      }
    }

    return {minRange, maxRange};
  }
};
```

#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/
659. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:55:10

```
class Solution {
 public:
  bool isPossible(vector<int>& nums) {
    unordered_map<int, int> count;
    vector<int> starts;  // start index of subsequence
    vector<int> ends;    // end index of subsequence

    for (const int num : nums)
      ++count[num];

    for (int i = 0; i < nums.size(); ++i) {
      if (i > 0 && nums[i] == nums[i - 1])
        continue;
      const int num = nums[i];
      const int currCount = count[num];
      const int prevCount = count.count(num - 1) ? count[num - 1] : 0;
      const int nextCount = count.count(num + 1) ? count[num + 1] : 0;
      for (int j = 0; j < currCount - prevCount; ++j)
        starts.push_back(num);
      for (int j = 0; j < currCount - nextCount; ++j)
        ends.push_back(num);
    }

    for (int i = 0; i < starts.size(); ++i)
      if (ends[i] - starts[i] < 2)
        return false;

    return true;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/split-array-into-consecutive-subsequences/
674. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:54:17

```
class Solution {
 public:
  int findLengthOfLCIS(vector<int>& nums) {
    int ans = 0;

    for (int l = 0, r = 0; r < nums.size(); ++r) {
      if (r > 0 && nums[r] <= nums[r - 1])
        l = r;
      ans = max(ans, r - l + 1);
    }

    return ans;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/longest-continuous-increasing-subsequence/
679. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:53:23

```
class Solution {
 public:
  bool judgePoint24(vector<int>& nums) {
    vector<double> doubleNums;

    for (const int num : nums)
      doubleNums.push_back(num);

    return dfs(doubleNums);
  }

 private:
  bool dfs(vector<double>& nums) {
    if (nums.size() == 1)
      return abs(nums[0] - 24) < 0.001;

    for (int i = 0; i < nums.size(); ++i)
      for (int j = 0; j < i; ++j) {
        for (const double num : generate(nums[i], nums[j])) {
          vector<double> nextRound{num};
          for (int k = 0; k < nums.size(); ++k) {
            if (k == i || k == j)  // used in generate()
              continue;
            nextRound.push_back(nums[k]);
          }
          if (dfs(nextRound))
            return true;
        }
      }

    return false;
  }

  vector<double> generate(double a, double b) {
    return {a * b, a / b, b / a, a + b, a - b, b - a};
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/24-game/
817. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:51:51

```
class Solution {
 public:
  int numComponents(ListNode* head, vector<int>& G) {
    int ans = 0;
    unordered_set<int> setG{begin(G), end(G)};

    for (; head; head = head->next)
      if (setG.count(head->val) &&
          (!head->next || !setG.count(head->next->val)))
        ++ans;

    return ans;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/linked-list-components/
884. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:50:48

```
class Solution {
 public:
  vector<string> uncommonFromSentences(string A, string B) {
    vector<string> ans;
    unordered_map<string, int> count;
    istringstream iss(A + ' ' + B);

    while (iss >> A)
      ++count[A];

    for (const auto& [word, freq] : count)
      if (freq == 1)
        ans.push_back(word);

    return ans;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/uncommon-words-from-two-sentences/
872. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:49:57

```
class Solution {
 public:
  bool leafSimilar(TreeNode* root1, TreeNode* root2) {
    vector<int> leaves1;
    vector<int> leaves2;
    dfs(root1, leaves1);
    dfs(root2, leaves2);
    return leaves1 == leaves2;
  }

  void dfs(TreeNode* node, vector<int>& leaves) {
    if (!node)
      return;
    if (!node->left && !node->right) {
      leaves.push_back(node->val);
      return;
    }

    dfs(node->left, leaves);
    dfs(node->right, leaves);
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/leaf-similar-trees/
Printing Right Triangle Star Pattern in c
Tags: c, beginners, programming, tutorial
Published: 2022-07-14 16:49:08

Code:
=================================================









  
int main()  
{  
    int n;  
    printf("Enter the number of rows");  
    scanf("%d",&n);  
    for(int i=1;i<=n;i++)  
    {  
        for(int j=1;j<=i;j++)  
        {  
            printf("* ");  
        }  
        printf("\n");  
    }  
    return 0;  
}  

=================================================


Output :

=================================================

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2r7t611ikdt03dmijilq.png)

=================================================
858. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:49:07

```
class Solution {
 public:
  int mirrorReflection(int p, int q) {
    int m = 1;  // # of room extension + 1
    int n = 1;  // # of reflection + 1

    while (m * p != n * q)
      m = ++n * q / p;

    if (n % 2 == 0)
      return 2;
    if (m % 2 == 0)
      return 0;
    if (m % 2 == 1)
      return 1;
    throw;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/mirror-reflection/
Using Recursion like a Pro - 1
Tags: algorithms, recursion
Published: 2022-07-14 16:48:21

## Why I write this post ?

In order to be a better programmer,
getting a dream software job,
all developers must face the devil call "Algorithms",
especially something called "**Recursion**" really showed me **how DESPERATION looks like** 😵‍💫😵‍💫😵‍💫.

&nbsp;

After struggling for a while,
I search for "How to use Recursion comfortably",
and finally found a book called [Think like a programmer](https://www.amazon.com/Think-Like-Programmer-Introduction-Creative/dp/1593274246).

![Think Like a programmer book](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/565bocsin2hgi5bc9vn2.jpg)

&nbsp;

This book really **SAVED MY LIFE** !!! 🥳🥳🥳,
and now I can use Recursion pretty comfortably,
even able to solve the medium Tree problem within 10 mins,
which is extremely amazing !!!

&nbsp;

And I want to share you guys such amazing experience,
so you won't suffer from it.


&nbsp;

## Prerequisite of being a Pro

Before becoming a recursion pro, let's review basics of recursion.

&nbsp;

`Recursion` means "calling itself",
and it has 2 vital concepts,
which are `Recurrence relation` and `Base case`, 

&nbsp;

- Recurrence relation : 
```
the relationship between the problem and its direct 
subproblems
```

- Base case :
```
the simplest case, which the function can handle on its own 
without calling itself.
```

( if you're not familiar with these concepts, you can read the  [Martin and Dragon story](https://www.cs.cmu.edu/~15110-f12/Touretzky-Common-Lisp-ch8.pdf) )

&nbsp;

...to be continued


&nbsp;

## References

 - [Think like a programmer](https://www.amazon.com/Think-Like-Programmer-Introduction-Creative/dp/1593274246)
 - [LeetCode Recursion Card](https://leetcode.com/explore/learn/card/recursion-i/)
853. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:48:16

```
struct Car {
  int pos;
  double time;  // time to reach the target
};

class Solution {
 public:
  int carFleet(int target, vector<int>& position, vector<int>& speed) {
    int ans = 0;
    vector<Car> cars(position.size());

    for (int i = 0; i < position.size(); ++i)
      cars[i] = {position[i], (double)(target - position[i]) / speed[i]};

    sort(begin(cars), end(cars),
         [](const auto& a, const auto& b) { return a.pos > b.pos; });

    double maxTime = 0;  // the time of the slowest car to reach the target

    for (const auto& car : cars)
      // a car needs more time to reach the target, so it becomes slowest
      if (car.time > maxTime) {
        maxTime = car.time;
        ++ans;
      }

    return ans;
  }
};
```

#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/car-fleet/
830. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:47:17

```
class Solution {
 public:
  vector<vector<int>> largeGroupPositions(string S) {
    const int n = S.length();
    vector<vector<int>> ans;

    for (int i = 0, j = 0; i < n; i = j) {
      while (j < n && S[j] == S[i])
        ++j;
      if (j - i >= 3)
        ans.push_back({i, j - 1});
    }

    return ans;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/positions-of-large-groups/
824. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:46:28

```
class Solution {
 public:
  string toGoatLatin(string S) {
    string ans;
    const unordered_set<char> vowels{'a', 'e', 'i', 'o', 'u',
                                     'A', 'E', 'I', 'O', 'U'};
    istringstream iss(S);
    string word;
    int i = 1;

    while (iss >> word) {
      if (i > 1)
        ans += ' ';
      if (vowels.count(word[0]))
        ans += word;
      else
        ans += word.substr(1) + word[0];
      ans += "ma" + string(i++, 'a');
    }

    return ans;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/goat-latin/
889. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:44:59

```
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
 public:
  TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
    unordered_map<int, int> postToIndex;

    for (int i = 0; i < post.size(); ++i)
      postToIndex[post[i]] = i;

    return build(pre, 0, pre.size() - 1, post, 0, post.size() - 1, postToIndex);
  }

 private:
  TreeNode* build(const vector<int>& pre, int preStart, int preEnd,
                  const vector<int>& post, int postStart, int postEnd,
                  const unordered_map<int, int>& postToIndex) {
    if (preStart > preEnd)
      return nullptr;
    if (preStart == preEnd)
      return new TreeNode(pre[preStart]);

    const int rootVal = pre[preStart];
    const int leftRootVal = pre[preStart + 1];
    const int leftRootPostIndex = postToIndex.at(leftRootVal);
    const int leftSize = leftRootPostIndex - postStart + 1;

    TreeNode* root = new TreeNode(rootVal);
    root->left = build(pre, preStart + 1, preStart + leftSize, post, postStart,
                       leftRootPostIndex, postToIndex);
    root->right = build(pre, preStart + leftSize + 1, preEnd, post,
                        leftRootPostIndex + 1, postEnd - 1, postToIndex);
    return root;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/
873. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:43:32

```
class Solution {
 public:
  int lenLongestFibSubseq(vector<int>& A) {
    const int n = A.size();
    int ans = 0;
    vector<vector<int>> dp(n, vector<int>(n, 2));
    unordered_map<int, int> numToIndex;

    for (int i = 0; i < n; ++i)
      numToIndex[A[i]] = i;

    for (int j = 0; j < n; ++j)
      for (int k = j + 1; k < n; ++k) {
        const int ai = A[k] - A[j];
        if (ai < A[j] && numToIndex.count(ai)) {
          const int i = numToIndex[ai];
          dp[j][k] = dp[i][j] + 1;
          ans = max(ans, dp[j][k]);
        }
      }

    return ans;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/
856. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:41:06

```
class Solution {
 public:
  int scoreOfParentheses(string S) {
    int ans = 0;
    int layer = 0;

    for (int i = 0; i + 1 < S.length(); ++i) {
      const char a = S[i];
      const char b = S[i + 1];
      if (a == '(' && b == ')')
        ans += 1 << layer;
      layer += a == '(' ? 1 : -1;
    }

    return ans;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/score-of-parentheses/
How to generate PHP enums from database in Laravel?
Tags: php, opensource, tutorial, laravel
Published: 2022-07-14 16:40:58

A magic number is a configuration parameter typed as a number in the middle of some code.

And that magic number is a **bad thing**.

Using this library, you will fix the following code:
{% embed https://gist.github.com/richardDobron/74e873c9caef5ce6b98a59ac8fff72e3 %}

To such:
{% embed https://gist.github.com/richardDobron/aa4207654f63540614a9e0c9ab31e4fb %}


## How to use this generator?
First of all, install composer package:
```
composer require dobron/laravel-db-enum-generator
```

Let's say the _user_roles_ database table has the following structure:

| Id  | Slug             | Role       |
|-----|------------------|------------|
| 1   | MANAGER          | Admin      |
| 2   | CONTENT_CREATOR  | Editor     |
| 3   | MODERATOR        | Moderator  |
| 4   | ADVERTISER       | Advertiser |
| 5   | INSIGHTS_ANALYST | Analyst    |

Now call artisan command `make:enum`.

```
# with laravel model
php artisan make:enum UserRoleTypes --model=UserRole --id=Id --slug=Slug --title=Role

# with laravel model that does not have slug column (will be auto-generated from --title)
php artisan make:enum UserRoleTypes --model=UserRole --id=Id --title=Role

# with database table
php artisan make:enum UserRoleTypes --table=user_roles --id=Id --slug=Slug --title=Role

# etc.
```
The `UserRoleTypes.php` file was generated in the `./app/enums/` directory.
If you update the database table, just use the same command with the _--force_ flag to overwrite the file.
{% embed https://gist.github.com/richardDobron/3df1b1f099bea100f6585c74d76f10c6 %}

## How to find magic numbers in the project?
I recommend using a library called *PHPMND*.

This stands for PHP Magic Number Detector.

![](https://raw.githubusercontent.com/povils/phpmnd/master/demo.gif)

A detailed installation guide can be found at [https://github.com/povils/phpmnd](https://github.com/povils/phpmnd)

But if you're using Composer, here's how you can install it quickly:
```
composer require --dev povils/phpmnd
```

Once that's done, you run it like this:
```
phpmnd .
```
812. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:39:56

```
class Solution {
 public:
  double largestTriangleArea(vector<vector<int>>& points) {
    double ans = 0;

    for (const auto& A : points)
      for (const auto& B : points)
        for (const auto& C : points)
          ans = max(ans, 0.5 * abs((B[0] - A[0]) * (C[1] - A[1]) -
                                   (C[0] - A[0]) * (B[1] - A[1])));

    return ans;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/largest-triangle-area/
842. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:38:33

```
class Solution {
 public:
  vector<int> splitIntoFibonacci(string num) {
    vector<int> ans;
    dfs(num, 0, ans);
    return ans;
  }

 private:
  bool dfs(const string& num, int s, vector<int>& ans) {
    if (s == num.length() && ans.size() >= 3)
      return true;

    for (int i = s; i < num.length(); ++i) {
      if (num[s] == '0' && i > s)
        break;
      const long val = stol(num.substr(s, i + 1 - s));
      if (val > INT_MAX)
        break;
      if (ans.size() >= 2 &&
          val > ans[ans.size() - 2] + static_cast<long>(ans.back()))
        break;
      if (ans.size() <= 1 ||
          val == ans[ans.size() - 2] + static_cast<long>(ans.back())) {
        ans.push_back(val);
        if (dfs(num, i + 1, ans))
          return true;
        ans.pop_back();
      }
    }

    return false;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/split-array-into-fibonacci-sequence/
869. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:34:29

```
class Solution {
 public:
  bool reorderedPowerOf2(int N) {
    int count = counter(N);

    for (int i = 0; i < 30; ++i)
      if (counter(1 << i) == count)
        return true;

    return false;
  }

 private:
  int counter(int n) {
    int count = 0;

    for (; n > 0; n /= 10)
      count += pow(10, n % 10);

    return count;
  }
};
```

#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/reordered-power-of-2/
880. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:31:56

```
class Solution {
 public:
  string decodeAtIndex(string s, int k) {
    long size = 0;  // length of decoded `s`

    for (const char c : s)
      if (isdigit(c))
        size *= c - '0';
      else
        ++size;

    for (int i = s.length() - 1; i >= 0; --i) {
      k %= size;
      if (k == 0 && isalpha(s[i]))
        return string(1, s[i]);
      if (isdigit(s[i]))
        size /= s[i] - '0';
      else
        --size;
    }

    throw;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/decoded-string-at-index/
885. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:30:39

```
class Solution {
 public:
  vector<vector<int>> spiralMatrixIII(int R, int C, int r0, int c0) {
    vector<vector<int>> ans{{r0, c0}};
    vector<int> dx{1, 0, -1, 0};
    vector<int> dy{0, 1, 0, -1};

    for (int i = 0; ans.size() < R * C; ++i)
      for (int step = 0; step < i / 2 + 1; ++step) {
        r0 += dy[i % 4];
        c0 += dx[i % 4];
        if (0 <= r0 && r0 < R && 0 <= c0 && c0 < C)
          ans.push_back({r0, c0});
      }

    return ans;
  }
};
```

#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/spiral-matrix-iii/
Paracetamol.js💊| #167: Explica este código JavaScript
Tags: javascript, webdev, spanish, programming
Published: 2022-07-14 16:22:22

## **<center>Explica este código JavaScript</center>**
#### <center>**Dificultad:** <mark>Básico</mark></center>

```js
const manzana = {
  color: "rojo",
  esMadura: true,
}

const copia = { ...manzana };

delete copia.color;

console.log(manzana);
console.log(copia);
```

A. `{ color: "rojo", esMadura: true }`, `{ esMadura: true }`
B. `{ color: "rojo", esMadura: true }`, `{ color: "rojo", esMadura: true }`
C. `{ esMadura: true }`, `{ esMadura: true }`
D. `Ninguna de las anteriores`

Respuesta en el primer comentario.

---
Write the program to show the tringle ?
Tags: bigner, star, tringle
Published: 2022-07-14 16:21:43

```
let space = '';
let n = '';
let m = '';
for(let i=1;i<=6;i++){
    for(let j=6;j>=i;j--){
      space = space + ' - ';
    }
    for(let z=1;z<=i;z++){
        n = n + ` ${z} `
    }
    for(let k=i-1;k>=1;k--){
        m = m + ` ${k} `
    }
    console.log(space+n+m)
    n = '';
    space = '';
    m = '';
}
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/neeg7no93hvn7yizr9ng.png)
6 reasons why you should use linux for Programming
Published: 2022-07-14 16:21:04

**Is Linux necessary for programming?**
In short no, but it is very popular among programmers for various reasons.But there are some distinct advantages of Linux over other OSs.

Some advantages of Linux over competitor operating systems are as follows: 

1.. Linux tends to come with most of the compilers and interpreters you'll need to get the job done. Other operating systems, like Windows, usually do not.

2.. Linux is versatile, powerful, secure, and fast.

3.. Linux has a massive community to help you out if you get stuck for any reason.

4.. The operating system also comes with a handy, built-in package manager.

5.. Error messages on other operating systems, like Windows or macOS, for example, tend to be less than helpful. On Linux, you can usually find the solution from someone else who has solved it. If not, just ask the community support staff.

6.. Another great thing about Linux is that you can often automate many repetitive tasks using simple lines of code.
888. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-14 16:18:07

```
class Solution {
 public:
  vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
    int diff =
        (accumulate(begin(A), end(A), 0) - accumulate(begin(B), end(B), 0)) / 2;
    unordered_set<int> set{begin(B), end(B)};

    for (int a : A)
      if (set.count(a - diff))
        return {a, a - diff};

    throw;
  }
};
```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/fair-candy-swap/