Skip to content

Commit ab7462d

Browse files
committed
Implement solution for "B. Circle of People" problem
1 parent 249aa38 commit ab7462d

1 file changed

Lines changed: 85 additions & 51 deletions

File tree

Problems/Mathematics/Day-01/sol/amansharma264/solution1.cpp

Lines changed: 85 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,108 @@
11
/*
2-
Problem: A. Spell Check
3-
4-
Problem Statement:
5-
Timur allows any permutation of the letters of his name "Timur" as a valid spelling,
6-
but the spelling must contain exactly one uppercase 'T' and the remaining letters
7-
('i', 'm', 'u', 'r') must be lowercase. Given a string s, check if it is a valid spelling
8-
of Timur's name.
9-
10-
Approach:
11-
1. If the length of the string is not 5, it cannot be "Timur".
12-
2. Sort the given string.
13-
3. Sort the reference string "Timur".
14-
4. If both sorted strings are equal, then s is a valid permutation of "Timur".
2+
====================================================
3+
PROBLEM STATEMENT:
4+
====================================================
5+
There are n people standing in a circle (n is even).
6+
Each person looks at the person exactly opposite to them.
7+
8+
You are given three distinct integers a, b, and c.
9+
It is known that person 'a' is looking at person 'b'.
10+
11+
Determine the person that 'c' is looking at.
12+
If multiple answers exist, print any.
13+
If no valid answer exists, print -1.
14+
15+
----------------------------------------------------
16+
PROBLEM LINK:
17+
https://codeforces.com/problemset/problem/1560/B
18+
====================================================
19+
*/
1520

16-
Time Complexity:
17-
O(n log n), where n = 5 (effectively constant).
21+
/*
22+
====================================================
23+
APPROACH:
24+
====================================================
25+
If person 'a' is looking at person 'b', then:
26+
- The distance between them is |a - b|
27+
- Since they are opposite, total number of people:
28+
n = 2 * |a - b|
29+
30+
Steps:
31+
1. Compute dist = |a - b|
32+
2. Compute n = 2 * dist
33+
3. If a, b, or c is greater than n → invalid → print -1
34+
4. Otherwise, the person opposite to c is:
35+
- c + dist (if within n)
36+
- else c - dist
37+
38+
----------------------------------------------------
39+
WHY THIS WORKS:
40+
- In a circular arrangement, opposite positions differ
41+
by exactly n/2 positions.
42+
====================================================
43+
*/
1844

19-
Space Complexity:
20-
O(1), uses only constant extra space.
45+
/*
46+
====================================================
47+
TIME & SPACE COMPLEXITY:
48+
====================================================
49+
Time Complexity: O(1) per test case
50+
Space Complexity: O(1)
51+
====================================================
52+
*/
2153

22-
Example:
54+
/*
55+
====================================================
56+
EXAMPLE:
57+
====================================================
2358
Input:
2459
1
25-
5
26-
miurT
60+
1 3 2
2761
2862
Output:
29-
YES
63+
4
3064
31-
Question Link:
32-
https://codeforces.com/problemset/problem/1722/A
65+
Explanation:
66+
|1 - 3| = 2 → n = 4
67+
Opposite of 2 is 2 + 2 = 4
68+
====================================================
69+
*/
3370

34-
Submission Link:
35-
https://codeforces.com/contest/1722/submission/345095137
71+
/*
72+
====================================================
73+
SUBMISSION LINK:
74+
====================================================
75+
https://codeforces.com/contest/1560/submission/345095137
76+
====================================================
3677
*/
3778

38-
#include <iostream>
39-
#include <algorithm>
79+
#include <bits/stdc++.h>
4080
using namespace std;
4181

42-
void spellCheck() {
43-
int n;
44-
cin >> n;
45-
46-
string s;
47-
cin >> s;
48-
49-
if (n != 5) {
50-
cout << "NO\n";
51-
return;
52-
}
53-
54-
string name = "Timur";
55-
56-
sort(s.begin(), s.end());
57-
sort(name.begin(), name.end());
58-
59-
if (s == name) {
60-
cout << "YES\n";
61-
} else {
62-
cout << "NO\n";
63-
}
64-
}
82+
#define fastio() ios::sync_with_stdio(false); cin.tie(nullptr);
6583

6684
int main() {
85+
fastio();
86+
6787
int t;
6888
cin >> t;
6989

7090
while (t--) {
71-
spellCheck();
91+
long long a, b, c;
92+
cin >> a >> b >> c;
93+
94+
long long dist = llabs(a - b);
95+
long long n = 2 * dist;
96+
97+
if (a > n || b > n || c > n) {
98+
cout << -1 << '\n';
99+
continue;
100+
}
101+
102+
if (c + dist <= n)
103+
cout << c + dist << '\n';
104+
else
105+
cout << c - dist << '\n';
72106
}
73107

74108
return 0;

0 commit comments

Comments
 (0)