Friday, 28 October 2016

Check for pair in A[] with sum as x

Given an array A[] and a number x, check for pair in A[] with sum as x 


##include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
@author Anurag
*/
int main() {
  // create a vector to store int
  int myints[] = {32,71,12,45,26,80,53,38};
  vector < int > vec(myints, myints + 8);
  sort(vec.begin(), vec.begin() + 8);
  vector < int > ::iterator f = vec.begin();
  vector < int > ::iterator l = vec.begin() + 7;
  int x = 50;
  while (f != vec.end()) {
    if (( * f) + ( * l) > x) {
      l--;
    } else if (( * f) + ( * l) < x) {
      f++;
    } else {
      cout << * f << "," << * l;
      break;
    }
  }
  return 0;
}}

Sunday, 17 July 2016

Factorial of a No. using Dynamic Programming

 Following program show how to find out the factorial of a No. using Dynamic Programming.

Factorial of No.   :
 
class Factorial
{
  public static void main(String[] args) {
    int n=8;
    long arr[] = new long[n+1];
    for(int i=0 ;i<n+1;i++)
      arr[i]=0L;
    System.out.println("Factorial is :: "+fact(n,arr));
  }
  static long fact(int n, long arr[])
  {
    if (n==0 || n==1)
      return 1;
    else if(arr[n]!=0)
      return arr[n];
    else
      {
        arr[n]=fact(n-1, arr)*n;
        return arr[n];
      }
  }
}

Friday, 15 July 2016

Counting Sort

Following code show counting sort implementation in Java .

Counting Sort :   

class CountingSort
{
  public static void main(String[] args) {
    char arr[] = {'g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's'};
    sort(arr);
    System.out.print("Sorted character array is ");
        for (int i=0; i<arr.length; ++i)
            System.out.print(arr[i]);
  }
  static void sort(char arr[])
  {
    int n = arr.length;
    int count[] = new int[256];
    for (int i=0; i<256; ++i)
        count[i] = 0;
    for (int i=0; i<n; ++i)
        ++count[arr[i]];
    for (int i = 0,j=0; i<n;j++)
      {  while(count[j]>0)
         {
           arr[i] = (char)j;
           --count[j];
           i++;
         }
      }
  }

}
 

Thursday, 14 July 2016

Hackerrank , Project Euler #1: Multiples of 3 and 5

Here you can find solutions for  the famous Euler Project's Problems . Questions can find on following link https://www.hackerrank.com/contests/projecteuler/challenges.

 Project Euler #1: Multiples of 3 and 5     :

# Enter your code here. Read input from STDIN. Print output to STDOUT
t= input()
for i in range(0,t):
    no=input()
    n=(no-1)/3
    sum_3=(n*(2*3+3*(n-1))/2)
    n=(no-1)/5
    sum_5=(n*(2*5+5*(n-1))/2)
    n=(no-1)/15
    sum_15=(n*(2*15+15*(n-1))/2)
    print sum_3+sum_5-sum_15

Wednesday, 13 July 2016

Selection Sort

Selection sort is a simple sorting algorithm.Smallest element is selected from the unsorted array and swapped with the leftmost element and that element becomes part of sorted array. This process continues moving unsorted array boundary by one element to the right.

 This algorithm is not suitable for large data sets as its average and worst case complexity are of O(n2) where n are no. of items.

Following  program shows selection sort algorithm in JAVA.

Selection Sort : 
public class SelectionSort {

    public static void main(String args[]) {
        int array[] = { 5, 2, 7, 4, 9, 0 };
        array = selectionSort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
        }
    }

    private static int[] selectionSort(int array[]) {
        for (int i = 0; i < array.length; i++) {
            for (int j = i; j < array.length; j++) {
                if (array[i] > array[j]) {
                    int temp;
                    temp = array[j];
                    array[j] = array[i];
                    array[i] = temp;

                }
            }
        }
        return array;
    }

}

Tuesday, 12 July 2016

BubbleSort

This Java bubble sort example shows how to sort an array of int using bubble sort algorithm. Bubble sort is the simplest sorting algorithm.

BubbleSort  :

package sorting;

public class BubbleSort {

    public static void main(String args[]) {
        int array[] = { 5, 2, 7, 4, 9, 0 };
        array = bubbleSort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
        }
    }

    private static int[] bubbleSort(int array[]) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp;
                    temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;

                }
            }
        }
        return array;
    }

}
 

QuickSort

Following program show Quicksort Algorithm in Java.

QuickSort:

public class QuickSort {
    public static void main(String args[]) {
        int array[]={9,2,5,1,7,1,2,9,5,4};
        quickSort(array,0,array.length-1);
        for(int e : array)
        {
            System.out.println(""+e);
        }
    }

    static void quickSort(int [] arr , int left ,int right)
    {
        if(left<right)
        {
        int p = partition(arr,left,right);
        quickSort(arr,left,p-1);
        quickSort(arr,p+1,right);
        }
    }

static int partition(int [] arr,int l, int h)
{
    int pivot=arr[h];
    int i=l-1;
    for(int j=l;j<h;j++)
    {
        if(arr[j]<=pivot)
        {
            swap(arr,i+1,j);
            i++;
        }
    }
swap(arr,i+1,h);
return i+1;
}
static void swap(int arr[] ,int i , int j)
{
    int temp= arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
}

}