"Find out the number of pairs in the list you've made, who sum up to an even number!"
"Who are you? And what do you mean? Can you elaborate a bit?" Exclaims Fatal Eagle.
"I meant, what I said... take a look at the sample explanation of the problem I want you to solve, you'll understand perhaps. And as for who I am? I'm Arjit and like you, I just want to save this city, too."
Fatal Eagle is quick to respond to Arjit's task and decides to solve it quickly. He takes out the list of integers that contain the powers of weird creatures and starts finding out the number of pairs that sum up to an even number. Here's how he does it:
Let's say that he had a list of 4 numbers: [1, 2, 3, 4] His answer would be 2. How? 1+3 = 4 and 2 + 4 - that is to say, a number will NOT be added to itself and 1+3 is the same as 3+1.
Example:
Input: n = 4, a = [1,2,3,4]
Output: 2
Approach
C++
#include <bits/stdc++.h>using namespace std;int theSavior(int n, int arr[]){int cnt = 0;for (int i = 0; i < n - 1; i++){for (int j = i + 1; j < n; j++){if (arr[i] != arr[j]){if (arr[i] % 2 == 0 && arr[j] % 2 == 0)cnt++;if (arr[i] % 2 != 0 && arr[j] % 2 != 0)cnt++;}}}return cnt;}int main(){int n = 4;int arr[n] = {1, 2, 3, 4};cout << theSavior(n, arr) << "\n";return 0;}
No comments:
Post a Comment