Tuesday, 8 November 2016

Hackerrank , Project Euler #5: Smallest multiple

Project Euler #5: Smallest multiple


from fractions import gcd
def lcm(numbers):
    return reduce(lambda x, y: (x*y)/gcd(x,y), numbers)
t=input()
for i in range(0,t):
    print lcm(range(1,input()+1))

Hackerrank , Project Euler #4: Largest palindrome product

Project Euler #4: Largest palindrome product


# Enter your code here. Read input from STDIN. Print output to STDOUT
t=input()
for i in range(0,t):
    no=input()
    for k in range(no-1,101100,-1):
        if str(k)==str(k)[::-1]:
            lst = [k for e in range(100,999) if k%e==0 and len(str(k/e))==3]
            if lst :
                print max(lst)
                break

Hckerrank , Project Euler #3: Largest prime factor

Project Euler #3: Largest prime factor


# Enter your code here. Read input from STDIN. Print output to STDOUT
t=input()
def largest_prime_factor(n):
    i = 2
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
    return n

for j in range(0,t):
    n=input()
    print largest_prime_factor(n)

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