Wednesday, 7 January 2015

Given a large array of unsigned ints, quickly find two who's sum is 10

import java.util.*; public class AdditionDemo { public static void main(String[] args) { HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); int arr[]=new int[9]; for(int i=0;i<9;i++) arr[i]=i+1; for (int i:arr) { if(hm.get(i)==null) { if(hm.get(10-i)!=null) hm.put((10-i),i); else hm.put(i,0); } } System.out.println(hm); } }

Illustrate with an example the difference between deep and shallow copy in java?

import java.io.*; import java.util.*; public class Employee implements Cloneable,Serializable { int eno; String ename; Address addr; public Employee(int en,String name,Address a) { eno=en; ename=name; addr=a; } public Object clone() throws CloneNotSupportedException { Object o=null; try{ ByteArrayOutputStream baos=new ByteArrayOutputStream(); ObjectOutputStream oos=new ObjectOutputStream(baos); oos.writeObject(this); ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois=new ObjectInputStream(bais); o=ois.readObject(); } catch(Exception e) { System.out.println(e.getMessage()); } return o; } } Address.java import java.io.*; import java.util.*; public class Address implements Cloneable,Serializable { String streetno; String city; public Address(String s,String c) { streetno=s; city=c; } } Test.java public class Test { public static void main(String[] args) { try { Address a1=new Address("123","Hyderabad"); Employee e1=new Employee(1,"Neil",a1); Employee e2=(Employee)e1.clone(); System.out.println(e2.addr.city); e1.addr.city="Mumbai"; e1.ename="shan"; System.out.println(e2.addr.city); System.out.println(e2.ename); } catch(Exception e) { System.out.println(e.getMessage()); } } }

change the value of the i without changing code of the main function, assign 20 to i ?

