Monday, August 29, 2016

Time Conversion

Problem Link (Click here)

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

int main(){
    string time;
    string temp;
    cin >> time;
    temp = time;
    if(time[8]=='A'){
        time.erase (8,2);
        if(time[0]=='1'&&time[1]=='2'){
            time[0]='0';
            time[1]='0';
        }
        cout<<time;
    }
    else{
        time.erase (2,8);
        stringstream ss(time);
        int x;
        ss >> x;
        if(x!=12){
            x=x+12;
        }
        cout<<x;
        temp.erase (0,2);
        temp.erase (6,2);
        cout<<temp;
    }

    return 0;

}


Sunday, August 28, 2016

COUNTING SORT WITH NEGATIVE VALUE

#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    int temp_p[100];
    int temp_n[100];
    int m,n,i,x;
    int j=0;
    int p=0;
    cout<<"Total number : ";
    cin>>n;
    cout<<"Maximum number : ";
    cin>>m;
    int pos[m+1];
    int neg[m+1];
    memset(pos, 0, sizeof pos);
    memset(neg, 0, sizeof neg);
    for(i=0;i<n;i++){
        cin>>x;
        if(x>=0){
            temp_p[j]=x;
            pos[x]++;
            j++;
        }
        else{
            temp_n[p]=x*-1;
            neg[x*-1]++;
            p++;
        }
    }
    for(i=1;i<=m;i++){
        pos[i]=pos[i]+pos[i-1];
        neg[i]=neg[i]+neg[i-1];
    }
    int arry_p[j];
    int arry_n[p];

    for(i=0;i<j;i++){
        int b=temp_p[i];
        arry_p[pos[b]-1]=b;
        pos[b]--;
    }
    for(i=0;i<p;i++){
        int b=temp_n[i];
        arry_n[neg[b]-1]=b;
        neg[b]--;
    }
    for(i=p-1;i>=0;i--){
        cout<<(arry_n[i]*-1)<<" ";
    }
    for(i=0;i<j;i++){
        cout<<arry_p[i]<<" ";
    }
    cout<<endl;


    return 0;
}

COUNTING SORT

#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    int temp[100];
    int m,n,i;
    cout<<"Total number : ";
    cin>>n;
    cout<<"Maximum number : ";
    cin>>m;
    int c[m+1];
    memset(c, 0, sizeof c);
    for(i=0;i<n;i++){
        cin>>temp[i];
        int a=temp[i];
        c[a]++;
    }
    for(i=1;i<=m;i++)
        c[i]=c[i]+c[i-1];

    int arry[n];
    for(i=0;i<n;i++){
        int b=temp[i];
        arry[c[b]-1]=b;
        c[b]--;
    }
    for(i=0;i<n;i++){
        cout<<arry[i]<<" ";
    }
    cout<<endl;

    return 0;
}

Tuesday, August 2, 2016

Kruskal's algorithm

#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

struct node{
    int s;
    int e;
    int weight;
};

int makeset(int a[],int e){
    for(int i=0;i<=e;i++)\
        a[i]=i;
    return a[e];
}
int found(int arry[], int x){
    if(x==arry[x])
        return x;
    return found(arry, arry[x]);
}
int Union(int arry[],int x,int y){
    if(x!=y)
        arry[y]=x;
}

bool compare(node a, node b){
    if(a.weight<b.weight)
        return true;
    else
        return false;
}

int main(){

    int i, totalEdge, totalNode, p1, p2, w;
    node edgeList[50];
    cout << "How many vertices?  " ;
    cin>>totalNode;
    cout << "How many edges?  " ;
    cin>>totalEdge;

    for(i=0;i<totalEdge;i++){
        cin>>p1;
        cin>>p2;
        cin>>w;

        edgeList[i].s = p1;
        edgeList[i].e = p2;
        edgeList[i].weight = w;
        edgeList[i].s = p2;
        edgeList[i].e = p1;
    }
    sort(edgeList,edgeList+totalEdge, compare);
    int arry[totalEdge];
    makeset(arry,totalEdge);

    int total=0;
    for(int i=0; i<totalEdge; i++){
        int x=found(arry, edgeList[i].s);
        int y=found(arry, edgeList[i].e);
        if(x!=y){
            Union(arry,x,y);
            total+=edgeList[i].weight;
            cout << edgeList[i].s << " " << edgeList[i].e << " " << edgeList[i].weight << endl;
        }
    }
    cout<< total<<endl;


    return 0;
}

Saturday, July 30, 2016

Coin Change Problem Using Dynamic Programming

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <conio.h>
#include <climits>
#include <cstring>

using namespace std;

int main ()
{
    int amount;
    int b[10000];
    memset(b, INT_MAX, sizeof b);
    b[0]=0;
    int a[20];
    int coin;
    int i;
    cout << "amount : " ;
    cin >> amount;
    cout << "No of coin : " ;
    cin >> coin;
    for(i=0; i<coin; i++){
        cin >> a[i];
    }

     for(int i = 0; i <amount; i++)
    {
        for(int j = a[i] ; j <= amount; j++)
        {
            int pre = b[j];
            int cur = 1+b[j-a[i]];
            b[j] = min(pre,cur);
        }
    }
     cout << b[amount];

getch();
return 0;
}

The Activity Selection Problem With Greedy Algorithom

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <conio.h>

using namespace std;

struct activity{
double s;
double e;
double c;
};

bool compare(activity p1, activity p2){
if(p1.e<p2.e)
return true;
else
return false;
}

