940A - Points on the line - CodeForces Solution


brute force greedy sortings *1200

Please click on ads to support us..

Python Code:

n,d=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
Max=0
for i in range (n):
    for j in range (i,n):
        if a[j]-a[i]>d:
            break
        Max=max(Max,j-i+1)
print(n-Max)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define li long long int
#define ppb pop_back
#define ct cout 
#define el "\n"
#define ff first
#define ss second
#define sg string
#define vec vector<li>
#define vep vector<pair<li,li>>
#define mep map<li,li>
#define ves vector<string>
#define isort(n) sort(n.begin(), n.end())
#define dsort(n) sort(n.begin(), n.end(), greater<int>());
#define all(n) n.begin(), n.end()
#define ub upper_bound
#define lb lower_bound
#define maxe *max_element
#define mine *min_element
#define X INT_MAX
#define Y INT_MIN
#define sp " "


void take_input(vector<li> &v , int size){
    for (int i = 0; i <size ; ++i)
    {
        li k;
        cin>>k;
        v.pb(k);
    }
}

void printvec(vector<li> &v ){
for (int i = 0; i < v.size(); ++i)
{
    ct<<v[i]<<" ";
}
ct<<el;
}


//*********************************************************
void pranay_prasoon(){
li n,d;
cin>>n>>d;
vec v;
if (n==1)
{
    ct<<0<<el;
    return;
}
take_input(v,n);
isort(v);
vec count;
for (int i = 0; i < n; ++i)
{
    li ans=1;
    for (int j = n-1; j >i ; --j)
    {
        if (i==j)
        {
            continue;
        }
        if (v[j]-v[i]<=d && v[j]-v[i]>=0)
        {
            
            // ct<<v[i]<<sp<<v[j]<<el;
            ans++;
            // ct<<ans<<el;
            
        }

    }
      count.pb(ans);

}
 // printvec(count);
 ct<<n-maxe(all(count))<<el;











 }
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
    int queries=1;
    // cin >> queries;
    while (queries--)
    {
        pranay_prasoon();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff