Friday, 5 August 2016

Minimum Distances Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
    int n,ar[1005],mn = INT_MAX;
    bool flag = false;
    cin >> n;
    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[i] == ar[j]){
                if(abs(i-j) < mn)mn = abs(i-j);
                flag = true;
                break;
            }
        }
    }
    if(!flag)mn = -1;
    cout << mn << endl;
    return 0;
}

Jumping on the Clouds: Revisited Hacker Rank Problem Solution.


Solution:

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

Thursday, 4 August 2016

Jumping on the Clouds Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {

 int N,ar[102],cnt = 0;
 cin >> N;
 for(int n = 0; n < N; n++)cin >> ar[n];
 for(int i = 1; i < N;cnt++){
        if(ar[i+1] != 1 && i < N-1)i+=2;
        else i++;  
 }
 cout << cnt << endl;
 return 0;
}

String Construction Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {

    int N;
 string str;
 cin >> N;
 for(int n = 0; n < N; n++){
        cin >> str;
        int len = str.length(),cnt = 0,ar[26]={0};
        for(int i = 0; i < len; i ++)ar[str[i]-'a'] = 1;
        for(int i = 0; i < 26; i++)
            if(ar[i] != 0)cnt++;
        cout << cnt << endl;
 }
 return 0;
}

Mars Exploration Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {

 string str;
 cin >> str;
 int len = str.length(),cnt = 0;
 for(int i = 0; i < len; i += 3){
        if(str[i] != 'S')cnt++;
        if(str[i+1] != 'O')cnt++;
        if(str[i+2] != 'S')cnt++;
 }
 cout << cnt << endl;
 return 0;
}

Beautiful Binary String Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {

 int n,cnt = 0;
 string str;
 cin >> n;
 cin >> str;
 for(int i = 0; i < n;){
        if(str[i] == '0' && str[i+1] == '1' && str[i+2] == '0'){
            cnt++;
            i+=3;
        }
        else i++;
 }
 cout << cnt << endl;
 return 0;
}

Wednesday, 3 August 2016

CamelCase Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {

 string str;
 int cnt = 1;
 cin >> str;
 int len = str.length();
 for(int i = 0; i < len; i++){
        if(str[i]>= 65 && str[i] <= 90)cnt++;
 }
 cout << cnt << endl;
 return 0;
}

Super Reduced String Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {

 string str;
 cin >> str;
 int len = str.length();
 for(int i = 0; i < len;){
        if(str[i] == str[i+1] && i > -1){
            str.erase(i,2);
            i--;
        }
        else i++;
 }

 if(!str.empty())cout << str << endl;
 else cout << "Empty String" << endl;
 return 0;
}

Circular Array Rotation Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
 int n,k,q,a,m;
 vector<int>ar;
 scanf("%d %d %d",&n,&k,&q);
 for(int i = 0; i < n; i++){
        scanf("%d",&a);
        ar.push_back(a);
 }
 k%=n;;
 for(int i = 0; i < q; i++){
        scanf("%d",&m);
        if(m-k >= 0)printf("%d\n",ar[m-k]);
        else printf("%d\n",ar[m-k+n]);
 }
 return 0;
}