int main(){
activity p[100];
int n,i;
int start,End;
int count = 1;
scanf("%d",&n);
for(i=0;i<n;i++){
cin>>start;
cin>>End;
p[i].s = start;
p[i].e = End;
}
sort(p,p+n, compare);

int j= 0;
for(i=1; i<n; i++){
        if(p[i].s >= p[j].e){
            count ++;
            j=i;
        }
}
cout << "Activity : "<<count<<endl;

getch();
return 0;
}

The Fractional Knapsack Problem With Greedy Algorithom

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <conio.h>


using namespace std;

struct Product{
double b;
double w;
double v;
double c;
};

bool compare(Product p1, Product p2){
if(p1.v>p2.v){
return true;
}
else{
return false;
}
}

int main(){
Product p[100];
int n,i;
double w,b,v;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%lf",&w);
scanf("%lf",&b);
p[i].w = w;
p[i].b = b;
p[i].v = b/w;
}
sort(p,p+n, compare);

int k=10,f=0,t=0;
for(i=0;i<n;i++){
        p[i].c=0;
}
i=0;
while(f<k){
        if(p[i].w <=(k-f)){
            t += p[i].b;
            f += p[i].w;
            p[i].c = p[i].w;
        }
        else{
            t += (k-f)*p[i].v;
            p[i].c = k-f;
             f += k-f;
        }
        i++;
}
cout <<"Total profit  :  " <<t << " $\n";

for(i=0;i<n;i++)
        cout << p[i].c << " \n";

getch();
return 0;
}

Disjoint-set data structure

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>

using namespace std;

int arry[100];

int makeset(int x){
    arry[x]=x;
}

int found(int x){
    if(x==arry[x])
        return x;
    return found(arry[x]);
}

int Union(int x,int y){
    int s1=found(x);
    int s2=found(y);
    if(s1!=s2){
        arry[s2]=s1;
        cout<<"Union between "<<x<<" & "<<y<<endl;
    }
    else
        cout<<"sorry, They are in same set\n";
}


int main()
{

    cout << "Please input your choice\n1. Make Set\n2. Union\n3. Find\n4. End"<< endl;
    while(1){
        cout << "Please input your choice : ";
        int n;
        cin>>n;
        switch(n)
        {
            case 1 : int a;
                     cout<< "Enter element : ";
                     cin>>a;
                     makeset(a);
                     break;
            case 2 : int x,y;
                     cout<< "Enter two element for union : ";
                     cin>>x>>y;
                     Union(x,y);
                     break;
            case 3 : int z;
                     cout<< "Enter find element : ";
                     cin>>z;
                     cout<< "Set no : "<< found(z) <<endl;
                     break;
            case 4 : return 0;
                     break;
            default : cout<<"Wrong input..... Try again"<<endl;
        }
    }

    return 0;
}

Tuesday, July 26, 2016

The Maximum Subarray

Problem link (Click Here)

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <climits>
#include <vector>

using namespace std;

int arry[1000000];

int maxoverlap(int f, int m, int l){
 int m1= INT_MIN;
    int s=0;
    for(int i=m; i>=f; i--){
        s+=arry[i];
        m1 = max(m1, s);
    }
 int m2= INT_MIN;
    s=0;
    for(int i=m+1; i<=l; i++){
        s+=arry[i];
        m2 = max(m2, s);
    }
    return m1+m2;
}

int maxsum(int f, int l){
 if(f==l) return arry[f];
 int lmax;
 int rmax;
 int over;
    if(f<l){
        int m = (f+l)/2;
         lmax = maxsum(f,m);
         rmax = maxsum(m+1,l);
         over = maxoverlap(f,m,l);

    }
    return max(max(lmax,rmax),over);
}


int main()
{
    int t;
    cin>> t;
    while(t--){
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>arry[i];
        }
        int maxs = maxsum(0, n-1);
        int a=0;
        for(int i=0;i<n;i++){
            if(arry[i]>=0){
                a+=arry[i];
            }
        }
        cout << maxs <<" "<< a << endl;
    }

    return 0;
}



Free Bambooo Bank Limited

Problem link(Click Here)

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <climits>
#include <vector>

using namespace std;

int main()
{
    int o[100];
    int e[100];
    int a[100];
    int n,t=0;
    int wc=3;
    int odd=0,even=0;
    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a[i];
        if(a[i]%2==0)
            even++;
        else
            odd++;
    }
    int j=0;
    int k=0;
    for(int i=0;i<n;i++){
        if(a[i]%2==0){
            e[j]=a[i];
            j++;
        }
        else{
            o[k]=a[i];
            k++;
        }
    }
    int count=0;
    for(int i=0; i<odd-1; i++){
        if(o[i]>o[i+1])
            count++;
    }
     for(int i=0; i<even-1; i++){
        if(e[i]>e[i+1])
            count++;
    }
    t=even/wc;
    t=t+odd/wc;
    cout<< t << endl ;
    if(count==0)
        cout<<"OK"<< endl;
    else
        cout<<"NOT OK"<< endl;

    return 0;
}


Tuesday, June 21, 2016

INCREMENT HEAP SORT WITH ASCENDING ORDER

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

int arry[100];

int heapify(int i,int n)
{
    int leargest;
    int left=2*i+1;
    int right=2*i+2;
    if(left<n && arry[left]>arry[i])
        leargest=left;
    else
        leargest=i;
    if(right<n && arry[right]>arry[leargest])
        leargest=right;
    if(leargest!=i){
        swap (arry[i],arry[leargest]);
        heapify(leargest,n);
    }
}

