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