Thursday, 5 January 2017

Using Divide And Conqure Technique finding maximum and minimum value of an array

Language C++

/************
done!
************/
#include<bits/stdc++.h>

using namespace std;

int a[1000];

void DAndCMaxMin(int i,int j,int &maxx,int &minx)
{
    int min1,max1;

    if(i==j){/**smallp*/

    maxx=a[i];

    minx=a[i];

    return;

    }

    if( i==j-1 )/**smallp*/

    {

        if(a[i]<a[j]){

            maxx=a[j];

            minx=a[i];

        }

        else

        {

            maxx=a[i];

            minx=a[j];

        }

        return;

    }

    else

    {

        int mid=(i+j)/2;

        /************

        dividing part

        ************/

        DAndCMaxMin(i,mid,maxx,minx);

        DAndCMaxMin(mid+1,j,max1,min1);

        /*************

        combining part

        *************/

        if(maxx<max1)

            maxx=max1;

        if(minx>min1)

            minx=min1;

    }
}

int main()

{

    int n;

    while(cin>>n)

    {

        int maxx=0,minx=0;

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

        DAndCMaxMin(0,n-1,maxx,minx);

        cout<<"Maximum value is "<<maxx<<endl;

        cout<<"Minimum value is "<<minx<<endl;

    }
    return 0;
}

Sunday, 1 January 2017

Java:Sorting an array

//This program will take some random values and sort them in ascending order

import static java.lang.Math.abs;
import java.util.*;

public class SortingAnArray {

    public static void main(String[] args) {

        Scanner obj=new Scanner(System.in);
        int n;
        n=obj.nextInt();
        int a[]=new int[n];
        Random ob=new Random();
        for(int i=0;i<n;i++)
        {
            a[i]=ob.nextInt()%9;
            if(a[i]<0)
                a[i]=abs(a[i]);
        }
        System.out.println("Elements before sorting");
        for(int i:a)
        {
            System.out.printf("%d ",i);
        }
        System.out.println();
        Arrays.sort(a);
        System.out.println("Elements after sorting");
        for(int i:a)
        {
            System.out.printf("%d ",i);
        }
        System.out.println();
    }
   
}

Saturday, 29 October 2016

STL:<stack> container

For using stack container we have to include <stack> in our program.We already know that stack is last in first out system.Mainly stack has two operations 1.push operation & 2.pop operation.
There are 5 member functions in the <stack> container.They are:
Suppose x is a stack
1.x.push(value);//for inserting a value in stack.
2.x.pop();//for removing the top element.
3.x.top();returns the top element of the stack.
4.x.empty();returns a boolean value that the is empty or not.
5.x.clear();for removing the entire stack.

Thursday, 27 October 2016

SORTING A LINKED LIST

/*******************
linked list
bubble sort
complexity:O(n^2)
*******************/
#include <bits/stdc++.h>

using namespace std;

class linked_list
{
    public:
    int data;
    class linked_list *next;
};
typedef linked_list node;

void show(node *p){

    while(p){
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    return;
}
void sort(node *p)
{
    while(p->next){
        node *test=p->next;
        while(test){
            if(test->data<p->data)
                swap(test->data,p->data);
            test=test->next;
        }
        p=p->next;
    }
}
void create(node *p){

    char c;
    scanf("%d%c", &p->data, &c);

    if(c=='\n')
        p->next=NULL;
    else
    {
        p->next=new node;
        create(p->next);
    }
    return;
}

int main(){

    bool x;
    while(cin>>x){

        node *head;
        head=new node;
        create(head);
        cout<<"list before sorting\n";
        show(head);
        sort(head);
        cout<<"list after sorting\n";
        show(head);
    }
    return 0;
}

Saturday, 27 August 2016

Stack by linked list

//here is a program that will create a linked stack

#include<iostream>
#include<stdio.h>
using namespace std;
struct linked_lists
{
    int data;
    struct linked_lists *next;
};
typedef struct linked_lists node;
void create(node *p);
void show(node *p);
node* push(node *p);
node* pop(node *p);
int main()
{
    node *head;
    head=new node;
    create(head);
    show(head);
    int c;
    bool x=true;
    while(x)
    {
        cout<<"1 push"<<endl;
        cout<<"2 pop"<<endl;
        cout<<"3 exit"<<endl;
        cin>>c;
        switch(c)
        {
        case 1:
            head=push(head);
            show(head);
            break;
        case 2:
            head=pop(head);
            show(head);
            break;
        case 3:
            x=false;
            break;

        }
    }
    return 0;
}
void create(node *p)
{
    char c;
    scanf("%d%c",&p->data,&c);
    if(c=='\n')
    {
        p->next=NULL;
    }
    else
    {
        p->next=new node;
        create(p->next);
    }
    return;
}
void show(node *p)
{
    if(p==NULL)
    {
        cout<<"empty"<<endl;
    }
    while(p)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    return;
}
node* push(node *p)
{
    cout<<"value:";
    node *n,*h;
    h=p;
    n=new node;
    cin>>n->data;
    n->next=NULL;
    if(p==NULL){
        p=n;
        return p;
    }
    while(p->next)
    {
        p=p->next;
    }
    p->next=n;
    return h;

}
node* pop(node *p)
{
    node *h;
    h=p;
    //if(p==NULL)
    //{
    //    return NULL;
    //}
    if(p->next==NULL)
    {
        p=p->next;
        return p;
    }
    while(p->next->next!=NULL)
    {
        p=p->next;
    }
    delete p->next;
    p->next=NULL;
    return h;
}

CODEFORCES 709A-Juicer Solution


  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int n,b,d;
  6.     cin>>n>>b>>d;
  7.     int a[n],waste=0,count=0;
  8.     for(int i=0;i<n;i++){
  9.         cin>>a[i];
  10.         if(a[i]>b)
  11.             continue;
  12.         waste+=a[i];
  13.         if(waste>d)
  14.         {
  15.             count++;
  16.             waste=0;
  17.         }
  18.     }
  19.     cout<<count<<endl;
  20.     return 0;
  21. }

Sunday, 21 August 2016

Lightoj 1387-Setu solution



  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4.      int t;
  5.      cin>>t;
  6.      for(int i=1;i<=t;i++){
  7.           int n;
  8.           cin>>n;
  9.           int money=0;
  10.           cout<<"Case "<<i<<":\n";
  11.           while(n--){
  12.               string x;
  13.               cin>>x;
  14.               if(x[0]=='d')
  15.                  {
  16.                     int g;
  17.                     cin>>g;
  18.                     money+=g;
  19.                   }
  20.                else
  21.                  cout<<money<<endl;
  22.          }
  23.      }
  24.      return 0;
  25. }

Kruskal Algorithm

Minimum Cost Spanning Tree By Kruskal Algorithm using priority_queue in c++ #include<bits/stdc++.h> using namespace std; typedef p...