int main ()
{
    int n,i;
    cout << "No of Array index : ";
    cin>>n;
    int temp = n;
    for(i=0; i<n; i++){
        cin>> arry[i];
    }
    for(int i=(n/2); i>=0; i--){
        heapify(i,n);
    }
     while(n>0){
        swap(arry[0], arry[n-1]);
        n--;
        heapify(0, n);
    }
    for(i=0; i<temp; i++)
     cout << arry[i] << " ";
     cout << endl;

    return 0;
}

INCREMENT HEAP SORT WITH DESCENDING ORDER

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

int arry[100];

int heapify(int i,int n)
{
    int leargest;
    int left=2*i+1;
    int right=2*i+2;
    if(left<n && arry[left]<arry[i])
        leargest=left;
    else
        leargest=i;
    if(right<n && arry[right]<arry[leargest])
        leargest=right;
    if(leargest!=i){
        swap (arry[i],arry[leargest]);
        heapify(leargest,n);
    }
}

int main ()
{
    int n,i;
    cout << "No of Array index : ";
    cin>>n;
    int temp = n;
    for(i=0; i<n; i++){
        cin>> arry[i];
    }
    for(int i=(n/2); i>=0; i--){
        heapify(i,n);
    }
     while(n>0){
        swap(arry[0], arry[n-1]);
        n--;
        heapify(0, n);
    }
    for(i=0; i<temp; i++)
     cout << arry[i] << " ";
     cout << endl;

    return 0;
}

Tuesday, June 14, 2016

MAXIMUM SUM IN A SUBARRAY WITH ARRAY

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>
#include <cstring>
#include <conio.h>
#include <algorithm>
#include <climits>
#include <vector>
using namespace std;


int maxoverlap(vector <int> &arry, int f, int m, int l){
    int m1= INT_MIN;
    int s=0;
    for(int i=m; i>=f; i--){
        s+=arry[i];
        cout<< arry[i]<< endl;
        m1 = max(m1, s);
    }
    int m2= INT_MIN;
    s=0;
    for(int i=m+1; i<=l; i++){
        s+=arry[i];
        m2 = max(m2, s);
    }
    return m1+m2;
}

int maxsum(vector <int> &arry, int f, int l){
    if(f==l) return arry[f];
    else if(f<l){
        int m = (f+l)/2;
        int lmax = maxsum(arry, f, m);
        int rmax = maxsum(arry, m+1,l);
        int over = maxoverlap(arry,f,m,l);
        return max(max(lmax,rmax),over);
    }
}


int main()
{
    int n;
    cout << "Size of index : " ;
    cin>> n;
    vector <int> v;
    int temp;

    for(int i=0; i<n; i++){
        cin>> temp;
        v.push_back(temp);
    }

    cout << maxsum(v, 0, n-1) << endl;


    return 0;
}

MAXIMUM SUM IN A SUBARRAY WITH ARRAY

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>
#include <cstring>
#include <conio.h>
#include <algorithm>
#include <climits>
#include <vector>

using namespace std;

int arry[100];

int maxoverlap(int f, int m, int l){
int m1= INT_MIN;
    int s=0;
    for(int i=m; i>=f; i--){
        s+=arry[i];
        m1 = max(m1, s);
    }
int m2= INT_MIN;
    s=0;
    for(int i=m+1; i<=l; i++){
        s+=arry[i];
        m2 = max(m2, s);
    }
    return m1+m2;
}

int maxsum(int f, int l){
if(f==l) return arry[f];
    if(f<l){
        int m = (f+l)/2;
        int lmax = maxsum(f,m);
        int rmax = maxsum(m+1,l);
        int over = maxoverlap(f,m,l);
        return max(max(lmax,rmax),over);
    }
}


int main()
{
    int n;
cout << "Size of index : " ;
    cin>> n;
    for(int i=0; i<n; i++){
        cin >> arry[i];
    }
 
    cout << maxsum(0, n-1) << endl;

    return 0;
}


Tuesday, June 7, 2016

FIND PRIME NUMBER USING RECURSION

#include <iostream>
#include <string>

using namespace std;


int prime (int i, int n)
{
    if(i==n/2)return 0;
    if(n%i==0)
        return 1;
    prime (i+1,n);
}

int main()
{
    int n, i;
    int found;
    cin>> n;
    found = prime (2 ,n);
    if(found==0)
        cout<< "Prime"<< endl;
    else
        cout<< "Not prime"<< endl;

    return 0;
}

FIND THE FREQUENCY OF AN ELEMENT IN AN ARRAY USING RECURSION

#include <iostream>
#include <cstdio>

using namespace std;

int count=0;
int a[100];

int func (int i,int n,int f)
{
    if(i>=n)
        return count;
    if(a[i]==f)
        count++;
    func(i+1,n,f);
}

int main()
{
    int n, i;
    cin>> n;

    for(i=0; i<n; i++){
        cin>> a[i];
    }
    cout<<"Find element : ";
    int f;
    cin >> f;
    int sum=func(0,n,f);

    cout<<"Frequence of this element : "<< sum <<endl;

    return 0;
}

FIND THE SUMMATIONOF ALL ELEMENT IN AN ARRAY USING RECURSION

#include <iostream>
#include <cstdio>


using namespace std;

int a[100];

int func (int i,int n,int sum)
{
    if(i>=n)
        return sum;
    sum += a[i];
    func(i+1,n,sum);
}

int main()
{
    int n, i,s=0;
    cin>> n;
    for(i=0; i<n; i++){
        cin>> a[i];
    }
    int sum=func(0,n,s);
    cout<<"Summation of this element : "<< sum <<endl;

    return 0;
}

