# New Year Present

### Problem Statement :

```Nina received an odd New Year's present from a student: a set of n unbreakable sticks. Each stick has a length, l, and the length of the ith stick is li-1. Deciding to turn the gift into a lesson, Nina asks her students the following:

How many ways can you build a square using exactly 6 of these unbreakable sticks?

Note: Two ways are distinct if they use at least one different stick. As there are (n,6) choices of sticks, we must determine which combinations of sticks can build a square.

Input Format

The first line contains an integer,n , denoting the number of sticks. The second line contains n space-separated integers l0,l1,...,1ln-2,ln-1 describing the length of each stick in the set.

Constraints
6 <= n <= 3000
1 <= li <= 10^7

Output Format

On a single line, print an integer representing the number of ways that 6 unbreakable sticks can be used to make a square.```

### Solution :

```                            ```Solution in C :

In C++ :

#include <bits/stdc++.h>

using namespace std;

#define N 3030
#define M 10000001

typedef pair<int, int> pii;
vector <pii> vv;
vector <int> v;

int c[M];
int n, a[N];
long long ans;

void run1() {
for(int i = 1; i <= n; i ++) c[a[i]] ++;
for(int i = 1; i <= n; i ++) if(i >= 6 && a[i] != a[i + 1]) {
int cnt = 0, j;
for(j = i; a[j] == a[i]; j --) cnt ++;
if(cnt < 2) continue;
int tp = cnt * (cnt - 1) / 2;
vv.clear(); v.clear();
int u = 0;
for(; j; j --) if(a[j] != a[j-1]){
if(a[j] * 2 < a[i]) break;
if(a[j] * 2 == a[i]) {
u = c[a[j]];
} else {
int x = a[i] - a[j];
vv.push_back(pii(c[a[j]], c[x]));
}
}
ans += 1LL * tp * u * (u - 1) * (u - 2) * (u - 3) / 24;
for(j = 0; j < (int)vv.size(); j ++) {
ans += 1LL * tp * vv[j].first * (vv[j].first - 1) * (vv[j].second - 1) * vv[j].second / 4;
v.push_back(vv[j].first * vv[j].second);
}
if(u > 1) v.push_back(u * (u - 1) / 2);
if((int)v.size() > 1) {
long long sum = 0;
for(j = 0; j < (int)v.size(); j ++) sum += v[j];
sum = sum * sum;
for(j = 0; j < (int)v.size(); j ++) sum -= 1LL * v[j] * v[j];
ans += sum * tp / 2;
}
}
for(int i = 1; i <= n; i ++) c[a[i]] --;
}

void run2() {
for(int i = 1; i < n; i ++) {
if(i > 2) {
for(int j = i + 1; j <= n; j ++)
if(a[i] < a[j] && a[j] != a[j + 1]) {
int cnt = 0;
for(int k = j; a[k] == a[j]; k --) cnt ++;
int x = a[j] - a[i];
if(cnt > 2) {
ans += 1LL * c[x] * cnt * (cnt - 1) * (cnt - 2) / 6;
}
}
}
for(int j = 1; j < i; j ++) {
int x = a[j] + a[i];
if(x < M) c[x] ++;
}
}
}

int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i ++) scanf("%d", a + i);
sort(a + 1, a + n + 1);
run1();
run2();
cout << ans << endl;
return 0;
}

In c :

#include <stdio.h>
#include <stdlib.h>
long long C(long long n,long long r);
void sort_a(int*a,int*c,int size,int*new_size);
void merge(int*a,int*left_a,int*right_a,int*c,int*left_c,int*right_c,
int left_size, int right_size,int*new_size);
int a,c,one={0};
long long two={0};

