Codeforces|Leetcode|Codechef free solutions
Open in Telegram
Free codeforces, Codechef, Leetcode solutions are available 😍😍😍😍😍😍 Helped More than 200+ students to crack coding round in 2022 and helped placed them in Good companies. 🥳🥳🥳🤩🤩🤩 Dm @Cpsoln if you want help in coding round.
Show more4 317
Subscribers
No data24 hours
-137 days
-5230 days
Posts Archive
Anyone good at machine learning then dm me
The work will be paid
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define _sort(x) sort(all(x))
#define loop(i, N) for (int i = 0; i < N; i++)
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
void solve()
{
int n;
cin >> n;
vpi arr(n);
loop(i, n) cin >> arr[i].first >> arr[i].second;
_sort(arr);
vi suf(n + 1, -1);
suf[n - 1] = arr[n - 1].second;
for (int i = n - 2; i >= 0; i--)
suf[i] = max(suf[i + 1], arr[i].second);
set<int> st;
int ans = INT_MAX;
for (int i = 0; i < n; i++)
{
int mx = suf[i + 1];
if (i != n - 1)
ans = min(ans, abs(mx - arr[i].first));
if (mx < arr[i].first && !st.empty())
{
auto curr = st.upper_bound(arr[i].first);
if (curr == st.end())
{
curr--;
ans = min(ans, abs(*curr - arr[i].first));
}
else
{
ans = min(ans, abs(*curr - arr[i].first));
if (curr != st.begin())
{
curr--;
ans = min(ans, abs(*curr - arr[i].first));
}
}
}
st.insert(arr[i].second);
}
cout << ans << endl;
}
int32_t main()
{
int T = 1;
cin >> T;
while (T--)
solve();
}
class Solution {
public:
int findValidSplit(vector& nums) {
int n=nums.size();
mapm;
for(int i=n-1;i>=0;i--){
int temp=nums[i];
for(int j=2;j<=sqrt(nums[i]);j++){
if(temp%j==0){
if(m[j]==0){
m[j]=i+1;
}
while(temp%j==0)
temp/=j;
}
}
if(temp>1){
if(m[temp]==0){
m[temp]=i+1;
}
}
}
int idx=1;
for(int i=0;i1){
idx=max(idx,m[temp]);
}
}
if(idx==n)
return -1;
return idx - 1;
}
};
int MOD = 1e9 + 7;
class Solution {
public:
int waysToReachTarget(int target, vector>& types) {
vector dp(target + 1);
dp[0] = 1;
for (auto& t : types) {
for (int i = target; i >= 0; i--) {
for (int j = 1; j <= t[0] && i - j * t[1] >= 0; j++) {
dp[i] = (dp[i] + dp[i - j * t[1]]) % MOD;
}
}
}
return dp[target];
}
};
class Solution:
def findValidSplit(self, nums: List[int]) -> int:
def hcfnaive(a, b):
if(b == 0):
return abs(a)
else:
return hcfnaive(b, a % b)
d=1
c=1
for i in range(len(nums)):
d*=nums[i]
for i in range(len(nums)-1):
c*=nums[i]
if hcfnaive(c,abs(d//c))==1:
return i
return -1