FIND A MAX ELEMENT IN AN ARRAY USING RECURSION

#include <iostream>
#include <cstdio>

using namespace std;

int m=0;
int a[100];

int func (int i,int n)
{
    if(i>=n)
        return m;
    if(m<a[i]){
        m=a[i];
    }
    func(i+1,n);
}

int main()
{
    int n, i;
    cin>> n;
    for(i=0; i<n; i++){
        cin>> a[i];
    }

    int max=func(0,n);
    cout<<"Max element is : "<<max<<endl;


    return 0;
}

PRINT AN ARRAY IN REVERSE ORDER USING RECURSION

#include <iostream>
#include <string>

using namespace std;

int a[100];

void rev (int n)
{
    if(n==-1)return;
    cout<<a[n]<<" ";
    rev (n-1);
}

int main()
{
    int n, i;
    cin>> n;
    for(i=0; i<n; i++){
        cin>> a[i];
    }
    rev (n-1);


    return 0;
}

Thursday, June 2, 2016

CODEFORCES problem 266B Queue at the School

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main()
{
    string s;
    int n, t;
    cin >> n >> t >> s;
    int k,i,j;
    while(t--){
        for(i=0; i<s.length()-1; i++){
            j=i+1;
            if(s[i]=='B' && s[j]=='G'){
                s[i]='G';
                s[j]='B';
                i=j;
            }
        }
    }
    cout << s << endl;

    return 0;

}

CODEFORCES problem 131A cAPS lOCK

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int main()
 {
     string a;
     cin >> a;
     int i, count=0;
     for(i=0; i<a.size(); i++){
        if(a[i]==tolower(a[i]))
            count++;
     }
     if(count==0){
        for(i=0; i<a.size(); i++)
            a[i]=tolower(a[i]);
        cout<< a <<endl;
    }
     else if(count==1 && a[0]==tolower(a[0])){
        a[0]=toupper(a[0]);
        for(i=1; i<a.size(); i++)
            a[i]=tolower(a[i]);
        cout<< a <<endl;
     }
     else
        cout<< a <<endl;

     return 0;
 }

CODEFORCES problem 112A Petya and Strings

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int main()
 {
     string a;
     string b;
     int i;
     cin >> a >> b;
     for(i=0; i<a.size(); i++){
        a[i] = tolower(a[i]);
        b[i] = tolower(b[i]);
     }
     if(a.compare(b)==0)
        cout << "0" << endl;
     else{
        for(i=0;i<a.size();i++){
            if(a[i]<b[i]){
                cout << "-1" << endl;
                break;
            }
            if(a[i]>b[i]){
                cout << "1" << endl;
                break;
            }
        }
     }

     return 0;
 }

Wednesday, June 1, 2016

CODEFORCES problem 271A Beautiful Year

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

int main()
 {
     int year;
     int a,b,c,d;
     cin >> year;
     while(1){
        year++;
        a=year/1000;
        b=year/100;
        b=b%10;
        c=year/10;
        c=c%10;
        d=year%10;
        if(a!=b && a!=c && a!=d && b!=c && b!=d && c!=d)
            break;
     }
     cout<< year << endl;

     return 0;
 }

CODEFORCES problem 110A Nearly Lucky Number

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

int main()
 {
     string number;
     int count = 0;
     int i;
     cin >> number;
     for(i = 0; i < number.length(); i++){
         if(number[i] == '4' || number[i] == '7')
            count++;
     }
     if(count == 4 || count == 7)
         cout << "YES" << endl;
     else
         cout << "NO" << endl;

     return 0;
 }

Tuesday, May 31, 2016

CODEFORCES problem 231A Team

Problem link (Click here)

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int p,v,t;
    int solve = 0;
    for(int i=0;i<n;i++){
        cin>> p >> v >> t;
        if(p+v+t>1)
            solve++;
    }
    cout << solve << endl;

    return 0;
}

CODEFORCES problem 236A Boy or Girl

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <cstring>
#include<algorithm>

using namespace std;

int main()
{
    char word[100];
    cin>>word;
    int length=strlen(word);
    sort(word,word+length);
    int i, count = 0;
    for(i=0; i<length; i++){
        if(word[i]!=word[i+1]){
            count++;
        }
    }
    if(count%2==0)
        cout<<"CHAT WITH HER!"<<endl;

    else
        cout<<"IGNORE HIM!"<<endl;

    return 0;
}

CODEFORCES problem 546A Soldier and Bananas

Problem link (Click here)

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int k,n,w;
    int rate;
    int cost=0;
    int loan;
    cin>> k >> n >> w;
    for(int i=1; i<=w; i++){
        rate=i*k;
        cost=cost+rate;
    }
    loan=cost-n;
    if(loan<0)
        loan=0;
    cout<<loan<<endl;

    return 0;
}

VECTOR // STACK // QUEUE // SET

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>
#include <vector>
#include <conio.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>

using namespace std;

int main()
{
/*vector <int> v;
v.push_back(12);
v.push_back(2);
v.push_back(15);
v.push_back(20);

int length=v.size();
sort(v.begin(),v.end());
cout<<length<<endl;
for(int i=0; i<length; i++)
cout<<v[i]<<endl;
*/

/*
stack <char> st;
st.push('b');
st.push('c');
st.push('d');
st.push('e');
while (1){
int status = st.empty();
if(status == 1){
            break;
}
//char tm= st.top();
cout<<st.top()<< endl;
st.pop();
}

*/
    set <int> s;
    s.insert(10);
    s.insert(5);
    s.insert(10);
    s.insert(10);
    s.insert(5);
    s.insert(1);
    cout<< s.size()<<endl;




getchar();
getchar();
    return 0;
}

