The great Kian is looking for a smart prime minister. He's looking for a guy who can solve the OLP (Old Legendary Problem). OLP is an old problem (obviously) that no one was able to solve it yet (like P=NP).
But still, you want to be the prime minister really bad. So here's the problem:
Given the sequence a1, a2, ..., an find the three values a1 + a4 + a7 + ..., a2 + a5 + a8 + ... and a3 + a6 + a9 + ... (these summations go on while the indexes are valid).
Example:
Input: n = 5, a = [1,2,3,4,5]
Output: 5 7 3
Approach
C++
#include <bits/stdc++.h>using namespace std;void theGreatKian(long long n, long long a[]){long long arr[n + 1];for (long long i = 1; i <= n; i++)arr[i] = a[i - 1];long long sum1 = 0, sum2 = 0, sum3 = 0;for (long long i = 1; i <= n; i += 3)sum1 += arr[i];for (long long i = 2; i <= n; i += 3)sum2 += arr[i];for (long long i = 3; i <= n; i += 3)sum3 += arr[i];cout << sum1 << " " << sum2 << " " << sum3 << "\n";}int main(){long long n = 5;long long a[] = {1, 2, 3, 4, 5};theGreatKian(n, a);return 0;}
No comments:
Post a Comment