Tuesday 18 August 2015

Binary Search in java

Time Complexity for Binary Search will be Log(n).  But the main Condition is, the inputted elements in the array must be sorted (ascending order).



import java.util.*;

class BSearch
{
  public static void main(String[] args)
  {
  Scanner s=new Scanner(System.in);
  int n=s.nextInt();
  int a[]=new int[n];
 
  for(int i=0;i<n;i++)
 a[i]=s.nextInt();
 
  System.out.println("enter the element to be find");
  int f=s.nextInt();
 
  int k=bs(a,0,n-1,f);
  if(k==-1)
 System.out.println("the element not present");
  else
      System.out.println("the element is at index  "+(k+1));
  }
 
 
  static int bs(int a[],int st,int en,int f)
  {
 if(en>=st)
 {
  int mid=(st+en)/2;

  if(a[mid]==f)
 return mid;
  else if(a[mid]<f)
 return bs(a,mid+1,en,f);
  else
 return bs(a,0,mid-1,f);
 }

 return -1;
  }
}

Sunday 16 August 2015

Single Link List in java






import java.util.*;

class Intnode
{
  public int data;
  public Intnode next;
 
  Intnode()
  {
  this.next=null;
  }
}



public class Linklist
{
  public static void main(String[] args)
  {
   Scanner s = new Scanner(System.in);
   Intnode start= new Intnode();
   Intnode temp=start;
 
    System.out.println("Please enter data in the node");
    int k=s.nextInt();
start.data=k;

while(true)
{
 System.out.println("do u want to make more such node ? press y/n");
  char a= s.next().charAt(0);
 
  if(a=='n')
   break;

  Intnode temp2= new Intnode();
  System.out.println("Please enter data in the node");
       k=s.nextInt();
  temp2.data=k;
 
temp.next=temp2;
temp=temp.next;
}

temp =start;
while(temp!=null)
{
 System.out.print(temp.data+" ->");
 temp=temp.next;
}

  }

}

Quick Sort in java








import java.util.*;

class qsort
{

 public static void main(String args[])
 {
Scanner s=new Scanner(System.in);
int n=s.nextInt();

int a[]=new int [n];

for(int i=0;i<n;i++)
a[i]=s.nextInt();

quick_sort(a,0,n-1);

for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
 }


  static void quick_sort(int a[],int st,int en)
  {
 if(st<en)
 {
       int k=pivot_ele(a,st,en);
  quick_sort(a,st,k);
  quick_sort(a,k+1,en);
 }
  }


  static int pivot_ele(int a[],int st,int en)
  {
 int i,j,pivot;
pivot=st;
i=st+1;
j=en;

while(i<=j)
{
while(a[i]<a[pivot] && i<en) i++;

while(a[j]>a[pivot]) j--;

if(i<=j)
{
int t=a[i];
a[i]=a[j];
a[j]=t;
i++;
j--;
}
}  ///while close

 int t=a[j];
a[j]=a[st];
a[st]=t;

return j;
    }
}

Saturday 15 August 2015

MERGE_SORT in java

import java.util.*;



class msort
{


 public static void main(String args[])
 {
Scanner s=new Scanner(System.in);
int n=s.nextInt();

        int a[]=new int [n];

for(int i=0;i<n;i++)
a[i]=s.nextInt();

merge_sort(a,0,n-1);

for(int i=0;i<n;i++)
System.out.print(a[i]+" ");

 }

  static void merge_sort(int a[],int st,int en)
  {
 if(st<en)
 {
       int k=(st+en)/2;
  merge_sort(a,st,k);
  merge_sort(a,k+1,en);
  merge(a,st,k,en);
 }
  }
 
    static void merge(int array[],int lowerIndex,int middle,int higherIndex)
{
int tempMergArr[]=new int[higherIndex-lowerIndex+1];

int k=0;
       for (int i = lowerIndex; i <= higherIndex; i++)
            tempMergArr[k++] = array[i];

        int i = lowerIndex;
int r=i;
        int j = middle + 1;
         k = lowerIndex;

        while (i <= middle && j <= higherIndex)
{
            if (tempMergArr[i-r] <= tempMergArr[j-r]) {
                array[k] = tempMergArr[i-r];
                i++;
            } else {
                array[k] = tempMergArr[j-r];
                j++;
            }
            k++;
        }


        while (i <=middle) {
            array[k] = tempMergArr[i-r];
            k++;
            i++;
        }

 while (j <=higherIndex) {
            array[k] = tempMergArr[j-r];
            k++;
            j++;
        }

    }
}