MISSING ELEMENT IN AN ARRAY

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>
#include <vector>
#include <conio.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>

using namespace std;

int main()
{
    vector <int> v;
    int n, i,temp;
    int miss;
    cin >> n;
    for(i=0; i<n; i++){
        cin>> temp;
        v.push_back(temp);
    }
    sort(v.begin(),v.end());
    int a=v[0];
    int b=0;

for(i=a; i<a+n; i++){
        if(v[b]!=i){
            miss=i;
            break;
        }
        b++;
}
    cout<< "Missing elemnet : "<<miss<< endl;

getchar();
getchar();
    return 0;
}





FINDING ARRAY IS SORTED

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>
#include <vector>
#include <conio.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>

using namespace std;

int main()
{
    vector <int> v;
    int n, i,temp;
    int count=0;
    cin >> n;
    for(i=0; i<n; i++){
        cin>> temp;
        v.push_back(temp);
    }

for(int i=0; i<n-1; i++){
        if(v[i]<v[i+1]){
            count++;
        }
}
if (count==n-1)
        cout<< "Sort"<<endl;
    else
        cout<< "Not Sort"<<endl;

getchar();
getchar();
    return 0;
}




WHETHER TWO ARRAY IS SAME USING VECTOR

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>
#include <vector>
#include <conio.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>

using namespace std;

int main()
{
    vector <int> v1;
    vector <int> v2;
    int n, i,temp;
    int count=0;;
    cin >> n;
    for(i=0; i<n; i++){
        cin>> temp;
        v1.push_back(temp);
    }
    for(i=0; i<n; i++){
        cin>> temp;
        v2.push_back(temp);
    }
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end());

for(int i=0; i<n; i++){
        if(v1[i]==v2[i]){
            count++;
        }
}
if (count==n)
        cout<< "Same"<<endl;
    else
        cout<< "Not Same"<<endl;

getchar();
getchar();
    return 0;
}



UNIQUE NUMBER USING SET

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>
#include <vector>
#include <conio.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>

using namespace std;

int main()
{

    set <int> s;
    int temp;
    int n;
    cin>>n;
    for(int i=0; i<n; i++){
        cin>>temp;
        s.insert(temp);
    }
    cout<< s.size()<<endl;

getchar();
getchar();
    return 0;
}


CODEFORCES problem 148A Insomnia cure

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>

using namespace std;

int main()
{
    int i,k,l,m,n,d;
    int count=0;
    cin>>k>>l>>m>>n>>d;
    for(i=1;i<=d;i++){
        if(i%k==0||i%l==0||i%m==0||i%n==0)
            count++;
    }
    cout<<count<<endl;

    return 0;
}

Monday, May 30, 2016

CODEFORCES problem 58/A Chat room

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>

using namespace std;

int main()
{
    string word="hello";
    string type;
    cin>>type;
    int length=type.length();
    int a=0;
    int i, count = 0;
    for(i=0; i<length; i++){
        if(type[i]==word[a]){
            a++;
            count++;
        }
    }
    if(count==5)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;

    return 0;
}

CODEFORCES problem 282/A Bit++

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>

using namespace std;

int main()
{
    string ch;
    int n;
    int x=0;
    cin >> n;
    while(n--){
        cin>>ch;
        if(ch[0]=='+' || ch[1]=='+'){
            x++;
        }
        else
            x--;
    }
    cout<< x <<endl;

    return 0;
}

CODEFORCES problem 122/A Lucky Division

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <string>

using namespace std;

int main()
{
    string num;
    int count=0,i;
    cin >> num;
    int a = atoi(num.c_str());
    int len=num.length();
    for(i=0; i<len; i++){
        if(num[i]=='4' || num[i]=='7')
            count++;
    }
    if(count==len)
        cout<<"YES"<<endl;
    else if(a%47==0)
        cout<<"YES"<<endl;
    else if (a%4==0 || a%7==0)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;


    return 0;
}

Sunday, May 29, 2016

CODEFORCES problem 96/A Football

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main()
{
    string team ;
    int i;
    int a=0, b=0;
    int length;
    cin >> team;
    length = team.size();
    for(i=0; i< length; i++){
        if(team[i]== '1'){
            a++;
            b=0;
        }
        else if(team[i]== '0'){
            b++;
            a=0;
        }
        if(a >= 7 || b >= 7)
            break;
    }

    if(a >= 7 || b >= 7)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;

    return 0;
}

CODEFORCES problem 467/A George and Accommodation

Problem link (Click here)

#include <iostream>
#include <cstdio>


using namespace std;

int main()
{
    int i, p, q, free, count=0;
    cin >> i;
    while(i--){
        cin >> p >> q;
        free = q-p;
        if(free>=2)
            count++;
    }
    cout << count << endl;

    return 0;
}

Wednesday, May 11, 2016

CODEFORCES problem 281/A Word Capitalization

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main()
{
    string stone ;
    int i=0;
    cin >> stone;
    stone[i]=toupper(stone[i]);

    cout << stone << endl;
    return 0;
}

CODEFORCES problem 266/A Stones on the Table

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main()
{
    string stone ;
    int count= 0;
    int n;
    cin >>n >> stone;
    for(int i = 1; i < n; i++ ){
        if(stone[i]==stone[i-1])
            count++;
    }

    cout << count << endl;
    return 0;
}

