Saturday, 24 February 2018

Codeforces Problem 940A - Points on the line (C++)



Welcome:

The question to the problem can be found here:


Explanation:
1. The diameter is the distance between any two points in the set.
2. Run two nested loops to compare all the elements.
3. Take a variable Ans. It will indicate minimum position in the array to satisfy the number of points taken.
4. Replace ans by comparing each element with all possible elements (ans).
5. Subtract the position ans from the total elements n.
6. Print it and Happy Coding.

Code:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
int d;
int k;
cin>>n>>d;
int arr[101];
for(int i=1;i<=n;i++){
    cin>>arr[i];
}
sort(arr+1,arr+n+1);
int ans=0;
for(int i=1;i<=n;i++){
    for(int j=i;j<=n;j++){
        if(arr[j]-arr[i]<=d && j-i+1>ans){
            ans = j-i+1;
        }
    }
}
    cout<<n-ans<<endl;


return 0;}

No comments:

Post a Comment

Spoj Problem ACMCEG2C - Pick the candies (C++)

  The problem link may be found here.       Explanation: Use Deque to keep track of elements of the variety of candies. If i is gre...