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;}

Tuesday, 9 January 2018

Codeforces Problem 913A - Modular Exponentiation (C++)

Welcome:

The question to the problem can be found here:


Explanation:
1. Store 2 power n in a temporary variable.
2. x%y if y > x then x remains .
3. If y is smaller print x%y
4. Implement the logic. Happy Coding.

Code:

#include<bits/stdc++.h>
using namespace std;
#define clr(a,replace) memset(a,replace,sizeof(a))
#define INF 99999
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
#define PB push_back
#define MAX 100000
#define pf printf
#define sf scanf

typedef long long ll;
int main(){
//freopen("input.txt","r",stdin);
  //  freopen("output.txt","w",stdout);
int n,m;
cin>>n>>m;
int res=1;

for(int i=0;i<n;i++){
 res *= 2;
 if(res> m){
cout<<m<<endl;
 return 0;
 }

}
cout<<m%res<<endl;

return 0;}

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...