CODEFORCES problem 133/A HQ9+

Problem link (Click here)

#include<cstdio>
#include <iostream>
#include <Cstring>

using namespace std;

int main()
{
    char a[100];
    int flag=0;
    gets(a);
    for(int i=0; i<strlen(a); i++){
        if(a[i]== 'H' || a[i]== 'Q' || a[i]== '9'){
            cout<<"YES"<<endl;
            flag++;
            break;
        }
    }
    if(flag==0)
        cout<<"NO"<<endl;

    return 0;
}

CODEFORCES problem 339/A Helpful Maths

Problem link (Click here)

#include<cstdio>
#include <cstdlib>
#include <iostream>
#include <Cstring>
#include <algorithm>
using namespace std;

int main()
{
    char a[100];
    int b[100];
    gets(a);
    int i, j=0,len=0;
    int length = strlen(a);
    for(i=0 ; i<length ; i++){
        if(a[i]== '1'|| a[i]== '2' || a[i]=='3' ){
            int c = atoi(&a[i]);
            b[j]=c;
            j++;
            len++;
        }
    }
    sort(b,b+len);
    for(i=0;i<len;i++){
        cout<<b[i];
        if(i<len-1)
            cout<<"+";
    }
    cout<<endl;
    return 0;
}


CODEFORCES problem 50/A Domino piling

Problem link (Click here)

#include<cstdio>
#include<iostream>

using namespace std;

int main()
{
    int M,N;
    cin >> M >> N;
    int count=(M*N)/(2*1);
    cout << count << endl;
    return 0;
}

Monday, May 2, 2016

UVA problem 382 - Perfection

Problem link (Click here)

#include<cstdio>
#include <iostream>

using namespace std;

int main()
{
    int n,i,sum;
    printf("PERFECTION OUTPUT\n");
    while(scanf("%d",&n)==1){
        if(n==0)
            break;
        else
            {
                sum=0;
                for(i=1;i<=n/2;i++){
                    if(n%i==0)
                        sum=sum+i;
                }
                if(n<10)
                    printf("    ");
                else if(n<100)
                    printf("   ");
                else if(n<1000)
                    printf("  ");
                else if(n<10000)
                    printf(" ");
                if(sum==n)
                    printf("%d  PERFECT\n",n);
                else if(sum<n)
                    printf("%d  DEFICIENT\n",n);
                else
                    printf("%d  ABUNDANT\n",n);
            }
    }
    printf("END OF OUTPUT\n");

    return 0;
}

Sunday, May 1, 2016

UVA problem 12696 - Cabin Baggage

Problem link (Click here)

#include<cstdio>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int test_case, count = 0;
    float length, width, depth, weight;

    cin >> test_case;
    while(test_case--)
    {
        cin>>length>>width>>depth>>weight;
        if((length<=56.00&&width<=45.00&&depth<=25.00||(length+width+depth)<=125.00)&&weight<=7.00){
            cout<<"1"<<endl;
            count++;
        }
        else
            cout<<"0"<<endl;
    }
    cout<<count<<endl;
    return 0;
}

UVA problem 11764 - Jumping Mario

Problem link (Click here)

#include<cstdio>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int test_case, c = 0;
    cin >> test_case;
    while(test_case--)
    {
        int total_wall, wall_height, wall = 0;
        int high_jump = 0;
        int low_jump = 0;
        c++;
        cin >> total_wall;
        while(total_wall--)
        {
            cin >> wall_height;
            if (wall == 0)
                wall = wall_height;
             else if(wall>wall_height)
                {
                    low_jump++;
                    wall = wall_height;
                }
             else if (wall<wall_height)
             {
                 high_jump++;
                 wall = wall_height;
             }
        }
        printf("Case %d: %d %d\n",c,high_jump,low_jump);
    }
    return 0;
}

UVA problem 11984 - A Change in Thermal Unit

Problem link (Click here)

#include<cstdio>
#include <iostream>
using namespace std;
int main()
{
    int test_case, t = 0;
    float f,a,c,d;
    cin >> test_case;
    while(test_case--)
    {
        t++;
        cin >> c >> d;
        f = 9*c/5+d;
        a = f*5/9;
        printf("Case %d: %.2f\n",t,a);

    }
    return 0;
}

UVA problem 12403 - Save Setu

Problem link (Click here)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    int n,b;
    int report=0;
    string a;
    cin>>n;
    getchar();
    while(n--)
    {
        cin>>a;
        if(a=="donate"){
            cin>>b;
            report=report+b;
        }
        else
            printf("%d\n", report);
    }
    return 0;
}

UVA problem 11530 - SMS Typing

