Skip to content

Commit

Permalink
Merge pull request #15 from Ankur-Dwivedi22/day_1easy
Browse files Browse the repository at this point in the history
chore: completed day1 easy task
  • Loading branch information
sanghaibiraj authored Mar 23, 2024
2 parents d29649e + 62f4c29 commit 683a2a5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"Local: solution","url":"d:\\OPCODE\\Code\\codecrack\\easy\\day_1\\solution.cpp","tests":[{"id":1711178288869,"input":"4\n7\n1 1 1 1 1 1 1\n4\n3 2 1 3\n3\n1 2 3\n1\n1","output":"35 \n2\n0\n0"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\OPCODE\\Code\\codecrack\\easy\\day_1\\solution.cpp","group":"local","local":true}
68 changes: 45 additions & 23 deletions easy/day_1/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,56 @@
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
int test;
cin >> test;
while (test--)
{
lil n;
cin >> n;
lil a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
lil c = 1, s = 0;
for (int i = 1; i < n; i++)
lil num; //number of sticks
cin >> num;

//creating vector vector instead of array as it is logically incorrect to take input from user and then assign the size of array to that input
vector<lil> length_of_sticks(num);
//stores length of each stick

for (int i = 0; i < num; i++)
cin >> length_of_sticks[i];
//sorting the vector for searching triplets of triangle in vector in single loop
sort(length_of_sticks.begin() , length_of_sticks.end());

lil count = 1, ans = 0;
//count -> keeps track of same length sticks
//ans-> keeps track of total number of triplets
//1LL is multiplied to avoid any kind of multiplication overflow
for (int i = 1; i < num; i++)
{
if (a[i] == a[i - 1])
c++;
//if the current length of stick is equal to previous one it increments count by one
if (length_of_sticks[i] == length_of_sticks[i - 1])
count++;

//if above condition does not holds then it implies count of identical length sticks encountered so far = count
else
{
if (c >= 3)
s += c * (c - 1) * (c - 2) / 6;
if (i - c > 0)
s += c * (c - 1) / 2 * (i - c);
c = 1;
//calculating number of triplets within group of count
//count >= 3 then nC3 = n*(n-1)*(n-2)/6
//add the result to final answer
if (count >= 3)
ans += count *1LL* (count - 1) *1LL* (count - 2) / 6;

//selecting two elements from group of identical length sticks : nC2 = n*(n-1)/2
//rest other element = (i-count)
//add the result to final answer
if (i - count > 0)
ans += count *1LL* (count - 1) / 2 *1LL* (i - count);
count = 1;
}
}
if (c >= 3)
s += c * (c - 1) * (c - 2) / 6;
if (n - c > 0)
s += c * (c - 1) / 2 * (n - c);
Cout << s << endl;

//after processing all the sticks if there are remaining identical sticks >= 3 then follow the same approach nC3
if (count >= 3)
ans += count *1LL* (count - 1) *1LL* (count - 2) / 6;
//same approach of selecting two elements from identical and rest others
if (num - count > 0)
ans += count *1LL* (count - 1) / 2 *1LL* (num - count);
cout << ans << endl; //Cout is changed to cout
}
}

0 comments on commit 683a2a5

Please sign in to comment.