-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path125.cpp
63 lines (49 loc) · 1.4 KB
/
125.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// C++ Program to count all possible
// groups of size 2 or 3 that have
// sum as multiple of 3
#include<bits/stdc++.h>
using namespace std;
// Returns count of all possible groups
// that can be formed from elements of a[].
int findgroups(int arr[], int n)
{
// Create an array C[3] to store counts
// of elements with remainder 0, 1 and 2.
// c[i] would store count of elements
// with remainder i
int c[3] = {0}, i;
int res = 0; // To store the result
// Count elements with remainder 0, 1 and 2
for (i=0; i<n; i++)
c[arr[i]%3]++;
// Case 3.a: Count groups of size 2
// from 0 remainder elements
res += ((c[0]*(c[0]-1))>>1);
// Case 3.b: Count groups of size 2 with
// one element with 1 remainder and other
// with 2 remainder
res += c[1] * c[2];
// Case 4.a: Count groups of size 3
// with all 0 remainder elements
res += (c[0] * (c[0]-1) * (c[0]-2))/6;
// Case 4.b: Count groups of size 3
// with all 1 remainder elements
res += (c[1] * (c[1]-1) * (c[1]-2))/6;
// Case 4.c: Count groups of size 3
// with all 2 remainder elements
res += ((c[2]*(c[2]-1)*(c[2]-2))/6);
// Case 4.c: Count groups of size 3
// with different remainders
res += c[0]*c[1]*c[2];
// Return total count stored in res
return res;
}
// Driver Code
int main()
{
int arr[] = {3, 6, 7, 2, 9};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Required number of groups are "
<< findgroups(arr,n) << endl;
return 0;
}