Problem link (Click here)

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int main()
{
    char a[1000];
    int i, n,test_case=0;
    scanf("%d",&n);
    getchar();
    while(n--){
        test_case++;
        int sum=0;
        gets(a);
        int l=strlen(a);
        for(i=0;i<l;i++){
            if(a[i]=='a')
                sum=sum+1;
            else if(a[i]=='b')
                sum=sum+2;
            else if(a[i]=='c')
                sum=sum+3;
            else if(a[i]=='d')
                sum=sum+1;
            else if(a[i]=='e')
                sum=sum+2;
            else if(a[i]=='f')
                sum=sum+3;
            else if(a[i]=='g')
                sum=sum+1;
            else if(a[i]=='h')
                sum=sum+2;
            else if(a[i]=='i')
                sum=sum+3;
            else if(a[i]=='j')
                sum=sum+1;
            else if(a[i]=='k')
                sum=sum+2;
            else if(a[i]=='l')
                sum=sum+3;
            else if(a[i]=='m')
                sum=sum+1;
            else if(a[i]=='n')
                sum=sum+2;
            else if(a[i]=='o')
                sum=sum+3;
            else if(a[i]=='p')
                sum=sum+1;
            else if(a[i]=='q')
                sum=sum+2;
            else if(a[i]=='r')
                sum=sum+3;
            else if(a[i]=='s')
                sum=sum+4;
            else if(a[i]=='t')
                sum=sum+1;
            else if(a[i]=='u')
                sum=sum+2;
            else if(a[i]=='v')
                sum=sum+3;
            else if(a[i]=='w')
                sum=sum+1;
            else if(a[i]=='x')
                sum=sum+2;
            else if(a[i]=='y')
                sum=sum+3;
            else if(a[i]=='z')
                sum=sum+4;
            else
                sum++;
            }
        printf("Case #%d: %d\n",test_case,sum);
    }

    return 0;
}

UVA problem 10783 - Odd Sum

Problem link (Click here)

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int i, j, n, a, b;
    int sum,test_case=0;
    cin>>n;
    for(i=0;i<n;i++){
        test_case++;
        sum=0;
        cin>>a>>b;
        if(a%2==0)
            a=a+1;
        if(b%2==0)
            b=b-1;
        for(j=a;j<=b;j=j+2)
            sum=sum+j;

        printf("Case %d: %d\n",test_case,sum);
    }
    return 0;
}

UVA problem 10921 - Find the Telephone

Problem link (Click here)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    int i;
    char a[1000];
    while(scanf("%s",&a)!=EOF)
    {
        int b=strlen(a);
        for(i=0; i<b; i++){
            if(a[i]=='A'||a[i]=='B'||a[i]=='C')
                printf("2");
            else if(a[i]=='D'||a[i]=='E'||a[i]=='F')
                printf("3");
            else if(a[i]=='G'||a[i]=='H'||a[i]=='I')
                printf("4");
            else if(a[i]=='J'||a[i]=='K'||a[i]=='L')
                printf("5");
            else if(a[i]=='M'||a[i]=='N'||a[i]=='O')
                printf("6");
            else if(a[i]=='P'||a[i]=='Q'||a[i]=='R'|a[i]=='S')
                printf("7");
            else if(a[i]=='T'||a[i]=='U'||a[i]=='V')
                printf("8");
            else if(a[i]=='W'||a[i]=='X'||a[i]=='Y'||a[i]=='Z')
                printf("9");
            else
                printf("%c",a[i]);
        }
        printf("\n");
    }

    return 0;
}

UVA problem 12250 - Language Detection

Problem link (Click here)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    int cases=0;
    string str;
    while(1)
    {
        cases++;
        cin>>str;
        if(str=="HELLO")
            printf("Case %d: ENGLISH\n", cases);
        else if( str=="HOLA")
            printf("Case %d: SPANISH\n", cases);
        else if( str=="HALLO")
            printf("Case %d: GERMAN\n", cases);
        else if( str=="BONJOUR")
            printf("Case %d: FRENCH\n", cases);
        else if( str=="CIAO")
            printf("Case %d: ITALIAN\n", cases);
        else if( str=="ZDRAVSTVUJTE")
            printf("Case %d: RUSSIAN\n", cases);
        else if( str=="#")
            break;
        else
            printf("Case %d: UNKNOWN\n", cases);
    }
    return 0;
}

Saturday, April 30, 2016

UVA problem 12577 - Hajj-e-Akbar

Problem link (Click here)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    int cases=0;
    string str;
    while(1)
    {
        cases++;
        cin>>str;
        if(str=="*")
            break;
        else if( str=="Hajj")
            printf("Case %d: Hajj-e-Akbar\n", cases);
        else
            printf("Case %d: Hajj-e-Asghar\n", cases);
    }
    return 0;
}

UVA problem 11799 - Horror Dash

Problem link (Click here)

#include<cstdio>
#include<iostream>

using namespace std;

int main()
{
  int test_case,n,i,a,c=0;

  cin >> test_case;
  while(test_case--)
  {
      c++;
      int max=0;
      cin>>n;
      for(i=0;i<n;i++){
            cin>>a;
            if(a>max)
                max=a;
      }
       cout <<"Case "<< c <<": "<<max<<endl;
  }
  return 0;
}




UVA problem 10082 - WERTYU

Problem link (Click here)

#include<stdio.h>
#include<string.h>
#include<iostream>

using namespace std;