Similarity between Merge_sort and Quick_sort


Similarity between Merge_sort  and  Quick_sort

In quick sort , the main thing is to choose the PIVOT  element.

In Merge sort , the main thing is merging part.



Wednesday 12 August 2015

java program for finding gcd ,lcm ,xor and addition (without using operators )

import java.util.*;

class LCM
{
 public static void main(String args[])
 {
   Scanner s=new Scanner(System.in);
   int k1 = s.nextInt();
   int k2= s.nextInt();
 
   int gcd=hcf(k1,k2);
   System.out.println(" hcf of these two number is "+gcd);
 
   int lcm=(k1*k2)/gcd;
   System.out.println(" lcm of these two number is "+lcm);
 
   int xor=k1^k2;
   System.out.println(" xor of these two number is " +xor);
 
   int addd=Add(k1,k2);
   System.out.println("addition method without using arthemetic operators is "+addd);
 
 
 }

 static int hcf(int a,int b)
 {
int r=a%(b);
int diviser=a;
int divident=b;
int quotient;
while(r>0)
{
  quotient=divident/diviser;
r=divident%diviser;
divident=diviser;
diviser=r;
}
return divident;
 }

 /*
 Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits.
Above is simple Half Adder logic that can be used to add 2 single bits. We can extend this logic for integers. If x and y don’t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result.
*/

  static int Add(int x, int y)
  {
    // Iterate till there is no carry
    while (y != 0)
    {
        // carry now contains common set bits of x and y
        int carry = x & y;

        // Sum of bits of x and y where at least one of the bits is not set
        x = x ^ y;

        // Carry is shifted by one so that adding it to x gives the required sum
        y = carry << 1;
    }
    return x;
  }


}

3 ways to take inputs in java (Scanner , console , BufferedReader )

import java.util.*;

import java.io.Console;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;



class Input
{
 public static void main(String []args) throws IOException
 {
  Scanner a = new Scanner(System.in); // creating object of scanner class
  String s=a.nextLine();              // way to take input through Scanner(nextLine for String)
  System.out.println(s);
  int i=a.nextInt();                  // way to take input through Scanner(nextInt for int)
  System.out.println(i);


  String str=System.console().readLine();  // way to take input from console
  System.out.println(str);



  BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); // creating object of BufferedReader
  String str1 = in.readLine();      
  System.out.println(str1);
  int num = Integer.parseInt(in.readLine());
  System.out.println(num);

 }
}

Tuesday 11 August 2015

A sample Python Program for Reference work ( Life Universe Everything )

  1. def main():
  2. while True:
  3. w=input()
  4. if w==42:
  5. break
  6. else :
  7. print w
  8. main()
-----------------------------------------------------------------------------------------=============================================================================================

I. Yet another A + B codeforces

def main():

            a=int(input())
            b=int(input())
            c=int(input())
           

            if a+b == c:
                print("YES")
            elif a+a == c:
                print("YES")
            elif b+b == c:
                print("YES")
            elif 2*c == b:
                print("YES")
            elif 2*a == b:
                print("YES")
            elif a+c == b:
                print("YES")
            elif 2*c == a:
                print("YES")
            elif 2*b == a:
                print("YES")
            elif c+b == a:
                print("YES")
            else:
                print("NO")

main()

A Sample PHP program for reference work( taking input ,creating arrays )

<?php fscanf(STDIN, "%d\n", $test); while($test--) { $ooo=array(); for($i=0;$i<26;$i++) $ooo[$i]=0; fscanf(STDIN, "%s\n", $raa); $qwe=0; $p=strlen($raa); for($i=0;$i<$p;$i++) { $w=ord($raa[$i]); $qwe= $w-97; $ooo[$qwe]=$ooo[$qwe]+1; } $f=0; $c=0; for($i=0;$i<26;$i++) { if($ooo[$i]%2!=0) { $f=1; $c++; } } if($f==0) { echo"Yes"; } else if($f==1 && strlen($raa)%2==1 && $c==1) { echo "Yes"; } else { echo "No"; } echo "<br>"; }

Uploading and Running Lambda function in AWS

Main.go package main import ( "fmt" "encoding/json" "log" "github.com/aws/aws-lambda-g...