Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/3600-3699/3658.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Tags
level-easy
math, number-theory

Problem Description
3658. GCD of Odd and Even Sums

You are given an integer n. Your task is to compute the GCD (greatest common divisor) of two values:
sumOdd: the sum of the smallest n positive odd numbers.
sumEven: the sum of the smallest n positive even numbers.
Return the GCD of sumOdd and sumEven.

Example 1:
Input: n = 4
Output: 4
Explanation:
Sum of the first 4 odd numbers sumOdd = 1 + 3 + 5 + 7 = 16
Sum of the first 4 even numbers sumEven = 2 + 4 + 6 + 8 = 20
Hence, GCD(sumOdd, sumEven) = GCD(16, 20) = 4.

Example 2:
Input: n = 5
Output: 5
Explanation:
Sum of the first 5 odd numbers sumOdd = 1 + 3 + 5 + 7 + 9 = 25
Sum of the first 5 even numbers sumEven = 2 + 4 + 6 + 8 + 10 = 30
Hence, GCD(sumOdd, sumEven) = GCD(25, 30) = 5.

Constraints:
1 <= n <= 10​​​​​​​00
*/

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <limits>
#include <iomanip>
#include <functional>
#include <cstring>
#include <climits>
#include "../core/core.h"
using namespace std;

class Solution
{
public:
int gcdOfOddEvenSums(int n)
{
int oddSum = (n * (2 * 1 + ((n - 1) * 2))) / 2;
int evenSum = (n * (2 * 2 + ((n - 1) * 2))) / 2;

return gcd(oddSum, evenSum);
}
};

int main()
{
int n;
cin >> n;
Solution sol;
int result = sol.gcdOfOddEvenSums(n);
cout << result << "\n";

return 0;
}
11 changes: 11 additions & 0 deletions tests/3600-3699/3658.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Test Case 1:
Input:
4
Output:
4

Test Case 2:
Input:
5
Output:
5
Loading