int main()
{
int l,i;
char s[100000];
while(gets(s))
    {
    l=strlen(s);
    for(i=0;i<l;i++)
        {
            if(s[i]=='W')
                printf("Q");
            else if (s[i]=='E')
                printf("W");
            else if (s[i]=='R')
                printf("E");
            else if (s[i]=='T')
                printf("R");
            else if (s[i]=='Y')
                printf("T");
            else if (s[i]=='U')
                printf("Y");
            else if (s[i]=='I')
                printf("U");
            else if (s[i]=='O')
                printf("I");
            else if (s[i]=='P')
                printf("O");
            else if (s[i]=='[')
                printf("P");
            else if (s[i]==']')
                printf("[");
            else if (s[i]=='\\')
                printf("]");
            else if (s[i]=='S')
                printf("A");
            else if (s[i]=='D')
                printf("S");
            else if (s[i]=='F')
                printf("D");
            else if (s[i]=='G')
                printf("F");
            else if (s[i]=='H')
                printf("G");
            else if (s[i]=='J')
                printf("H");
            else if (s[i]=='K')
                printf("J");
            else if (s[i]=='L')
                printf("K");
            else if (s[i]==';')
                printf("L");
            //else if (s[i]==''')
            else if (s[i]=='X')
                printf("Z");
            else if (s[i]=='C')
                printf("X");
            else if (s[i]=='V')
                printf("C");
            else if (s[i]=='B')
                printf("V");
            else if (s[i]=='N')
                printf("B");
            else if (s[i]=='M')
                printf("N");
            else if (s[i]==',')
                printf("M");
            else if (s[i]=='.')
                printf(",");
            else if (s[i]=='/')
                printf(".");
            else if (s[i]=='1')
                printf("`");
            else if (s[i]=='2')
                printf("1");
            else if (s[i]=='3')
                printf("2");
            else if (s[i]=='4')
                printf("3");
            else if (s[i]=='5')
                printf("4");
            else if (s[i]=='6')
                printf("5");
            else if (s[i]=='7')
                printf("6");
            else if (s[i]=='8')
                printf("7");
            else if (s[i]=='9')
                printf("8");
            else if (s[i]=='0')
                printf("9");
            else if (s[i]=='-')
                printf("0");
            else if (s[i]=='=')
                printf("-");
            else if (s[i]==' ')
                printf(" ");
            else
                printf(";");
        }
    printf("\n");
    }
return 0;
}

UVA problem 12372 - Packing for Holiday

Problem link (Click here)

#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
  int test_case, L, W, H, c = 0;
  cin >> test_case;
  while(test_case--)
  {
      cin >> L>>W>>H;
      c++;
      if(L>20||W>20||H>20)
        cout <<"Case "<< c <<": "<<"bad"<<endl;
      else
        cout <<"Case "<< c <<": "<<"good"<<endl;
  }
  return 0;
}


UVA problem 272 - TEX Quotes

Problem link (Click here)

#include<iostream>
#include<stdio.h>
#include <string.h>

using namespace std;

int main()
{
  char s[1000];
  int count = 0;
  int a;
  while (gets(s))
  {
      a=strlen(s);
      for(int i = 0; i < a; i++)
      {
          if(s[i] == '"'){
              count++;
              if(count%2==0)
                printf("''");
              else
                printf("``");
          }
          else
            printf("%c",s[i]);
      }
      printf("\n");
  }
  return 0;
}


UVA problem 1124 - Celebrity jeopardy

Problem link (Click here)

#include <cstdio>

using namespace std;
int main()
{
    char a[100];

    while(gets(a))
    puts(a);
    return 0;
}



Friday, April 29, 2016

CODEFORCES problem 118B-Present from Lena

Problem link (Click here)

#include<iostream>

using namespace std;

int main()
{
     int i, j, k, l, n;
     cin >> n;
     for(i=0;i<=n;i++)
        {
            for(j=i;j<n;j++)
                cout<<"  ";
            for(k=0;k<=i;k++){
                    cout << k;
                    if(i!=0)
                        cout << " ";
                 }
             for(k=i-1;k>=0;k--){
                cout<<k;
                 if(k>0)
                    cout<<" ";
            }
            cout<<"\n";
         }
      for(i=n-1;i>=0;i--)
      {
        for(j=n-1;j>=i;j--)
          cout<<"  ";
        for(k=0;k<=i;k++){
                cout << k;
                if(i!=0)
                    cout << " ";
             }
         for(k=i-1;k>=0;k--){
            cout<<k;
             if(k>0)
                cout<<" ";
        }
        cout<<"\n";
      }
     return 0;
}



UVA problem 11172 – Relational Operator

Problem link (Click here)

#include<iostream>
using namespace std;

int main ()
{
    int i, t, a, b;
    cin>>t;
    for(i=0; i<t; i++){
         cin>>a>>b;
        if(a<b)
            cout<<'<'<<endl;
         else if (a>b)
            cout<<'>'<<endl;
        else
            cout<<'='<<endl;
    }
    return 0;
}


UVA problem 10300 – Ecological Premium

Problem link (Click here)

#include <stdio.h>
int main()
{
    long long a,b,c,m,n,i;

    scanf("%lld",&m);
    while(m--){
        scanf("%lld",&n);
        long long sum=0;
        for(i=0;i<n;i++){
            scanf("%lld %lld %lld",&a, &b, &c);
            sum=sum+a*c;
        }
        printf("%lld\n",sum);
    }
    return 0;
}


UVA problem 100 – The 3n + 1

Problem link (click here)

#include <stdio.h>
#include <algorithm>

using namespace std;
int main()
{
      int i, j;
      int c,d;
      while(scanf("%d",&i)!= EOF){
            int max=0;
            scanf("%d",&j);
            c=i;
            d=j;
            if(i>j){
                  swap(i,j);
            }
            int k,n;
            for(k=i;k<=j;k++){
                  int lenth=1;
                  n=k;
                  while(n!=1){
                       if (n%2!=0){
                            n=3*n+1;
                       }
                       else
                            n=n/2;
                       lenth++;
                  }
                  if(max<lenth){
                       max=lenth;
                  }
             }
             printf("%d %d %d\n",c,d,max);
       }
       return 0;
}



UVA problem 10055 – Hashmat the Brave Warrior

Problem link (Click here)

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
      long long i, j, d;
      while(cin >> i){
           cin >> j;
           if(i>j)
                swap(i,j);
           cout << j-i <<endl;
      }
      return 0;
}