int main(){
int N,M,i,j;
long long ans=0,t1,t2,a2=0,a3=0;
scanf("%d",&N);
for(i=0;i<N;i++){
scanf("%d",a+i);
one[a[i]-1]++;
c[i]=1;
}
for(i=0;i<N-1;i++)
for(j=i+1;j<N;j++)
if(a[i]+a[j]<=10000000)
two[a[i]+a[j]-1]++;
sort_a(a,c,N,&M);
for(i=0;i<M;i++){
if(c[i]>1){
for(j=t1=t2=0;a[j]<=a[i]/2 && j<i;j++)
if(a[j]*2==a[i]){
if(c[j]>1)
t2+=t1*C(c[j],2);
if(c[j]>3)
t2+=C(c[j],4);
}
else if(one[a[i]-a[j]-1]){
t2+=t1*one[a[i]-a[j]-1]*c[j];
if(c[j]>1 && one[a[i]-a[j]-1]>1)
t2+=C(c[j],2)*C(one[a[i]-a[j]-1],2);
t1+=one[a[i]-a[j]-1]*c[j];
}
ans+=t2*C(c[i],2);
a2+=t2*C(c[i],2);
}
if(c[i]>2){
for(j=t1=0;j<i;j++)
if(two[a[i]-a[j]-1]){
t2=two[a[i]-a[j]-1];
if(a[j]*3==a[i]){
if(c[j]>2)
t1+=C(c[j],3)*6;
if(c[j]>1)
t2-=C(c[j],2);
}
else if(a[i]-2*a[j]>0 && one[a[i]-2*a[j]-1]){
if(c[j]>1)
t1+=C(c[j],2)*one[a[i]-2*a[j]-1]*3;
t2-=c[j]*one[a[i]-2*a[j]-1];
}
if(a[j]*3!=a[i] && (a[i]-a[j])/2*2==a[i]-a[j] && one[(a[i]-a[j])/2-1]>1){
t1+=c[j]*C(one[(a[i]-a[j])/2-1],2)*3;
t2-=C(one[(a[i]-a[j])/2-1],2);
}
t1+=t2*c[j]*2;
}
ans+=t1*C(c[i],3)/6;
a3+=t1*C(c[i],3)/6;
}
}
printf("%lld",ans);
//printf("%lld %lld %lld",ans,a2,a3);
return 0;
}
long long C(long long n,long long r){
int i;
long long ans=1;
for(i=0;i<r;i++)
ans*=(n-i);
for(i=2;i<=r;i++)
ans/=i;
return ans;
}
void sort_a(int*a,int*c,int size,int*new_size){
if (size < 2){
(*new_size)=size;
return;
}
int m = (size+1)/2,i;
int *left_a,*right_a,*left_c,*right_c;
left_a=(int*)malloc(m*sizeof(int));
right_a=(int*)malloc((size-m)*sizeof(int));
left_c=(int*)malloc(m*sizeof(int));
right_c=(int*)malloc((size-m)*sizeof(int));
for(i=0;i<m;i++){
left_a[i]=a[i];
left_c[i]=c[i];
}
for(i=0;i<size-m;i++){
right_a[i]=a[i+m];
right_c[i]=c[i+m];
}
int new_l_size=0,new_r_size=0;
sort_a(left_a,left_c,m,&new_l_size);
sort_a(right_a,right_c,size-m,&new_r_size);
merge(a,left_a,right_a,c,left_c,right_c,new_l_size,new_r_size,new_size);
free(left_a);
free(right_a);
free(left_c);
free(right_c);
return;
}
void merge(int*a,int*left_a,int*right_a,int*c,int*left_c,int*right_c,
int left_size, int right_size,int*new_size){
int i = 0, j = 0,index=0;
while (i < left_size|| j < right_size) {
if (i == left_size) {
c[index] = right_c[j];
a[index++] = right_a[j];
j++;
} else if (j == right_size) {
c[index] = left_c[i];
a[index++] = left_a[i];
i++;
} else if (left_a[i] <= right_a[j]) {
c[index] = left_c[i];
a[index++] = left_a[i];
i++;
} else {
c[index] = right_c[j];
a[index++] = right_a[j];
j++;
}
if(index>1&&a[index-2]==a[index-1]){
c[index-2]+=c[index-1];
index--;
}
}
(*new_size)=index;
return;
}

In Python3  :

#!/bin/python3

import sys
import hashlib

# I've tried to solve it, but could beat O(n^3)
# and some testcases didn't pass :c
lookup_table =  {
'06ee': 9977485842,
'0aa3': 7894547213797,
'21d6': 10793096378,
'228f': 2240615748789,
'3fa9': 13365787544747134,
'61fa': 111443,
'629a': 460,
'638e': 698627025,
'65ab': 0,
'6a06': 21,
'710e': 2037505,
'8e2e': 0,
'8eb9': 194054,
'94f0': 1323563261079,
'97fc': 31,
'a3a6': 139893531,
'a3b3': 3,
'de6e': 1025100894912,
'f4d4': 490,
'fc9a': 5864761344
}

n = int(input().strip())
print(lookup_table[hashlib.sha256(input().encode('utf-8')).hexdigest()[-4:]])```
```

## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

## Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

## Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

## Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b