Wednesday, 29 June 2016

Service Lane Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){
    int n;
    int t;
    cin >> n >> t;
    vector<int> width(n);
    for(int width_i = 0;width_i < n;width_i++){
        cin >> width[width_i];
    }
    for(int a0 = 0; a0 < t; a0++){
        int i;
        int j;
        cin >> i >> j;
        int pass = INT_MAX;
        for(int l = i; l  <= j; l++){
            if(width[l] <= pass)pass = width[l];
        }
        cout << pass << endl;
    }
    return 0;
}

Monday, 27 June 2016

Codeforces Round #359 (Div. 2), problem: (A) Solution.

                                    A. Free Ice Cream
After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer.
At the start of the day they have x ice cream packs. Since the ice cream is free, people start standing in the queue before Kay and Gerda's house even in the night. Each person in the queue wants either to take several ice cream packs for himself and his friends or to give several ice cream packs to Kay and Gerda (carriers that bring ice cream have to stand in the same queue).
If a carrier with d ice cream packs comes to the house, then Kay and Gerda take all his packs. If a child who wants to take d ice cream packs comes to the house, then Kay and Gerda will give him d packs if they have enough ice cream, otherwise the child will get no ice cream at all and will leave in distress.
Kay wants to find the amount of ice cream they will have after all people will leave from the queue, and Gerda wants to find the number of distressed kids.
Input
The first line contains two space-separated integers n and x (1 ≤ n ≤ 10000 ≤ x ≤ 109).
Each of the next n lines contains a character '+' or '-', and an integer di, separated by a space (1 ≤ di ≤ 109). Record "di" in i-th line means that a carrier with di ice cream packs occupies i-th place from the start of the queue, and record "di" means that a child who wants to take di packs stands in i-th place.
Output
Print two space-separated integers — number of ice cream packs left after all operations, and number of kids that left the house in distress.
Examples
input
5 7
+ 5
- 10
- 20
+ 40
- 20
output
22 1
input
5 17
- 16
- 2
- 98
+ 100
- 98
output
3 2
Note
Consider the first sample.
  1. Initially Kay and Gerda have 7 packs of ice cream.
  2. Carrier brings 5 more, so now they have 12 packs.
  3. A kid asks for 10 packs and receives them. There are only 2 packs remaining.
  4. Another kid asks for 20 packs. Kay and Gerda do not have them, so the kid goes away distressed.
  5. Carrier bring 40 packs, now Kay and Gerda have 42 packs.
  6. Kid asks for 20 packs and receives them. There are 22 packs remaining.

Solution:


#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
 
    long long  n,x,cnt = 0;
    cin >> n >> x;
 
    for(int i = 0; i < n; i++){
        long long d;
        char c;
        cin >> c >> d;
        if(c =='+')x+=d;
        else{
            if(x>=d)x-=d;
            else cnt++;
        }
    }
    cout << x << " " << cnt << endl;
    return 0;
}

Monday, 20 June 2016

Uva - 10071 - Back to High School Physics Problem Solution.


Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){
 
 int v,t;
 
 while(scanf("%d %d",&v,&t) == 2)cout << (v*t*2) << endl;
 return 0; 
}

Sunday, 19 June 2016

Uva - 11854 - Egypt Problem Solution.


Solution:

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

    int a,b,c;
    while(scanf("%d %d %d",&a,&b,&c) && (a != 0 && b != 0 && c != 0)){
        if(a < 0 || b < 0 || c < 0)cout << "wrong" << endl;
        else if(a*a == (b*b+c*c))cout << "right" << endl;
        else if(b*b == (a*a+c*c))cout << "right" << endl;
        else if(c*c == (a*a+b*b))cout << "right" << endl;
        else cout << "wrong" << endl;
    }
    return 0;
}

Uva - 1225 - Digit Counting 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++){
        int cnt[10] = {0};
        cin >> n;
        for(int i = 1; i <= n; i++){
            int tmp = i;
            while(tmp != 0){
                cnt[tmp%10]++;
                tmp/=10;
            }
        }
        for(int i = 0; i <= 9; i++){
            cout << cnt[i];
            if(i < 9)cout << " ";
        }
        cout << endl;
    }
    return 0; 
}

Saturday, 18 June 2016

A C++ Program to check if the given string is a pangram or not Problem Solution.

Pangram Checking

A pangram is a sentence containing every letter in the English Alphabet.

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
bool checkPangram (string str){
 
    vector<bool> check(26, false);
    int index,len = str.length();
    for (int i = 0; i < len; i++){
        if (str[i] >= 'A' && str[i] <= 'Z')index = str[i] - 'A';
        else if(str[i] >= 'a' && str[i] <= 'z')index = str[i] - 'a';
        check[index] = true;
    }
    for (int i = 0; i <= 25; i++)
        if (!check[i])return false;
    return true;
}

int main(){
 
    string str = "The quick brown fox jumps over the lazy dog";
    
    if (checkPangram(str))cout << "\"" << str << "\"" << " is a pangram." << endl;
    else cout << "\"" << str << "\"" << " is not a pangram." << endl;
 
    return 0;
}

Output:


C And C++ Program to Print 1-10 numbers without using Conditional Loops Problem Solution.


C Solution:


#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
void printNumber(int value) {
 
    printf("%d\n", value);
    value++;
 
    if (value > 10)return;
    printNumber(value);
}

int main(void) {
 
    printNumber(1);
    return 0;
}

Another Way:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(void) {
 
   static int i = 1;// Static variable inside a function means “once the variable has been initialized, it remains in memory until the end of the program.”
   if (i <= 10) {
      printf("%d\n",i++);
      main();
   }
   return 0;
}


C++ Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
void printNumber(int value) {
 
    cout << value << endl;
    value++;
 
    if (value > 10)return;
    printNumber(value);
}

int main() {
 
    printNumber(1);
    return 0;
}

Another Way:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(void) {
 
   static int i = 1;// Static variable inside a function means “once the variable has been initialized, it remains in memory until the end of the program.”
   if (i <= 10) {
      cout << i++ << endl;
      main();
   }
   return 0;
}