Skip to content

Commit 39ae8f4

Browse files
authored
Merge pull request #345 from InsanelySlowBurn/main
Day 2 Q1
2 parents 577af26 + 76eb233 commit 39ae8f4

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//We need to find what numbered person is oppisite to the person numbered 'c'
2+
//On the basis of 'a' and 'b' being opposites
3+
//Time and space complexity are both O(1)
4+
5+
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
9+
int main() {
10+
int t;
11+
cin>>t;
12+
while(t--){
13+
int a,b,c;
14+
cin>>a>>b>>c;
15+
//eg: 1,4,5
16+
17+
int n=abs(a-b)*2;
18+
//n=6
19+
20+
if(c>n){
21+
//5>6 is false
22+
//c>n then c would be out of the possible numbers
23+
cout<<"-1"<<endl;
24+
continue;
25+
}
26+
27+
if(c>(n/2)){
28+
//5>3 implies the number opposite to it is in the lower half
29+
cout<<c-(n/2)<<endl;
30+
}
31+
else{
32+
cout<<c+(n/2)<<endl;
33+
}
34+
35+
}
36+
37+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//We have to find how many pairs have the maximum absolute difference
2+
//only the highest and the lowest number in the array will have max difference
3+
//so we need to find how many times the highest and lwest element appears and calc their permutations
4+
//time and space complexity is O(n)
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
8+
int main() {
9+
int t;
10+
cin>>t;
11+
while(t--){
12+
long long int n;
13+
cin>>n;
14+
int a[n];
15+
for(int i=0;i<n;i++){
16+
cin>>a[i];
17+
}
18+
long long int j=1,k=1,min=a[0],max=a[0];
19+
//j is no of min elements
20+
//k is no of max elements
21+
22+
for(int i=1;i<n;i++){
23+
if(min<a[i]){
24+
j=1;
25+
min=a[i];
26+
}
27+
else if(min==a[i]){
28+
j++;
29+
}
30+
31+
if(max>a[i]){
32+
k=1;
33+
max=a[i];
34+
}
35+
else if(max==a[i]){
36+
k++;
37+
}
38+
}
39+
40+
if(min==max)
41+
//if all elements are same, permutation formula becomes n*(n-1)
42+
cout<<n*(n-1)<<endl;
43+
else
44+
cout<<j*k*2<<endl;
45+
}
46+
47+
}
48+
//https://codeforces.com/problemset/submission/1771/355657715

0 commit comments

Comments
 (0)