int fun() { //write a code here /* method 1 #define fun() i=20 */ //method 2 int j; int *ptr; ptr=&j; for (;*(ptr)!=10;ptr++); *ptr=20; } int main() { int i=10; fun(); //substitute i=20 printf("%d",i); }

Find the unique number that is present only once in array while all the others are present three times.

Example: 2,3,5,1,2,2,5,3,5,3
Answer : 1 as 2,3,5 are repeated three times Complexity should be better than O(nlogn)

#include <stdio.h> int dnr(int arr[],int n) { int i,j,count=0,result=0; int bit=1; for (i=0;i<32;i++) { count=0; for (j=0;j<n;j++ ) { if(arr[j] & bit) count++; } count%=3; if(count) result=result | bit; bit=bit<<1; } return result; } void main() { int a[]={17,14,11,14,17,21,17,21,21,14}; printf("%d\n",dnr(a,10)); }

Sunday, 7 December 2014

AVL Tree program in C

#include <stdio.h> #include<malloc.h> typedef enum{FALSE,TRUE}booleanx; struct anode *avlinsert(int,struct anode*,booleanx*); struct anode *avlsearch(struct anode*,int); void avldisplay(struct anode*,int); void avlinorder(struct anode*); struct anode { int info; //data int balance;//balance factor struct anode *lchild; struct anode *rchild; }; void main() { booleanx ht_inc; int info; int choice; system("cls"); struct anode *root=(struct anode*)malloc(sizeof(struct anode)); root=NULL; while(1) { printf("\n1. Insert\n"); printf("\n2. Display\n"); printf("\n3. Return to tree menu\n"); printf("Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: printf("\n Enter the value to be inserted:"); scanf("%d",&info); if(avlsearch(root,info)==NULL) root=avlinsert(info,root,&ht_inc); else printf("\n Duplicate value ignored\n"); break; case 2: if(root=NULL) { printf("\n Tree is empty\n"); continue; } printf("\n Tree is:\n"); avldisplay(root,1); printf("\n\n"); printf("\n Inorder traversal is:"); avlinorder(root); printf("\n"); break; case 3: return; default : printf("\nWrong choice\n"); }//end of switch }//end of while }//end of main struct anode *avlsearch(struct anode*ptr,int info) { if(ptr!=NULL) if(info<ptr->info) ptr=avlsearch(ptr->lchild,info); else if(info>ptr->info) ptr=avlsearch(ptr->rchild,info); return ptr; }//end of search struct anode *avlinsert(int info,struct anode*pptr,booleanx*ht_inc) { struct anode *aptr; struct anode *bptr; if(pptr==NULL) { pptr=(struct anode*)malloc(sizeof(struct anode)); pptr->info=info; pptr->lchild=NULL; pptr->rchild=NULL; pptr->balance=0 *ht_inc=true; return pptr; } if (info<pptr->info) { pptr->lchild=avlinsert(info,pptr->lchild,ht_inc); if(*ht_inc==true) { switch(pptr->balance) { case 1://right heavy; pptr->balance=0 *ht_inc=false; break; case 0: //balanced pptr->balance=1; break; case -1://left heavy aptr=pptr->lchild; if(aptr->balance==-1) { printf("left to left rotation\n"); pptr->lchild=aptr->rchild; aptr->rchild=pptr; pptr->balance=0; aptr->balance=0; pptr=aptr; } else { printf("left to right rotation\n"); bptr=aptr->rchild; aptr->rchild=bptr->lchild; bptr->lchild=aptr; pptr->lchild=bptr->rchild; bptr->rchild=pptr; if (bptr->balance==1) pptr->balance=-1; else pptr->balance=0; if(bptr->balance==-1) aptr->balance=1; else aptr->balance=0; bptr->balance=0; pptr=bptr; } *ht_inc=false; }//end of switch }//end of if }//end of if if(info>pptr->info) { pptr->rchild=avlinsert(info,pptr->rchild,ht_inc); if (*ht_inc==true) { switch (pptr->balance) { case 1://left heavy pptr->balance=0; *ht_inc=false; break; case 0://balanced pptr->balance=-1; break; case -1: //right heavy aptr=pptr->rchild; if (aptr->balance==-1) { printf("Right to right rotation\n"); pptr->rchild=aptr->lchild; aptr->lchild=pptr; pptr->balance=0; aptr->balance=0; pptr=aptr; } else { printf("Right to left rotation\n"); bptr=aptr->lchild; aptr->lchild=bptr->rchild; bptr->rchild=aptr; pptr->rchild=bptr->lchild; bptr->lchild=pptr; if(bptr->balance==-1) pptr->balance=1; else pptr->balance=0; if(bptr->balance==1) aptr->balance=-1; else aptr->balance=0; bptr->balance=0; pptr=bptr; }//end of else *ht_inc=false; }//end of switch }//end of if }//end of if return pptr; }//end of the function void avldisplay(struct anode*ptr,int level) { int i; if (ptr!=NULL) { avldisplay(ptr->rchild,level+1); printf("\n"); for (i=0; i<level;i++ ) printf(" "): printf("%d",ptr->info); avldisplay(ptr->lchild,level+1); }//end of if }//end of function void avlinorder(struct anode*ptr) { if (ptr!=NULL) { avlinorder(ptr->lchild); printf("%d",ptr->info); avlinorder(ptr->rchild); } }

Wednesday, 5 November 2014

How to find the sizeof Data type and Display in Decemial,Octal and HexaDecimal format in C

void main() { int i; float r; printf("welcome to kmit\n"); printf("welcome to Telengana\n"); i=15; //i=35.756; r=105643.7564; printf("%d\n",i); printf("%0.4f\n",r); printf("%d\n",sizeof(i)); printf("%d\n",sizeof(r)); printf("%d\n",i);//display in decimal printf("%o\n",i);//display in Octal printf("%0x\n",i);//display in Hexadecimal }

Radix sort program in C

#include <stdio.h> struct radix { struct radix *link; int info; }*start; void display(); int ndig(); int kdig(int,int); void radixsort(); void main() { struct radix *temp,*q; int i,j,k,item; printf("enter no of items : "); scanf("%d",&item); for(i=0;i<item;i++) { printf("enter the item %d :\n",i+1); scanf("%d",&j); temp=(struct radix*)malloc(sizeof(struct radix)); temp->info=j; temp->link=NULL; if(start==NULL) start =temp; else { q=start; while(q->link!=NULL) q=q->link; q->link=temp; } } printf("unsorted list is: \n"); display(); radixsort(); printf("\nsorted list : \n"); display(); } void display() { struct radix *temp; temp=start; while(temp!=NULL) { printf("%d\t",temp->info); temp=temp->link; } } int ndig() { struct radix *temp; int large=0,ndig=0; temp=start; while(temp!=NULL) { if(temp->info>large) large=temp->info; temp=temp->link; } while(large>0) { ndig++; large=large/10; } return(ndig); } int kdig(int num,int k) { int i,dig; for(i=1;i<=k;i++) { dig=num%10; num=num/10; } return dig; } void radixsort() { struct radix *p,*front[10],*rear[10]; int mindig,maxdig,lstsig,maxsig,k,i,dig; lstsig=1; maxsig=ndig(); for(k=lstsig;k<=maxsig;k++) { for(i=0;i<=9;i++) { rear[i]=NULL; front[i]=NULL; } mindig=9; maxdig=0; p=start; while(p!=NULL) { dig=kdig(p->info,k); if(dig>maxdig) maxdig=dig; if(dig<mindig) mindig=dig; if(front[dig]==NULL) front[dig]=p; else rear[dig]->link=p; rear[dig]=p; p=p->link; } printf("\nmindig %d maxdig %d",mindig,maxdig); start=front[mindig]; for(i=mindig;i<maxdig;i++) { if(rear[i+1]!=NULL) rear[i]->link=front[i+1]; else rear[i+1]=rear[i]; rear[maxdig]->link=NULL; } printf("\nnew list\n"); display(); } }