Monday, 31 October 2016

Equalize the Array Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
    int n,a,cnt[102] = {0},mx = 0,index = -1,ans = 0;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a;
        cnt[a]++;
        if(cnt[a] > mx){
            mx = cnt[a];
            index = a;
        }
    }
    for(int i = 1; i <= 100; i++)if(i != index)ans+=cnt[i];
    cout << ans << endl;
    return 0;
}

Strange Counter Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){
    long long t,n = 2;
    cin >> t;
    while(3*(n-1) < t)n *= 2;
    cout << 3*(n-1)-t+1 << endl;
    return 0;
}

Sunday, 30 October 2016

Apple and Orange Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){
    int s,t,a,b,m,n,ap,apcnt = 0,ora,orcnt = 0;
    cin >> s >> t >> a >> b >> m >> n;
    for(int i = 0;i < m;i++){
        cin >> ap;
        if(a+ap >= s && a+ap <= t)apcnt++;
    }
    for(int i = 0;i < n;i++){
        cin >> ora;
        if(b+ora >= s && b+ora <= t)orcnt++;
    }
    cout << apcnt << "\n" << orcnt << endl;
    return 0;
}

Thursday, 27 October 2016

Left Rotation Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
    int n,d,ar[100002];
    cin >> n >> d;
    for(int i = 0; i < n; i++)cin >> ar[(n-d+i)%n];
    for(int i = 0; i < n; i++)cout << ar[i] << " ";
    cout << endl;
    return 0;
}

Flatland Space Stations Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){
    int n,m;
    cin >> n >> m;
    vector<int> c;
    for(int i = 0;i < m;i++){
       int a;cin >> a;
       c.push_back(a);
    }
    int ans = 0;
    if(n == m){
        cout << ans << endl;
        return 0;
    }
    sort(c.begin(),c.end());
    for(int i = 0; i < m-1;  i++)ans = max(ans,abs(c[i]-c[i+1])/2);
    ans = max(max(ans,c[0]),(n-1)-c[m-1]);
    cout << ans << endl;
    return 0;
}

Wednesday, 26 October 2016

LightOJ - 1301 - Monitoring Processes Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
struct cmp{
    bool operator()(int& n1, int& n2){
        if(n1 > n2)return true;
        return false;
    }
};
int main() {
    int T,a,b;
    scanf("%d",&T);
    for(int t = 1; t <= T; t++){
        int n;
        scanf("%d",&n);
        vector<vector<int> >ar;
        for(int i = 0; i < n; i++){
            scanf("%d %d",&a,&b);
            vector<int>tmp;
            tmp.push_back(a);
            tmp.push_back(b);
            ar.push_back(tmp);
        }
        sort(ar.begin(),ar.end());
        int cnt = 1;
        priority_queue<int,vector<int>,cmp> pqueue;
        pqueue.push(ar[0][1]);
        for(int i = 0; i < n-1; i++){
            if(ar[i+1][0] <= pqueue.top()){
                cnt++;
                pqueue.push(ar[i+1][1]);
            }else {
                pqueue.pop();
                pqueue.push(ar[i+1][1]);
            }
        }
        printf("Case %d: %d\n",t,cnt);
    }
    return 0;
}

Thursday, 20 October 2016

Larry's Array Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
    int T,n;
    cin >> T;
    for(int t = 0; t < T; t++){
        cin >> n;
        int ar[n],sum = 0;
        for(int i = 0; i < n; i++)cin >> ar[i];
        for(int i = 0; i < n; i++){
            for(int j = i+1; j < n; j++)if(ar[j] < ar[i])sum += 1; 
        }
        (sum%2 == 0) ? cout << "YES" << endl : cout << "NO" << endl;
    }
    return 0;
}

Tuesday, 18 October 2016

Bon Appétit Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
    int n,k,totalCost = 0,charged,a;
    cin >> n >> k;
    for(int i = 0; i < n; i++){
        cin >> a;
        if(i!=k)totalCost+=a;
    }
    cin >> charged;
    ((charged -= (totalCost/2)) != 0) ? cout << charged << endl : cout << "Bon Appetit" << endl;
    return 0;
}

Viral Advertising Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() { int n; cin >> n; int result = 2,like = 2; for(int i = 2; i <= n; i++){ like*=3; like = like/2; result+=like; } cout << result << endl; return 0; }

Friday, 14 October 2016

Sock Merchant Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
    int ar[102],n,cnt[102] = {0},pairs = 0;
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> ar[i];
        cnt[ar[i]]++;
    }
    for(int i = 0; i < 101; i++){
        if(cnt[i]!= 0)pairs+=cnt[i]/2;
    }
    cout << pairs << endl;
    return 0;
}

Repeated String Hacker Rank Problem Solution.


Solution:


#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() { string str; long long n,cnt = 0,rem,rept,remCnt = 0; cin >> str; cin >> n; long long len = str.length(); rem = n%len; rept = n/len; for(int i = 0; i < len; i++){ if(str[i] == 'a')cnt++; if(str[i] == 'a' && i < rem)remCnt++; } cout << (cnt*rept)+remCnt << endl; return 0; }

Wednesday, 5 October 2016

TEST - Life, the Universe, and Everything Sphere Problem Solution.


Solution:

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

int main() {
 vector<int>input;
 
 int a;
 while(cin >> a)input.push_back(a);
 
 for(int i  = 0; i < input.size(); i++){
  if(input[i] == 42)return 0;
  cout << input[i] << endl;
 }
 return 0;
}

Monday, 3 October 2016

Codeforces Round #375 (Div. 2), problem: (B) Text Document Analysis Solution.


Solution:

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

int main() {
 int n,lw = 0,nw = 0;
 string str;
 cin >> n;
 cin >> str;
 int len = str.length(),cnt = 0;
 
 bool cp = false;
 for(int i = 0; i < len; i++){
  if(str[i] != '_' && str[i] != '(' && str[i] != ')' && cp == false){
   cnt++;
  }
  if(str[i] == '_' || str[i] == '(' || str[i] == ')' && cp == false){
   if(lw < cnt)lw = cnt;
   cnt = 0;
  }
  if(str[i] == '(')cp=true;
  if(str[i] == ')')cp = false;
 }
 if(lw < cnt)lw = cnt;
 cnt = 0;
 for(int i = 0; i < len; i++){
  if(str[i] == '('){
   int j;
   for(j = i; str[j] != ')'; j++){
    if((str[j] == '_' || str[j] == '(') && (str[j+1] >= 'A' && str[j+1] <= 'Z' || str[j+1] >= 'a' && str[j+1] <= 'z'))nw++;
   }
   i=j;
  }
  
 }
 cout << lw << " " << nw << endl;
 return 0;
}

Codeforces Round #375 (Div. 2), problem: (A) The New Year: Meeting Friends Solution.

Solution:

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

int main() {
 int ar[3];
 cin >> ar[0] >> ar[1] >> ar[2];
 sort(ar,ar+3);
 int tot = abs(ar[1]-ar[0])+abs(ar[1]-ar[2]);
 cout << tot << endl;
 return 0;
}