Date post: | 21-Feb-2021 |
Category: |
Technology |
Upload: | nguyennhovinh |
View: | 4 times |
Download: | 0 times |
ProblemSolvinginDataStructures&AlgorithmsUsingCFirstEdition
ByHemantJain
BookTitle:ProblemsSolvinginDataStructures&AlgorithmsUsingCBookAuthor:HemantJainPublishedbyHemantJainHIG 4, Samarth Tower, Sarvadharm,D Sector, Bhopal, India, Pin Code:462042PublishedinBhopal,IndiaThiseditionpublishedMarch2017
Copyright©HemantJain2017.AllRightReserved.Hemant Jain asserts themoral right tobe identified as the author of thiswork.Allrightsreserved.Nopartofthispublicationmaybereproduced,storedinorintroducedintoaretrievalsystem,ortransmitted,inanyform,orbyanymeans (electrical,mechanical, photocopying, recording or otherwise)without the priorwritten permission of the author, except in the case ofvery brief quotations embodied in critical reviews and certain other non-commercial uses permitted by copyright law. Any person who does anyunauthorized act in relation to this publicationmaybe liable to criminalprosecutionandcivilclaimsfordamages.
ACKNOWLEDGEMENTTheauthorisverygratefultoGODALMIGHTYforhisgraceandblessing.DeepestgratitudeforthehelpandsupportofmybrotherDr.SumantJain.This book would not have been possible without the support andencouragementheprovided.Iwouldliketoexpressprofoundgratitudetomyguide/myfriendNaveenKaushik for his invaluable encouragement, supervision and usefulsuggestionthroughoutthisbookwritingwork.Hissupportandcontinuousguidanceenablemetocompletemyworksuccessfully.Finally yet importantly, I am thankful to Love Singhal, Anil Berry andOtherswhohelpedmedirectlyorindirectlyincompletingthisbook.HemantJain
TABLEOFCONTENTSTABLEOFCONTENTSCHAPTER0:HOWTOUSETHISBOOKWHATTHISBOOKISABOUTPREPARATIONPLANSSUMMARY
CHAPTER1:INTRODUCTION-PROGRAMMINGOVERVIEWINTRODUCTIONVARIABLEPOINTERSARRAYTWODIMENSIONALARRAYARRAYINTERVIEWQUESTIONSSTRUCTUREPOINTERTOSTRUCTUREDYNAMICMEMORYALLOCATIONFUNCTIONCONCEPTOFSTACKSYSTEMSTACKANDFUNCTIONCALLSPARAMETERPASSING,CALLBYVALUEPARAMETERPASSING,CALLBYREFERENCERECURSIVEFUNCTIONEXERCISES
CHAPTER2:ALGORITHMSANALYSISINTRODUCTIONASYMPTOTICANALYSISBIG-ONOTATIONOMEGA-ΩNOTATIONTHETA-ΘNOTATIONCOMPLEXITYANALYSISOFALGORITHMSTIMECOMPLEXITYORDERDERIVINGTHERUNTIMEFUNCTIONOFANALGORITHMTIMECOMPLEXITYEXAMPLESMASTERTHEOREMMODIFIEDMASTERTHEOREMEXERCISE
CHAPTER3:APPROACHTOSOLVEALGORITHMDESIGNPROBLEMSINTRODUCTIONCONSTRAINTSIDEAGENERATION
COMPLEXITIESCODINGTESTINGEXAMPLESUMMARY
CHAPTER4:ABSTRACTDATATYPEABSTRACTDATATYPE(ADT)DATA-STRUCTUREARRAYLINKEDLISTSTACKQUEUETREESBINARYTREEBINARYSEARCHTREES(BST)PRIORITYQUEUE(HEAP)HASH-TABLEDICTIONARY/SYMBOLTABLEGRAPHSGRAPHALGORITHMSSORTINGALGORITHMSCOUNTINGSORTENDNOTE
CHAPTER5:SEARCHINGINTRODUCTIONWHYSEARCHING?DIFFERENTSEARCHINGALGORITHMSLINEARSEARCH–UNSORTEDINPUTLINEARSEARCH–SORTEDBINARYSEARCHSTRINGSEARCHINGALGORITHMSHASHINGANDSYMBOLTABLESHOWSORTINGISUSEFULINSELECTIONALGORITHM?PROBLEMSINSEARCHINGEXERCISE
CHAPTER6:SORTINGINTRODUCTIONTYPEOFSORTINGBUBBLE-SORTMODIFIED(IMPROVED)BUBBLE-SORTINSERTION-SORTSELECTION-SORTMERGE-SORT
QUICK-SORTQUICKSELECTBUCKETSORTGENERALIZEDBUCKETSORTHEAP-SORTTREESORTINGEXTERNALSORT(EXTERNALMERGE-SORT)COMPARISONSOFTHEVARIOUSSORTINGALGORITHMS.SELECTIONOFBESTSORTINGALGORITHMEXERCISE
CHAPTER7:LINKEDLISTINTRODUCTIONLINKEDLISTTYPESOFLINKEDLISTSINGLYLINKEDLISTDOUBLYLINKEDLISTCIRCULARLINKEDLISTDOUBLYCIRCULARLISTEXERCISE
CHAPTER8:STACKINTRODUCTIONTHESTACKABSTRACTDATATYPESTACKUSINGARRAY(MACRO)STACKUSINGARRAY(DYNAMICMEMORY)STACKUSINGARRAY(GROWINGCAPACITYIMPLEMENTATION)STACKUSINGARRAY(GROWING-REDUCINGCAPACITYIMPLEMENTATION)STACKUSINGLINKEDLISTPROBLEMSINSTACKPROSANDCONSOFARRAYANDLINKEDLISTIMPLEMENTATIONOFSTACK.USESOFSTACKEXERCISE
CHAPTER9:QUEUEINTRODUCTIONTHEQUEUEABSTRACTDATATYPEQUEUEUSINGARRAYQUEUEUSINGLINKEDLISTPROBLEMSINQUEUEEXERCISE
CHAPTER10:TREEINTRODUCTIONTERMINOLOGYINTREEBINARYTREE
TYPESOFBINARYTREESPROBLEMSINBINARYTREEBINARYSEARCHTREE(BST)PROBLEMSINBINARYSEARCHTREE(BST)EXERCISE
CHAPTER11:PRIORITYQUEUEINTRODUCTIONTYPESOFHEAPHEAPADTOPERATIONSOPERATIONONHEAPHEAP-SORTUSESOFHEAPPROBLEMSINHEAPEXERCISE
CHAPTER12:HASH-TABLEINTRODUCTIONHASH-TABLEHASHINGWITHOPENADDRESSINGHASHINGWITHSEPARATE-CHAININGPROBLEMSINHASHINGEXERCISE
CHAPTER13:GRAPHSINTRODUCTIONGRAPHREPRESENTATIONADJACENCYMATRIXADJACENCYLISTGRAPHTRAVERSALSDEPTHFIRSTTRAVERSALBREADTHFIRSTTRAVERSALPROBLEMSINGRAPHDIRECTEDACYCLICGRAPHTOPOLOGICALSORTMINIMUMSPANNINGTREES(MST)SHORTESTPATHALGORITHMSINGRAPHEXERCISE
CHAPTER14:STRINGALGORITHMSINTRODUCTIONSTRINGMATCHINGDICTIONARY/SYMBOLTABLEPROBLEMSINSTRINGMEMMOVEFUNCTIONEXERCISE
CHAPTER15:ALGORITHMDESIGNTECHNIQUESINTRODUCTIONBRUTEFORCEALGORITHMGREEDYALGORITHMDIVIDE-AND-CONQUER,DECREASE-AND-CONQUERDYNAMICPROGRAMMINGREDUCTION/TRANSFORM-AND-CONQUERBACKTRACKINGBRANCH-AND-BOUNDA*ALGORITHMCONCLUSION
CHAPTER16:BRUTEFORCEALGORITHMINTRODUCTIONPROBLEMSINBRUTEFORCEALGORITHMCONCLUSION
CHAPTER17:GREEDYALGORITHMINTRODUCTIONPROBLEMSONGREEDYALGORITHM
CHAPTER18:DIVIDE-AND-CONQUER,DECREASE-AND-CONQUERINTRODUCTIONGENERALDIVIDE-AND-CONQUERRECURRENCEMASTERTHEOREMPROBLEMSONDIVIDE-AND-CONQUERALGORITHM
CHAPTER19:DYNAMICPROGRAMMINGINTRODUCTIONPROBLEMSONDYNAMICPROGRAMMINGALGORITHM
CHAPTER20:BACKTRACKINGANDBRANCH-AND-BOUNDINTRODUCTIONPROBLEMSONBACKTRACKINGALGORITHM
CHAPTER21:COMPLEXITYTHEORYANDNPCOMPLETENESSINTRODUCTIONDECISIONPROBLEMCOMPLEXITYCLASSESCLASSPPROBLEMSCLASSNPPROBLEMSCLASSCO-NPNP–HARD:NP–COMPLETEPROBLEMSREDUCTIONENDNOTE
CHAPTER22:INTERVIEWSTRATEGYINTRODUCTIONRESUMENONTECHNICALQUESTIONSTECHNICALQUESTIONS
CHAPTER23:SYSTEMDESIGNSYSTEMDESIGNSYSTEMDESIGNPROCESSSCALABILITYTHEORYDESIGNSIMPLIFIEDFACEBOOKDESIGNFACEBOOKFRIENDSSUGGESTIONFUNCTIONDESIGNASHORTENINGSERVICELIKEBITLYSTOCKQUERYSERVERDESIGNABASICSEARCHENGINEDATABASEDESIGNABASICSEARCHENGINECACHINGDUPLICATEINTEGERINMILLIONSOFDOCUMENTSZOMATOYOUTUBEDESIGNIRCTCALARMCLOCKDESIGNFORELEVATOROFABUILDINGVALETPARKINGSYSTEMOODESIGNFORAMCDONALDSSHOPOBJECTORIENTEDDESIGNFORARESTAURANTOBJECTORIENTEDDESIGNFORALIBRARYSYSTEMSUGGESTASHORTESTPATHEXERCISE
APPENDIXAPPENDIXA
CHAPTER0:HOWTOUSETHISBOOK
WhatthisbookisaboutThisbookisaboutusageofdatastructuresandalgorithmsincomputerprogramming.Datastructuresarethewaysinwhichdataisarrangedincomputersmemory.Algorithmsaresetofinstructionstosolvesomeproblembymanipulatingthesedatastructures.DesigninganefficientalgorithmtosolveacomputerscienceproblemisaskillofComputerprogrammer.TheskillwhichtechcompanieslikeGoogle,Amazon,Microsoft,Facebook,Adobeandmanyothersarelookingfor inan interview.Oncewearecomfortablewithaprogramminglanguage, thenextstep is tolearnhowtowriteefficientalgorithms.ThisbookassumesthatyouareaClanguagedeveloper.YouarenotanexpertinClanguage,butyouarewellfamiliarwithconceptsofpointers,functions,arraysandrecursion.Atthestartofthisbook,wewillberevisingtheClanguagefundamentalsthatwillbeusedthroughoutthisbook.Wewillbelookingintosomeoftheproblemsinarraysandrecursiontoo.TheninthecomingchapterwewillbelookingintoComplexityAnalysis.Followedbythevariousdatastructures and their algorithms.Will look into a linked list, stack, queue, trees, heap,Hash-Table andgraphs.Wewillalsobelookingintosorting,searchingtechniques.Moreover, we will be looking into analysis of various algorithm techniques, such as brute forcealgorithms, greedy algorithms, divide & conquer algorithms, dynamic programming, reduction &backtracking.Intheend,wewillbelookingintosystemdesignthatwillgiveasystematicapproachtosolvethedesignproblemsinanInterview.
PreparationPlansGiven the limited timeyouhavebeforeyournext interview, it is important tohaveasolidpreparationplan.Thepreparationplandependsuponthetimeandwhichcompaniesyouareplanningtotarget.Belowarethethree-preparationplanfor1Month,3Monthand5Monthdurations.
1MonthPreparationPlansBelowisalistoftopicsandapproximatetimeuserneedtotaketofinishthesetopics.Thesearethemostimportantchaptersthatmusttobepreparedbeforeappearingforaninterview.Thisplanshouldbeusedwhenyouhavealimitedtimebeforeaninterview.Thesechapterscover90%ofdata structures and algorithm interview questions. In this plan sincewe are reading about the variousADTinchapter4sowecanusethesedatatypeeasilywithoutknowingtheinternaldetailshowtheyareimplemented.Chapter24isforsystemdesign,youmustreadthischapterifyouarethreeormoreyearsofexperienced.Anyway,readingthischapterwillgivethereaderabroaderperspectiveofvariousdesigns.Time Chapters Explanation
Week1
Chapter 1: Introduction - ProgrammingOverviewChapter2:AlgorithmsAnalysisChapter 3: Approach To Solve AlgorithmDesignProblemsChapter4:AbstractDataType
You will get a basic understanding of how to findcomplexity of a solution. You will know how tohandlenewproblems.Youwill read about avarietyofdatatypesandtheiruses.
Week2Chapter5:SearchingChapter6:SortingChapter14:StringAlgorithms
Searching,SortingandStringalgorithmconsistsofamajorportionoftheinterviews.
Week3Chapter7:LinkedListChapter8:StackChapter9:Queue
Linkedlistsareoneofthefavouritesinaninterview.
Week4Chapter10:TreeChapter23:InterviewStrategyChapter24:SystemDesign
This portion youwill read about Trees and SystemDesign.You are good to go for interviews. Best ofluck.
3MonthPreparationPlanThisplanshouldbeusedwhenyouhavesome time toprepare foran interview.Thispreparationplanincludesnearlyeverythinginthisbookexceptvariousalgorithmtechniques.Algorithmproblemsthatarebasedondynamicprogramming,divide&conqueretc.WhichareaskedinvaryspecificcompanieslikeGoogle,Facebook,etc.Therefore,untilyouareplanningtofaceinterviewwiththemyoucanparkthesetopicsforsometimeandfocusontherestofthetopics.Again,samethingherewithsystemdesignproblems,themoreexperienceyouare,themoreimportantthischapterbecomes.However,ifyouareafresherfromcollege,thenalsoyoushouldreadthischapter.Time Chapters Explanation
Week1
Chapter 1: Introduction - ProgrammingOverviewChapter2:AlgorithmsAnalysisChapter 3: Approach To Solve AlgorithmDesignProblemsChapter4:AbstractDataType
Youwill get abasicunderstandingofhow to findcomplexity of a solution. You will know how tohandle new problems. You will read about avarietyofdatatypesandtheiruses.
Week 2 &Week3
Chapter5:SearchingChapter6:SortingChapter14:StringAlgorithms
Searching, sorting and string algorithmconsists ofamajorportionoftheinterviews.
Week 4 &Week5
Chapter7:LinkedListChapter8:StackChapter9:Queue
Linked lists are one of the favourites in aninterview.
Week 6 &Week7
Chapter10:TreeChapter11:Heap
This portion you will read about trees and heapdatastructures.
Week 8 &Week9
Chapter12:Hash-TableChapter13:Graphs
Hash-Table are used throughout this book invarious places but now it is time to understandhowHash-Tableareactuallyimplemented.Graphsare used to propose a solution many real lifeproblems.
Week10 Chapter23:InterviewStrategyChapter24:SystemDesignInterview strategy and system design chapter arethefinalchaptersofthiscourse.
Week 11 &Week12 Revisionofthechapterslistedabove.
At this time, you need to revise all the chaptersthat we have seen in this book.Whatever is leftneeds to be completed and the exercise that maybeleftneedingtobesolvedinthisperiod.
5MonthPreparationPlanThispreparationplanismadeontopof3-monthplan.Inthisplan,thestudentsshouldlookforalgorithmdesign chapters. In addition, in the rest of the time they need to practice more and more fromwww.topcoder.com and other resources. If you are targeting google, Facebook, etc., then it is highlyrecommendedtojointopcoderandpracticeasmuchaspossible.Time Chapters Explanation
Week1Week2
Chapter 1: Introduction - ProgrammingOverviewChapter2:AlgorithmsAnalysisChapter3:ApproachToSolveAlgorithmDesignProblemsChapter4:AbstractDataType
You will get a basic understanding of how tofind complexity of a solution. You will knowhow to handle new problems. You will readaboutavarietyofdatatypesandtheiruses.
Week 3Week4Week5
Chapter5:SearchingChapter6:SortingChapter14:StringAlgorithms
Searching, sorting and string algorithm consistsofamajorportionoftheinterviews.
Week 6Week7Week8
Chapter7:LinkedListChapter8:StackChapter9:Queue
Linked lists are one of the favourites in aninterview.
Week9Week10
Chapter10:TreeChapter11:Heap Thisportionyouwillreadabouttrees
Week11Week12
Chapter12:Hash-TableChapter13:Graphs
Hash-Table are used throughout this book invarious places but now it is time to understandhow Hash-Table are actually implemented.
http://www.topcoder.com
Graphsareusedtoproposeasolutionmanyreallifeproblems.
Week13Week14Week 15Week16
Chapter15:AlgorithmDesignTechniquesChapter16:BruteForceChapter17:GreedyAlgorithmChapter 18: Divide-And-Conquer, Decrease-And-ConquerChapter19:DynamicProgrammingChapter 20: Backtracking And Branch-And-BoundChapter 21: Complexity Theory And NpCompleteness
Thesechapterscontainvariousalgorithmstypesand their usage. Once the user is familiar withmostof this algorithm.Then thenext step is tostart solving topcoder problems fromhttps://www.topcoder.com/
Week 17Week18
Chapter22:InterviewStrategyChapter23:SystemDesign
Interviewstrategyandsystemdesignchapterarethefinalchaptersofthiscourse.
Week 19Week20
Revisionofthechapterslistedabove.
At this time,youneed to reviseall thechaptersthatwehaveseeninthisbook.Whateverisleftneedstobecompletedandtheexercisethatmaybeleftneedingtobesolvedinthisperiod.
https://www.topcoder.com/
SummaryTheseare fewpreparationplans thatcanbe followed tocomplete thisbook therebypreparing for theinterview.Itishighlyrecommendedthattheusershouldreadtheproblemstatementfirst,thenheshouldtrytosolvetheproblemsbyhimselfandthenonlyheshouldlookintothesolutiontofindtheapproachofthebookpracticingmoreandmoreproblemsopenourthinkingcapacityandyouwillbeabletohandlenew problems in an interview. System design is a topic that is not asked much from a fresher fromcollege, but as you gain experience its importance increase. We will recommend practicing all theproblems given in this book, then solve more and more problems from online resources likewww.topcoder.com,www.careercup.com,www.geekforgeek.cometc.
CHAPTER1:INTRODUCTION-PROGRAMMINGOVERVIEW
IntroductionThis chapter emphasizes on the fundamentals ofCProgramming language. Itwill talk about variables,pointers,recursion,arraysetc.
Variable"Variables"aresimplystoragelocationstoholddata.Foreveryvariable,somememoryisallocated.Thesizeof thismemorydependsonthetypeof thevariable.Forexample,2bytesareallocatedfor integertype,4bytesareallocatedforfloattype,etc.Program1.11.#include2.3.intmain()4.{5.intvar1,var2,var3;6.var1=100;7.var2=200;8.var3=var1+var2;9.printf("Adding%dand%dwillgive%d",var1,var2,var3);10.return0;11.}Analysis:Line5:Memory isallocated forvariablesvar1,var2andvar3.Wheneverwedeclareavariable, thenmemoryisallocatedforstoringthevalueinthevariable.Inourexample,2bytesareallocatedforeachofthevariable.Line6&7:Value100isstoredinvariablevar1andvalue200isstoredinvariablevar2.Line8:Valueofvar1andvar2isaddedandstoredinvar3.Line9:Finally,thevalueofvar1,var2andvar3isprintedtoscreen.
PointersPointersarenothingmorethanvariablesthatstorememoryaddressesofanothervariableandcanbeusedtoaccess thevalue storedat thoseaddresses.Variousoperators suchas*,&,and [], enableus tousepointers.Address-Thenumericlocationofaplaceinmemory.Anaddressofamemoryisusedbythecomputertoidentifydatastoredinitquickly.Justasthepostaladdressofahouseisusedtoidentifyahousebythepostalworker.Dereference -Apointerstoresanaddressofa location in thememory.Toget thevaluestored in thataddress,weneed todereference thepointer,meaningweneed togo to that location andget thevaluestoredthere.Program1.21.intmain()2.{3.intvar;4.int*ptr;5.var=10;6.ptr=&var;7.8.printf("Valuestoredatvariablevaris%d\n",var);9.printf("Valuestoredatvariablevaris%d\n",*ptr);10.11.printf("Theaddressofvariablevaris%p\n",&var);12.printf("Theaddressofvariablevaris%p\n",ptr);13.return0;14.}Analysis:Line3:Anintegertypevariablevariscreated.Line4:Apointertointptriscreated.Line6:Addressofvarisstoredinptr.Line8&9:Valuestoredinvariablevarisprintedtoscreen.*Operatorisusedtogetthevaluestoredatthepointerlocation.Line11&12:Memoryaddressofvarisprintedtothescreen.&operatorisusedtogettheaddressofavariable.PointtoRemember:
1.Youcandefineapointerbyincludinga*beforethenameofthevariable.(Pointerdeclaration)2.Youcangetthevaluestoredataddressbyadding*beforethevariablename.(Pointeruse)3.Youcangettheaddressofavariablebyusing&operator.
ArrayAnarray is adata structureused to storemultipledataelementsof the samedata type.All thedata isstoredsequentially.Thevaluestoredatanyindexcanbeaccessedinconstanttime.Program1.31.intmain()2.{3.intarr[10];4.for(inti=0;i<10;i++)5.{6.arr[i]=i;7.}8.printArray(arr,10);9.}Analysis:Line3:Definesanarrayofintegers.Thearrayisofsize10-whichmeansthatitcanstore10integersinsideit.Line 6: Array elements are accessed using subscript operator []. Lowest subscript is 0 and highestsubscriptis(sizeofarray–1).Value0to9isstoredinthearrayatindex0to9.Line8:ArrayanditssizearepassedtoprintArray()function.Program1.41.voidprintArray(intarr[],intcount)2.{3.printf("Valuesstoredinarrayare:");4.for(inti=0;i<count;i++)5.{6.printf("[%d]",arr[i]);7.}8.}Analysis:Line1:ArrayvariablearranditsvariablecountarepassedasargumentstoprintArray()function.Line4-7:Finally,arrayvaluesareprintedtoscreenusingtheprintffunctioninaloop.PointtoRemember:1.Arrayindexalwaysstartsfrom0indexandhighestindexissize-1.2.Thesubscriptoperatorhashighestprecedenceifyouwritearr[2]++.Thenthevalueofarr[2]willbe
incremented.Eachelementofthearrayhasamemoryaddress.Thefollowingprogramwillprintthevaluestoredinanarrayandtheiraddress.
Program1.51.voidprintArrayAddress(intarr[],intcount)2.{3.printf("Valuesstoredinarrayare:");4.for(inti=0;i<count;i++)5.{6.printf("Data:[%d]hasAddress:[%p]\n",arr[i],arr+i);7.}8.}Analysis:Line 6: Value stored in an array is printed. The address of the various elements in the array is alsoprinted.PointtoRemember:Forarrayelements,consecutivememorylocationisallocated.Program1.61.voidprintArrayUsingPointer(intarr[],intcount)2.{3.printf("Valuesstoredinarrayare:");4.int*ptr=arr;5.for(inti=0;i<count;i++)6.{7.printf("Data:[%d]hasAddress:[%p]\n",*ptr,ptr);8.ptr++;9.}10.}Analysis:Line4:Apointertoanintiscreatedanditwillpointtothearray.Line7:valuestoredinpointerisprintedtoscree.Line8:Pointerisincremented.
TwoDimensionalArrayWecandefinetwodimensionalormultidimensionalarray.Itisanarrayofarray.Program1.71.intmain()2.{3.intarr[4][2];4.intcount=0;5.for(inti=0;i<4;i++)6.for(intj=0;j<2;j++)7.arr[i][j]=count++;8.9.print2DArray((int**)arr,4,2);10.print2DArrayAddress((int**)arr,4,2);11.}1.voidprint2DArray(int*arr[],introw,intcol)2.{3.for(inti=0;i<row;i++)4.for(intj=0;j<col;j++)5.printf("[%d]",*(arr+i*col+j));6.7.}1.voidprint2DArrayAddress(int*arr[],introw,intcol)2.{3.for(inti=0;i<row;i++)4.for(intj=0;j<col;j++)5.printf("Value:%d,Address:%p\n",*(arr+i*col+j),(arr+i*col+j));6.}Analysis:·Anarrayiscreatedwithdimension4x2.Thearraywillhave4rowsand2columns.·Valueisassignedtothearray·Finallythevaluestoredinarrayisprintedtoscreenandtheaddressusedtostorevaluesisprintedby
usingprint2DArray()andprint2DarrayAddress()function.Wecandefineapointerarraysimilartoanarrayofinteger.Thearrayelementwillstoreapointerinsideit.Program1.81.voidprintArray(int*arr[],intcount)2.{3.int*ptr;4.for(inti=0;i<count;i++)
5.{6.ptr=arr[i];7.printf("[%d]",*ptr);8.}9.}1.voidprintArrayAddress(int*arr[],intcount)2.{3.int*ptr;4.for(inti=0;i<count;i++)5.{6.ptr=arr[i];7.printf("Valueis:%d,Addressis:%p\n",*ptr,ptr);8.}9.}1.intmain()2.{3.intone=1,two=2,three=3;4.int*arr[3];5.arr[0]=&one;6.arr[1]=&two;7.arr[2]=&three;8.printArray(arr,3);9.printArrayAddress(arr,3);10.}Analysis:·Threevariables,one,twoandthreearedefined.·Pointerarrayarrisdefied.·Theaddressofone,twoandthreeisstoredinsidearrayarr.·printArray()andprintArrayAddress()functionsareusedtoprintvaluestoredinthearray.
ArrayInterviewQuestionsThefollowingsectionwilldiscussthevariousalgorithmsthatareapplicabletoarraysandwillfollowbylistofpracticeproblemswithsimilarapproaches.
SumArrayProgram:Writeafunctionthatwillreturnthesumofalltheelementsoftheintegerarraygivenarrayanditssizeasanargument.Program1.9intSumArray(intarr[],intsize){
inttotal=0;intindex=0;for(index=0;index
smallerthanthemiddlevalue.Wewillsearcheithertheleftortherightportionofthearray.Ateachstep,weareeliminatinghalfofthesearchspacetherebymakingthisalgorithmveryefficientthelinearsearch.
BinarySearchProgram1.11:Binarysearchinasortedarray./*BinarySearchAlgorithm–IterativeWay*/intBinarySearch(intarr[],intsize,intvalue){
intlow=0,mid;inthigh=size-1;while(low
voidreverseArray(int*a,intn){
for(inti=0,j=n-1;i5,6,7,8,9,10,1,2,3,41,2,3,4,5,6,7,8,9,10=>4,3,2,1,10,9,8,7,6,5=>5,6,7,8,9,10,1,2,3,4Analysis:·Rotatingarrayisdoneintwopartstrick.Inthefirstpart,wefirstreverseelementsofarrayfirsthalfand
thensecondhalf.·Thenwereversethewholearraytherebycompletingthewholerotation.
Findthelargestsumcontiguoussubarray.Givenanarrayofpositiveandnegativeintegers,findacontiguoussubarraywhosesum(sumofelements)ismaximized.
Program1.13intmaxSubArraySum(inta[],intsize){
intmaxSoFar=0,maxEndingHere=0;for(inti=0;i<size;i++){maxEndingHere=maxEndingHere+a[i];if(maxEndingHere<0)maxEndingHere=0;if(maxSoFar<maxEndingHere)maxSoFar=maxEndingHere;}returnmaxSoFar;
}Analysis:·Maximumsubarrayinanarrayisfoundinasinglescan.Wekeeptrackofglobalmaximumsumsofar
andthemaximumsum,whichincludethecurrentelement.·Whenwefindglobalmaximumvaluesofarislessthanthemaximumvaluecontainingcurrentvaluewe
updatetheglobalmaximumvalue.·Finallyreturntheglobalmaximumvalue.
StructureStructuresareusedwhenwewanttoprocessdataofmultipledatatypesasasingleentity.Program1.14:DemonstratingStructure1.structcoord{2.intx;3.inty;4.};5.intmain()6.{7.structcoordpoint;8.point.x=10;9.point.y=10;10.printf("Xaxiscoordvalueis%d\n",point.x);11.printf("Yaxiscoordvalueis%d\n",point.y);12.printf("Sizeofstructureis%dbytes\n",sizeof(point));13.return0;14.}Output:Xaxiscoordvalueis10Yaxiscoordvalueis10Sizeofstructureis8bytesAnalysis:Line1-4:Wehavedeclaredstructure"coord",whichcontaintwoelementsinsideit.Thetwoelementsxandycorrespondingtox-axisandy-axiscoordinates.Line8:Wehavedeclaredavariable"point"oftype"structcoord”.Line9-10:Wehaveassignedcoordinate(10,10),toxandyelementofthe"point".Variouselementsofastructureareassessedusingthedot(.)operator.Line11-12:Weareprinting thevalue stored in thex andy elements of thepoint that is of type structcoord.Line13:Weareprintingthesizeofstructpoint.Sincestructureconsistsofmorethanoneelement,thenthesizeofastructureisthesumofalltheelementsinsideit.
PointertostructurePointerscanbeusedtoaccessthevariouselementsofastructure.Thevariouselementsofastructureareaccessedbypointerusing->operator.Program1.15:Pointertostructure1.#include2.structstudent{3.introllNo;4.char*firstName;5.char*lastName;6.};7.intmain()8.{9.inti=0;10.structstudentstud;11.structstudent*ptrStud;12.ptrStud=&stud;13.ptrStud->rollNo=1;14.ptrStud->firstName="john";15.ptrStud->lastName="smith";16. printf("Roll No: %d Student Name: %s %s ", ptrStud->rollNo, ptrStud->firstName, ptrStud->lastName);17.18.return0;19.}Analysis:Line2-6:Wehavedeclaredastructstudentthatcontainrollno,firstandlastnameofastudent.Line12:Wehavedeclaredapointertostructstudent.Line13-15:PointerptrStudispointingtostud.WehaveusedptrStudtoassignavaluetostructstud.Wehaveused->operatortoaccessthevariouselementsofthestructurepointedbyptrStud.Note: If we have used the stud to assignwewould have used "." operator. The same structurewhenaccessedusingpointerweuseindirectionoperator"->".Line16:Wehavefinallyprintedallthevariouselementsofstructurevariablestud.Note:Inthesamewayyoucanuse->operatortoaccesselementsoftheUnion.
DynamicMemoryAllocationIn c language, dynamic memory is allocated using the malloc(), calloc() and realloc() functions. Thedynamicmemoryrequiredtobefreedusingthefree()function.
MallocfunctionDefinitionofthemallocfunctionisasbelow.void*malloc(size_tsize);Itallocatesamemoryblockoflength"size"bytesandreturnapointertotheblock.ItwillreturnNULLifthesystemdoesnothaveenoughmemory.TheCstandarddefinesvoid*isagenericpointerthatisrequiredtobecastedtotherequiredtype.MostCcompilersneedthiscasting.However,thelatestANSICstandarddoesnotrequire.E.g.int*p=(int*)malloc(sizeof(int));
CallocfunctionDefinitionofcallocfunctionisasbelow.void*calloc(size_tnum,size_tsize);Itallocatesamemoryblockoflength"num*size"bytesandreturnapointertotheblock.ItwillreturnNULLifthesystemdoesnothaveenoughmemory.Onethingmoreitdoesthat,itinitializeseverybytetozero.
ReallocfunctionDefinitionofreallocfunctionisasbelow.void*realloc(void*ptr,size_tnewSize);Itisusedtochangethememoryblocksizeofapreviouslyallocatedblockofmemorypointedbyptr.ItreturnsamemoryblockofthenewSize.Iftheblocksizeifincreased,thenthecontentoftheoldmemoryblockiscopiedtoanewlyallocatedregion.Ifthepointerreturnedbythefunctionisdifferentfromtheoldpointerptr.Thenptrwillnolongerpointtoavalidlocation.Soingeneralyoushouldnotuseptronceitispassedtorealloc()function.IfptrisNULL,reallocworkssameasmalloc().Note:Again,youneed tocast the returnvalueofmalloc/calloc/reallocbeforeusing it. int*i= (int*)malloc(size);
FreeFunctionThememory that is allocatedusingmalloc/calloc/reallocneed tobe freedusing a free() function.The
syntaxofthefree()functionisasbelow.voidfree(void*pointer);Apointer topreviously allocatedmemory ispassed to free() function.The free() functionwill put theallocatedmemoryblockbacktoheapsection.
FunctionFunctionsareusedtoprovidemodularitytotheprogram.Byusingfunction,wecandividecomplextasksintosmallermanageabletasks.Theuseofthefunctionisalsotoavoidduplicatecode.Forexample,wecandefineafunctionsum()whichtaketwointegersandreturntheirsum.Thenwecanusethisfunctionmultipletimeswheneverwewantsumoftwointegers.Program1.16:DemonstratingFunctionCalls1.#include2./*functiondeclaration*/3.intsum(intnum1,intnum2);4.intmain()5.{6./*localvariabledefinition*/7.intx=10;8.inty=20;9.intresult;10./*callingafunctiontofindsum*/11.result=sum(x,y);12.printf("Sumis:%d\n",result);13.return0;14.}15./*functionreturningthesumoftwonumbers*/16.intsum(intnum1,intnum2)17.{18./*localvariabledeclaration*/19.intresult;20.result=num1+num2;21.returnresult;22.}Output:Sumis:30Analysis:Line3:functiondeclarationofsum()functionLine11:sumfunctioniscalledfromthismainbypassingvariablexandywithvalue10and20atthispointcontrolflowwillgotoLine16.Line16:variablespassedtosumfunctionarecopiedintonum1andnum2localvariables.Line20&21: thesumiscalculatedandsaved inavariable result.And the result is returned.Controlflowcomesbacktolinenumber11.Line11-12:returnvalueofthesumfunctionissavedinalocalvariableresultandprintedtothescreen.
ConceptofStackAstackisamemoryinwhichvaluesarestoredandretrievedin“lastinfirstout”manner.Dataisaddedtostackusingpushoperationanddataistakenoutofstackusingpopoperation.
1.Initiallythestackwasempty.Thenwehaveaddedvalue1tostackusingpush(1)operator.2.Similarly,push(2)andpush(3)3.Popoperationtakethetopofthestack.Stackdataisaddedanddeletedin“lastin,firstout”manner.4.Firstpop()operationwilltake3outofthestack.5.Similarly,otherpopoperationwilltake2then1outofthestack6.Intheend,thestackisemptywhenalltheelementsaretakenoutofthestack.
SystemstackandFunctionCallsWhenthefunctioniscalled,thecurrentexecutionisstoppedandthecontrolgoestothecalledfunction.Afterthecalledfunctionexits/returns,theexecutionresumesfromthepointatwhichtheexecutionwasstopped.Togettheexactpointatwhichexecutionshouldberesumed,theaddressofthenextinstructionisstoredinthestack.Whenthefunctioncallcompletetheaddressatthetopofthestackistakenout.Program1.171.voidfun2()2.{3.printf("fun2line1\n");4.}5.6.voidfun1()7.{8.printf("fun1line1\n");9.fun2();10.printf("fun1line2\n");11.}12.13.intmain()14.{15.printf("mainline1\n");16.fun1();17.printf("mainline2\n");18.}Output:mainline1fun1line1fun2line1fun1line2mainline2Analysis:Line13:Everyprogramstartswithmain()function.Line15:Thisisthefirststatementthatwillbeexecuted.Andwewillget“mainline1”asoutput.Line16:fun1()iscalled.Beforecontrolgoestofun1()thennextinstructionthatisaddressofline17isstoredinthesystemstack.Line6:Controlgoestofun1()function.Line8:Thisisthefirststatementinsidefun1(),thiswillprint“fun1line1”tooutput.Line9:fun2()iscalledfromfun1().Beforecontrolgoestofun2()addressofthenextinstructionthatisaddressofline10isaddedtothesystemstack.
Line1:Controlgoestofun2()function.Line3:“fun2line1”isprintedtoscreen.Line10:Whenfun2exits,controlcomeback to fun1.Moreover, theprogramreads thenext instructionfromthestack,andline10isexecuted.Andprint“fun1line2”toscreen.Line17:When fun1 exits, control comesback to themain function.Program reads thenext instructionfromthestack,linenumber17isexecuted,andfinally“mainline2”isprintedtoscreen.Pointstoremember:1.Functionsareimplementedusingastack.2.Whenafunctioniscalledtheaddressofthenextinstructionispushedintothestack.3.Whenafunctionisfinishedtheaddressoftheexecutionistakenoutofthestack.
Parameterpassing,CallbyvalueArgumentscanbepassedfromonefunctiontootherusingparameters.Bydefault,alltheparametersarepassedbyvalue.Thatmeansaseparatecopyiscreatedinsidethecalledfunctionandthevariableinthecallingfunctionremainsunchanged.Program1.181.voidincrement(intvar)2.{3.var++;4.}5.6.intmain()7.{8.inti=10;9.printf("Valueofibeforeincrementis:%d\n",i);10.increment(i);11.printf("Valueofibeforeincrementis:%d\n",i);12.}Output:Valueofibeforeincrementis:10Valueofibeforeincrementis:10Analysis:Line8:variable”i”isdeclaredandvalue10isinitializedtoit.Line9:valueif”i”isprinted.Line10:incrementfunctioniscalled.Whenafunctioniscalledthevalueoftheparameteriscopiedintoanothervariableofthecalledfunction.Flowofcontrolgoestolineno1.Line 3: value of var is incremented by 1. However, remember, it is just a copy inside the incrementfunction.Line11:Whenthefunctionexits,thevalueof”i”isstill10.Pointstoremember:1.Passbyvaluejustcreatesacopyofvariable.2.Passbyvalue,valuebeforeandafterthefunctioncallremainsame.
Parameterpassing,CallbyReferenceIfyouneedtochangethevalueoftheparameterinsidethefunction,thenyoushouldusecallbyreference.Clanguagebydefaultpassesbyvalue.Therefore,tomakeithappen,youneedtopasstheaddressofavariableandchangingthevalueofthevariableusingthisaddressinsidethecalledfunction.Program1.191.voidincrement(int*ptr)2.{3.(*ptr)++;4.}5.intmain()6.{7.inti=10;8.printf("Valueofibeforeincrementis:%d\n",i);9.increment(&i);10.printf("Valueofibeforeincrementis:%d\n",i);11.}Output:Valueofibeforeincrementis:10Valueofibeforeincrementis:11Analysis:Line9:Addressof“i” ispassed to thefunction increment.Function increment takesapointer to intasargument.Line3:Variableattheaddressptrisaccessedanditsvalueisincremented.Line10:Finally,incrementedvalueisprintedtoscreen.Pointstoremember:1.Callbyreferenceisimplementedindirectlybypassingtheaddressofavariabletothefunction.
RecursiveFunctionArecursivefunctionisafunctionthatcallsitself,directlyorindirectly.A recursive function consists of two parts: TerminationCondition andBody (which include recursiveexpansion).1.TerminationCondition:A recursive function always contains oneormore terminating condition.A
conditioninwhichrecursivefunctionisprocessingasimplecaseanddonotcallitself.2.Body(includingrecursiveexpansion):Themainlogicoftherecursivefunctioncontainedinthebody
ofthefunction.Italsocontainstherecursionexpansionstatementthatinturncallsthefunctionitself.Threeimportantpropertiesofrecursivealgorithmare:1)Arecursivealgorithmmusthaveaterminationcondition.2)Arecursivealgorithmmustchangeitsstate,andmovetowardstheterminationcondition.3)Arecursivealgorithmmustcallitself.Note:Thespeedofarecursiveprogramisslowerbecauseofstackoverheads.If thesametaskcanbedoneusingan iterativesolution(loops), thenweshouldpreferan iterativesolution(loops) inplaceofrecursiontoavoidstackoverhead.Note:Withoutterminationcondition,therecursivefunctionmayrunforeverandwillfinallyconsumeallthestackmemory.
FactorialProgram1.20:FactorialCalculation.N!=N*(N-1)….2*1.1.intfactorial(unsignedinti)2.{3./*TerminationCondition*/4.if(i
5.printInt(number/10);6.printf("%c",digit);7.}Analysis:Line3:Eachtimeremainderiscalculatedandstoreditscharequivalentindigit.Line4-5:ifthenumberisgreaterthan10thenthenumberdividedby10ispassedtoprintInt()function.Line6:Numberwillbeprintedwithhigherorderfirstthenthelowerorderdigits.TimeComplexityisO(N)
PrintBase16IntegersProgram1.22:Genericprinttosomespecificbasefunction.1.voidprintInt(unsignedintnumber,constintbase)2.{3.char*conversion="0123456789ABCDEF";4.chardigit=number%base;5.if(number/=base)6.printInt(number,base);7.printf("%c",conversion[digit]);8.}Analysis:Line1:basevalueisalsoprovidedalongwiththenumber.Line4:remainderofthenumberiscalculatedandstoredindigit.Line5-6:ifthenumberisgreaterthanbasethen,numberdividedbybaseispassedasanargumenttotheprintInt()functionrecursively.Line7:Numberwillbeprintedwithhigherorderfirstthenthelowerorderdigits.TimeComplexityisO(N)
IntegertoStringProgram1.231.char*intToStr(char*p,unsignedintnumber)2.{3.chardigit=number%10+'0';4.if(number/=10)5.p=intToStr(p,number);6.*p++=digit;7.return(p);8.}Analysis:Line1:characterbufferp ispassedasargumentandnumber ispassed,whichneed tobeconverted tostring.
Line3:leastsignificantdigitofthenumberisconvertedintocorrespondingcharacter.Line4-5: if thenumber isgreater than10 then,numberdividedby10 ispassedas anargument to theintToStr()functionrecursively.Line6:Thecharacterisstoredinthebufferphigherorderfirst,thenthelowerorderdigits.TimeComplexityisO(N)
TowerofHanoiTheTowerofHanoi(alsocalledtheTowerofBrahma)WearegiventhreerodsandNnumberofdisks,initiallyallthedisksareaddedtofirstrod(theleftmostone)indecreasingsizeorder.Theobjectiveistotransfertheentirestackofdisksfromfirsttowertothirdtower(therightmostone),movingonlyonediskatatimeandneveralargeroneontoasmaller.
Program1.241.voidtowerOfHanoi(intnum,charsrc,chardst,chartemp)2.{3.if(num<1)4.return;5.6.towerOfHanoi(num-1,src,temp,dst);7.printf("\nMovedisk%dfrompeg%ctopeg%c",num,src,dst);8.towerOfHanoi(num-1,temp,dst,src);9.}1.intmain()2.{3.intnum=4;4.printf("ThesequenceofmovesinvolvedintheTowerofHanoiare:\n");5.towerOfHanoi(num,'A','C','B');6.return0;7.}Analysis:TowerOfHanoiproblemifwewanttomoveNdisksfromsourcetodestination,thenwefirst
moveN-1disksfromsourcetotemp,thenmovethelowestNthdiskfromsourcetodestination.ThenwillmoveN-1disksfromtemptodestination.
Greatestcommondivisor(GCD)Program1.251.intGCD(intm,intn)2.{3.if(m
15.}16.voidpermutation(int*arr,inti,intlength){17.if(length==i){18.printArray(arr,length);19.return;20.}21.intj=i;22.for(j=i;j<length;j++){23.swap(arr,i,j);24.permutation(arr,i+1,length);25.swap(arr,i,j);26.}27.return;28.}29.intmain()30.{31.intarr[5];32.for(inti=0;i<5;i++)33.{34.arr[i]=i;35.}36.permutation(arr,0,5);37.}Analysis:In permutation function at each recursive call number at index, “i” is swapped with all thenumbersthatarerightofit.Sincethenumberisswappedwithallthenumbersinitsrightonebyoneitwillproduceallthepermutationpossible.
BinarysearchusingrecursionProgram1.281./*BinarySearchAlgorithm–RecursiveWay*/2.intBinarySearchRecursive(intarr[],intlow,inthigh,intvalue)3.{4.if(low>high)5.return-1;6.intmid=low+(high-low)/2;/*Toavoidtheoverflow*/7.if(arr[mid]==value)8.returnmid;9.elseif(arr[mid]<value)10.returnBinarySearchRecursive(arr,mid+1,high,value);11.else12.returnBinarySearchRecursive(arr,low,mid-1,value);13.}
Analysis:Similar iterativesolutionwehadalreadyseen.Nowletus lookinto therecursivesolutionof thesameprobleminthissolutionalso,wearedivingthesearchspaceintohalfanddoingthesamewhatwehaddoneintheiterativesolution.
Exercises1.Findaverageofalltheelementsinanarray.2.Findthesumofalltheelementsofatwodimensionalarray.3.Findthelargestelementinthearray.4.Findthesmallestelementinthearray.5.Findthesecondlargestnumberinthearray.6.Printall themaxima’s inanarray. (Avalue isamaximumif thevaluebeforeandafter its indexare
smallerthanitisordoesnotexist.)Hint:a)Starttraversingarrayfromtheendandkeeptrackofthemaxelement.b)Ifweencounteranelement>max,printtheelementandupdatemax.
7.Printalternateelementsinanarray.8.Givenanarraywithvalue0or1,writeaprogramtosegregate0ontheleftsideand1ontherightside.9.Givenalistofintervals,mergealloverlappingintervals.
Input:{[1,4],[3,6],[8,10]}Output:{[1,6],[8,10]}
10.Writeafunctionthatwilltakeintervalsasinputandtakescareofoverlappingintervals.11.Reverse an array in-place. (You cannot use any additional array inotherwardsSpaceComplexity
shouldbeO(1).)Hint:Usetwovariable,startandend.Startsetto0andendsetto(n-1).Incrementstartanddecrementend.Swapthevaluesstoredatarr[start]andarr[end].Stopwhenstartisequaltoendorstartisgreaterthanend.
12.Givenanarrayof0sand1s.Weneedtosortitsothatallthe0sarebeforeallthe1s.Hint:Usetwovariable,startandend.Startsetto0andendsetto(n-1).Incrementstartanddecrementend.Swapthevaluesstoredatarr[start]andarr[end]onlywhenarr[start]==1andarr[end]==0.Stopwhenstartisequaltoendorstartisgreaterthanend.
13.Givenanarrayof0s,1sand2s.Weneedtosortitsothatallthe0sarebeforeallthe1sandallthe1s
arebefore2s.Hint:Sameasabovefirstthink0sand1sasonegroupandmoveallthe2sontherightside.Thendoasecondpassoverthearraytosort0sand1s.
14.Findtheduplicateelementsinanarrayofsizenwhereeachelementisintherange0ton-1.
Hint:Approach1:Compareeachelementwithalltheelementsofthearray(usingtwoloops)O(n2)solutionApproach2:MaintainaHash-Table.Setthehashvalueto1ifweencountertheelementforthefirsttime.Whenwesamevalueagainwecanseethatthehashvalueisalready1sowecanprintthatvalue.
O(n)solution,butadditionalspaceisrequired.Approach3:Wewillexploittheconstraint"everyelementisintherange0ton-1".Wecantakeanarrayarr[]ofsizenandsetalltheelementsto0.Wheneverwegetavaluesayval1.Wewillincrementthevalueatarr[var1]indexby1.Intheend,wecantraversethearrayarrandprinttherepeatedvalues.AdditionalSpaceComplexitywillbeO(n)whichwillbeless thanHash-Tableapproach.
15.Findthemaximumelementinasortedandrotatedarray.Complexity:O(logn)
Hint:Usebinarysearchalgorithm.16.Givenanarraywith'n'elements&avalue'x',findtwoelementsinthearraythatsumsto'x'.
Hint:Approach1:Sortthearray.Approach2:UsingaHash-Table.
17.Write a function to find the sum of every number in an int number. Example: input= 1984, output
shouldbe32(1+9+8+4).18.WriteafunctiontocomputeSum(N)=1+2+3+…+N.
CHAPTER2:ALGORITHMSANALYSIS
IntroductionComputer programmer learn by experience. We learn by seeing solved problems and solving newproblemsbyourselves.Studyingvariousproblem-solvingtechniquesandbyunderstandinghowdifferentalgorithmsaredesignedhelpsustosolvethenextproblemthatisgiventous.Byconsideringanumberofdifferentalgorithms,wecanbegintodeveloppatternsothatthenexttimeasimilarproblemarises,wearebetterabletosolveit.Whenanintervieweraskstodevelopaprograminaninterviewer,whatarethestepsthatanintervieweeshouldfollow.Wewillbetakingasystematicapproachtohandletheproblemandfinallyreachingtothesolution.
AlgorithmAnalgorithmisasetofstepstoaccomplishatask.Analgorithminacomputerprogramisasetofstepsappliedoverasetofinputtoproduceasetofoutput.Knowledgeofalgorithmhelpsustogetourdesiredresultfasterbyapplyingtherightalgorithm.Themostimportantpropertiesofanalgorithmare:1.Correctness:The algorithm should be correct. It should be able to process all the given inputs and
providecorrectoutput.2.Efficiency:Thealgorithmshouldbeefficientinsolvingproblems.Algorithmiccomplexityisdefinedashowfastaparticularalgorithmperforms.ComplexityisrepresentedbyfunctionT(n)-timeversustheinputsizen.
AsymptoticanalysisAsymptoticanalysisisusedtocomparetheefficiencyofalgorithmindependentlyofanyparticulardatasetorprogramminglanguage.Wearegenerallyinterestedintheorderofgrowthofsomealgorithmandnotinterestedintheexacttimerequiredforrunninganalgorithm.ThistimeisalsocalledAsymptotic-runningtime.
Big-ONotationDefinition:“f(n)isbig-Oofg(n)”orf(n)=O(g(n)),iftherearetwo+veconstantscandn0suchthatf(n)≤cg(n)foralln≥n0,Inotherwords,cg(n)isanupperboundforf(n)foralln≥n0Thefunctionf(n)growthisslowerthancg(n)
Example:n2+n=O(n2)
Omega-ΩNotationDefinition:“f(n)isomegaofg(n).”orf(n)=Ω(g(n))iftherearetwo+veconstantscandn0suchthatcg(n)≤f(n)foralln≥n0Inotherwords,cg(n)islowerboundforf(n)Functionf(n)growthisfasterthancg(n)
Findrelationshipoff(n)=ncandg(n)=cnF(n)=Ω(g(n))
Theta-ΘNotationDefinition:“f(n)isthetaofg(n).”orf(n)=Θ(g(n))iftherearethree+veconstantsc1,c2andn0suchthatc1g(n)≤f(n)≤c2g(n)foralln≥n0g(n)isanasymptoticallytightboundonf(n).Functionf(n)growsatthesamerateasg(n).
Example:n3+n2+n=Ɵ(n2)Example:n2+n=Ɵ(n2)Findrelationshipoff(n)=2n2+nandg(n)=n2f(n)=O(g(n))f(n)=Ɵ(g(n))f(n)=Ω(g(n))Note:-AsymptoticAnalysisisnotperfect,butthatisthebestwayavailableforanalysingalgorithms.Forexample, say thereare twosortingalgorithms first take f(n)=10000nlognand f(n)=n2 time.Theasymptoticanalysissaysthatthefirstalgorithmisbetter(asitignoresconstants)butactuallyforasmallset of data when n is small then 10000, the second algorithm will perform better. To consider thisdrawbackofasymptoticanalysiscaseanalysisofthealgorithmisintroduced.
Complexityanalysisofalgorithms1)WorstCase complexity: It is the complexityof solving theproblem for theworst input of sizen. Itprovidestheupperboundforthealgorithm.Thisisthemostcommonanalysisdone.2)AverageCasecomplexity:Itisthecomplexityofsolvingtheproblemonanaverage.Wecalculatethetimeforallthepossibleinputsandthentakeanaverageofit.3)BestCasecomplexity:Itisthecomplexityofsolvingtheproblemforthebestinputofsizen.
TimeComplexityOrderAlistofcommonlyoccurringalgorithmTimeComplexityinincreasingorder:Name NotationConstant O(1)Logarithmic O(logn)Linear O(n)N-LogN O(n.logn)Quadratic O(n2)Polynomial O(nc)cisaconstant&c>1Exponential O(cn)cisaconstant&c>1FactorialorN-power-N O(n!)orO(nn)
ConstantTime:O(1)Analgorithmissaidtoruninconstanttimeregardlessoftheinputsize.Examples:1.Accessingnthelementofanarray2.Pushandpopofastack.3.EnqueueandDequeueofaqueue.4.AccessinganelementofHash-Table.5.Bucketsort
LinearTime:O(n)Analgorithmissaidtoruninlineartimeiftheexecutiontimeofthealgorithmisdirectlyproportionaltotheinputsize.Examples:1.Arrayoperationslikesearchelement,findmin,findmaxetc.2.Linkedlistoperationsliketraversal,findmin,findmaxetc.Note:whenweneedtosee/traverseallthenodesofadata-structureforsometaskthencomplexityisnolessthanO(n)
LogarithmicTime:O(logn)Analgorithmissaidtoruninlogarithmictimeiftheexecutiontimeofthealgorithmisproportionaltothelogarithm of the input size. Each step of an algorithm, a significant portion of the input is pruned outwithouttraversingit.Example:1.BinarysearchNote:Wewillreadaboutthesealgorithmsinthisbook.
N-LogNTime:O(nlog(n))Analgorithmissaidtoruninlogarithmictimeiftheexecutiontimeofanalgorithmisproportionaltothe
productofinputsizeandlogarithmoftheinputsize.Example:1.Merge-Sort2.Quick-Sort(Averagecase)3.Heap-SortNote:Quicksortisaspecialkindofalgorithmtosortalistofnumbers.Itsworst-casecomplexityisO(n2)andaveragecasecomplexityisO(n.logn)
QuadraticTime:O(n2)Analgorithmissaidtoruninlogarithmictimeiftheexecutiontimeofanalgorithmisproportionaltothesquareoftheinputsize.Examples:1.Bubble-Sort2.Selection-Sort3.Insertion-Sort
DerivingtheRuntimeFunctionofanAlgorithm
Constants
Eachstatementtakesaconstanttimetorun.TimeComplexityisO(1)
Loops
The running time of a loop is a product of running time of the statement inside a loop and number ofiterationsintheloop.TimeComplexityisO(n)
NestedLoop
Therunningtimeofanestedloopisaproductofrunningtimeofthestatementsinsideloopmultipliedbyaproductofthesizeofalltheloops.TimeComplexityisO(nc)Wherecisanumberofloops.Fortwoloops,itwillbeO(n2)
Consecutive Statements
Justaddtherunningtimesofalltheconsecutivestatements
If-Else Statement
Considertherunningtimeofthelargerofifblockorelseblock.Moreover,ignoretheotherone.
Logarithmicstatement
Ifeachiterationtheinputsizeofdecreasesbyaconstantmultiplefactors.O(logn)
TimeComplexityExamplesExample1intfun(intn){
intm=0;for(inti=0;i
}returnm;
}Eachtimeproblemspaceisdividedintohalf.TimeComplexity:O(log(n))Example5intfun(intn){
inti=0,m=0;i=n;while(i>0){m+=1;i=i/2;}returnm;
}Sameasaboveeachtimeproblemspaceisdividedintohalf.TimeComplexity:O(log(n))Example6intfun(intn){
inti=0,j=0,k=0,m=0;i=n;for(i=0;i
for(i=0;i
m+=1;returnm;
}O(N+(N-1)+(N-2)+...)=O(N(N+1)/2)//arithmeticprogression.TimeComplexity:O(n2)Example11intfun(intn){
inti=0,j=0,k=0,m=0;
for(i=0;i
returnm;}Theinnerloopwillrunfor1,2,4,8,…ntimesinsuccessiveiterationoftheouterloop.TimeComplexity:T(n)=O(1+2+4+….+n/2+n)=O(n)
MasterTheoremThemastertheoremsolvesrecurrencerelationsoftheform:T(n)=aT(n/b)+f(n)Wherea≥1andb>1."n"isthesizeoftheproblem."a"isanumberofsubproblemintherecursion.“n/b”isthesizeofeachsub-problem."f(n)"isthecostofthedivisionoftheproblemintosubproblemormergeofresultsofsubproblemstogettheresult.Itispossibletodetermineanasymptotictightboundinthesethreecases:Case1:when )andconstantЄ>1,thenthefinalTimeComplexitywillbe:
Case2:when )andconstantk≥0,thenthefinalTimeComplexitywillbe:
)Case3:when andconstantЄ>1,thenthefinalTimeComplexitywillbe:T(n)=Ɵ(f(n))
Example14:TakeanexampleofMerge-Sort,T(n)=2T(n/2)+nSol:-Logba=log22=1
)Case2appliesand )T(n)=Ɵ(nlog(n))Example15:BinarySearchT(n)=T(n/2)+O(1)Sol:-Logba=log21=0
)
Case2appliesand )T(n)=Ɵ(log(n))Example16:BinarytreetraversalT(n)=2T(n/2)+O(1)Sol:-Logba=log22=1F(n)=1= )Case1appliesandT(n)=Ɵ(n)Example17:TakeanexampleT(n)=T(n/2)+n2Sol:-Logba=log22=1f(n)=n2=Case3appliesandT(n)=Ɵ(f(n))T(n)=O(n2)Example18:TakeanexampleT(n)=4T(n/2)+n2Sol:-Logba=log24=2f(n)=n2= )Case2appliesandT(n)= )T(n)=Ɵ(n2logn)
ModifiedMastertheoremThisisashortcuttosolvingthesameproblemeasilyandfast.IftherecurrencerelationisintheformofT(n)=aT(n/b)+dxs
Example19:T(n)=2T(n/2)+n2Sol:-r=log22s=2Case3:log22<sT(n)=Ɵ(f(n))=Ɵ(n2)
Example20:T(n)=T(n/2)+2nSol:-r=log21=0s=1Case3T(n)=Ɵ(n)Example21:T(n)=16T(n/4)+nSol:-r=2s=1Case1T(n)=Ɵ(n2)Example22:T(n)=2T(n/2)+nlognSol:-Thereislogninf(n)sousemastertheoremshortcutwillnotword.T(n)=nlog(n)= )
)=Ɵ(nlog(n))Example23:T(n)=2T(n/4)+n0.5Sol:-r=log42=0.5=s
Case2:)=Ɵ(n0.5log1.5n)
Example24:T(n)=2T(n/4)+n0.49Sol:-Case1:
=Ɵ(n0.5)Example25:T(n)=3T(n/3)+√nSol:-r=log33=1s=½Case1T(n)=Ɵ(n)Example26:T(n)=3T(n/4)+nlognSol:-Thereislogninf(n)soseeifmastertheorem.f(n)=nlogn= )Case3:T(n)=Ɵ(nlog(n))Example27:T(n)=3T(n/3)+n/2Sol:-r=1=sCase2:T(n)=Ɵ(nlog(n))
Exercise1.Trueorfalse
a)5n+10n2=O(n2)b)nlogn+4n=O(n)c)log(n2)+4log(logn)=O(logn)d)12n1/2+3=O(n2)e)3n+11n2+n20=O(2n)
2.Whatisthebest-caseruntimeComplexityofsearchinganarray?3.Whatistheaverage-caseruntimeComplexityofsearchinganarray?
CHAPTER3:APPROACHTOSOLVEALGORITHMDESIGNPROBLEMS
IntroductionKnowthetheoreticalknowledgeofthealgorithmisessential,butitisnotsufficient.Youneedtohaveasystematicapproachtosolveaproblem.Ourapproachisfundamentallydifferenttosolveanyalgorithmdesign question.Wewill follow a systematic five-step approach to reach to our solution.Master thisapproachandyouwillbebetterthanmostofthecandidatesininterviews.Fivestepsforsolvingalgorithmdesignquestionsare:1.Constraints2.IdeasGeneration3.Complexities4.Coding5.Testing
ConstraintsSolving a technical question is not just about knowing the algorithms and designing a good softwaresystem.The interviewerwants to knowyou approach towards any given problem.Many peoplemakemistakes as they do not ask clarifying questions about a given problem.They assumemany things andbeginworkingwiththat.Thereisdata,whichismissingthatyouneedtocollectfromyourinterviewerbeforebeginningtosolveaproblem.Inthisstep,youwillcapturealltheconstraintsabouttheproblem.Weshouldnevertrytosolveaproblemthatisnotcompletelydefined.Interviewquestionsarenotlikeexampaperwhereallthedetailsaboutaproblem arewell defined. In the interview, the interviewer actually expects you to ask questions andclarifytheproblem.Forexample:Whentheproblemstatementsaysthatwriteanalgorithmtosortnumbers.Thefirstinformationyouneedtocaptureiswhatkingofdata.LetussupposeinterviewerrespondwiththeanswerInteger.Thesecondinformationthatyouneedtoknowwhatisthesizeofdata.Youralgorithmdiffersiftheinputdatasizeif100integersor1billionintegers.BasicguidelinefortheConstraintsforanarrayofnumbers:1.Howmanynumbersofelementsinthearray?2.Whatistherangeofvalueineachelement?Whatistheminandmaxvalue?3.Whatisthekindofdataineachelementisitanintegerorafloatingpoint?4.Doesthearraycontainuniquedataornot?BasicguidelinefortheConstraintsforanarrayofstring:1.Howmanynumbersofelementsinthearray?2.Whatisthelengthofeachstring?Whatistheminandmaxlength?3.Doesthearraycontainuniquedataornot?BasicguidelinefortheConstraintsforaGraph1.Howmanynodesarethereinthegraph?2.Howmanyedgesarethereinthegraph?3.Isitaweightedgraph?Whatistherangeofweights?4.Isthegraphdirectedorundirected?5.Isthereisaloopinthegraph?6.Istherenegativesumloopinthegraph?7.Doesthegraphhaveself-loops?We have already seen this in graph chapter that depending upon the constraints the algorithm appliedchangesandsoisthecomplexityofthesolution.
IdeaGenerationWehavecoveredalotoftheoreticalknowledgeinthisbook.Itisimpossibletocoverallthequestionsasnewonesarecreatedeveryday.Therefore,weshouldknowhowtohandlenewproblems.Evenifyouknowthesolutionofaproblemaskedbytheinterviewerthenalsoyouneedtohaveadiscussionwiththeinterviewerandreachtothesolution.Youneedtoanalysetheproblemalsobecausetheinterviewermaymodifyaquestionalittlebitandtheapproachtosolveitwillvary.Well,howtosolveanewproblem?Thesolutiontothisproblemisthatyouneedtodoalotofpracticeandthemoreyoupracticethemoreyouwillbeabletosolveanynewquestion,whichcomeinfrontofyou.Whenyouhavesolvedenoughproblems,youwillbeabletoseeapatterninthequestionsandabletosolvenewproblemseasily.Followingisthestrategythatyouneedtofollowtosolveanunknownproblem:1.Trytosimplifythetaskinhand.2.Tryafewexamples3.Thinkofasuitabledata-structure.4.Thinkaboutsimilarproblemsyouhavealreadysolved.
TrytosimplifythetaskinhandLetuslookintothefollowingproblem:Husbandsandtheirwivesarestandinginrandominaline.TheyhavebeennumberedforhusbandsH1,H2,H3and soon.And their correspondingwiveshavenumberW1,W2,W3and soon.Youneed toarrangethemsothatH1willstandfirst,followedbyW1,thenH2followedbyW2andsoon.Atthefirstlook,itlooksdifficult,butitisasimpleproblem.Trytofindarelationofthefinalposition.P(Hi)=i*2-1P(Wi)=i*2TherestofthealgorithmweareleavingyoutodosomethinglikeInsertion-Sortandyouaredone.
TryafewexamplesInthesameaboveproblemifyouhavetrieditwithsomeexamplefor3husbandandwifepairthenyoumayhavereached to thesameformula thatwehaveshownin theprevioussection.Sometimethinkingsomemoreexamplestrytosolvetheproblemathand.
Thinkofasuitabledata-structureForsomeproblems,itisstraightforwardwhichdatastructureismostsuitable.Forexample,ifwehaveaproblemfindingmin/maxofsomegivenvalue,thenprobablyheapisthedatastructurewearelookingfor.Wehaveseenanumberofdatastructurethroughoutthisbook.Inaddition,wehavetofigureoutwhichdata-structurewillsuiteourneed.Letuslookintoaproblem:Wearegivenastreamofdataatanytimewecanbeaskedtotellthemedianvalueofthedataandmaybewecanbeaskedtopopmediandata.
Wecanthinkaboutsomesortoftree,maybebalancedtreewheretherootisthemedian.Waitbutitisnotsoeasytomakesurethetreeroottobeamedian.Aheapcangiveusminimumormaximumsowecannotgetthedesiredresultfromittoo.However,whatifweuse twoheaponemaxheap andonemin heap.The smaller valueswill go tomin heap and thebiggervalueswillgotomaxheap.Inaddition,wecankeepthecountofhowmanyelementsarethereintheheap.Therestofthealgorithmyoucanthinkyourself.For every new problem think about the data structure, you know and may be one of them or somecombinationofthemwillsolveyourproblem.ThinkaboutsimilarproblemsyouhavealreadysolvedLetussupposeyouaregiventwolinkedlistheadpointerandtheymeetatsomepointandneedtofindthepointof intersection.However, inplaceoftheendofboththelinkedlist tobeanullpointer thereisaloop.Youknowhowtofindintersectionpointoftwointersectinglinkedlist,youknowhowtofindifalinkedlist have a loop (three-pointer solution). Therefore, you can apply both of these solutions to find thesolutionoftheprobleminhand.
ComplexitiesSolving a problem is not just finding a correct solution. The solution should be fast and should havereasonablememory requirement.Youhavealready readaboutBig-Onotation in thepreviouschapters.You shouldbe able todoBig-Oanalysis. In caseyou think the solutionyouhaveprovided is not thatoptimalandthereissomemoreefficientsolution,thenthinkagainandtrytofigureoutthisinformation.MostinterviewersexpectthatyoushouldbeabletofindthetimeandSpaceComplexityofthealgorithms.YoushouldbeabletocomputethetimeandSpaceComplexityinstantly.Wheneveryouaresolvingsomeproblem,youshouldfindthecomplexityassociatedwithitfromthisyouwouldbeabletochoosethebestsolutions.Insomeproblemsthereissometrade-offsbetweenspaceandTimeComplexity,soyoushouldknowthesetrade-offs.Sometimetakingsomebitmorespacesavesalotoftimeandmakeyouralgorithmmuchfaster.
CodingAt this point, you have already captured all the constraints of the problem, proposed few solutions,evaluatedthecomplexitiesofthevarioussolutionsandpickedtheonesolutiontodofinalcoding.Neverever,jumpintocodingbeforediscussingconstraints,Ideagenerationandcomplexitywiththeinterviewer.WeareaccustomedtocodinginanIDElikevisualstudio.Somanypeoplestrugglewhenaskedtowritecodeonawhiteboardorsomeblanksheet.Therefore,weshouldhavealittlepracticetothecodingonasheetofpaper.Youshouldthinkbeforecodingbecausethereisnobackbuttoninsheetofpaper.Alwaystrytowritemodularcode.Smallfunctionsneedtobecreatedsothatthecodeiscleanandmanaged.Ifthere is a swap function so just use this function and tell the interviewer that youwill write it later.Everybodyknowsthatyoucanwriteswapcode.
TestingOncethecodeiswritten,youarenotdone.Itismostimportantthatyoutestyourcodewithseveralsmalltest cases. It shows that you understand the importance of testing. It also gives confidence to yourinterviewerthatyouarenotgoingtowriteabuggycode.Onceyouaredonewith,yourcodingitisagoodpracticethatyougothroughyourcodelinebylinewithsomesmalltestcase.Thisisjusttomakesureyourcodeisworkingasitissupposedtowork.Youshouldtestfewtestcases.Normaltestcases:Thesearethepositivetestcases,whichcontainthemostcommonscenario,andfocusisontheworkingofthebaselogicofthecode.Forexample,ifwe`aregoingtowritesomealgorithmforlinkedlist,thenthismaycontainwhatwillhappenwhenalinkedlistwith3or4nodesisgivenasinput.Thesetestcasesyoushouldalwaysruninyourheadbeforesayingthecodeisdone.Edgecases:Theseare the testcases,whicharegoing to test theboundariesof thecode.For thesamelinkedlistalgorithm,edgecasesmaybehowthecodebehaveswhenanemptylistispassedorjustonenodeispassed.Thesetestcasesyoushouldalwaysruninyourheadbeforesayingthecodeisdone.Edgecasesmayhelptomakeyourcodemorerobust.Justfewchecksneedtobeaddedtothecodetotakecareofthecondition.Loadtesting:Inthiskindoftest,yourcodewillbetestedwithahugedata.Thiswillallowustotestifyourcodeisslowortoomuchmemoryintensive.Alwaysfollowthesefivestepsnever jumptocodingbeforedoingconstraintanalysis, ideageneration,andComplexityAnalysis:.Atleastnever,missthetestingphase.
ExampleLetussupposetheintervieweraskyoutogiveabestsortingalgorithm.Some interviewee will directly jump to Quick-SortO(nlogn). Oops, mistake you need to ask manyquestionsbeforebeginningtosolvethisproblem.Questions1:Whatisthetypeofdata?Aretheyintegers?Answer:Yes,theyareintegers.Questions2:Howmuchdataarewegoingtosort?Answer:Maybethousands.Questions3:Whatexactlyisthisdataabout?Answer:Theystoreaperson’sageQuestions4:Whatkindofdata-structureusedtoholdthisdata?Answer:DataaregivenintheformofsomearrayQuestions5:Canwemodifythegivendata-structure?Inaddition,many,manymore…?Answer:No,youcannotmodifythedatastructureprovidedOkfromthefirstanswer,wewilldeducethatthedataisinteger.Thedataisnotsobigitjustcontainsafewthousandentries.Thethirdanswerisinterestingfromthiswededucethattherangeofdatais1-150.Dataisprovidedinanarray.Fromfifthsanswerwededucethatwehavetocreateourowndatastructureandwecannotmodifythearrayprovided.Sofinally,weconclude,wecanjustusebucketsorttosortthedata.Therangeisjust1-150soweneedjust151-capacityintegralarray.DataisunderthousandssowedonothavetoworryaboutdataoverflowandwegetthesolutioninlineartimeO(N).Note:Wewillreadsortinginthecomingchapters.
SummaryAtthispoint,youknowtheprocessofhandlingnewproblemsverywell.Inthecomingchapterwewillbelookingintoalotofvariousdatastructureandtheproblemstheysolve.Ahugenumberofproblemsaresolved in thisbook.However, is recommendedso first try tosolve thembyyourself, then lookfor thesolution.Alwaysthinkaboutthecomplexityoftheproblem.Intheinterviewinteractionisthekeytogetproblemdescribedcompletelyanddiscussyourapproachwiththeinterviewer.
CHAPTER4:ABSTRACTDATATYPE
Abstractdatatype(ADT)Anabstractdatatype(ADT)isalogicaldescriptionofhowweviewthedataandtheoperationsthatareallowedonit.ADTisdefinedasauserpointofviewofadatatype.ADTconcernsaboutthepossiblevaluesofthedataandwhatareinterfaceexposedbyit.ADTdoesnotconcernabouttheactualimplementationofthedatastructure.Forexample,auserwantstostoresomeintegersandfindameanofit.Doesnottalkabouthowexactlyitwillbeimplemented.
Data-StructureDatastructuresareconcreterepresentationsofdataandaredefinedasaprogrammerpointofview.Data-structurerepresentshowdatawillbestoredinmemory.Alldata-structureshavetheirownprosandcons.Dependingupontheproblemathand,wepickadata-structurethatisbestsuitedforit.Forexample,wecanstoredatainanarray,alinked-list,stack,queue,tree,etc.
Array
Array represent a collection of multiple elements of the same datatype. Arrays are fixed size datastructure,thesizeofthisdatastructuremustbeknownatcompiletimeandcannotbechangedafterthat.Arraysarethemostcommondatastructureusedtostoredata.Aswecannotchangethesizeofanarray,wegenerallydeclarelargesizearraytohandleanyfuturedataexpansion.Thisendsupincreatinglargesizearray,wheremostofthespaceisunused.
ArrayADTOperationsBelowistheAPIofarray:Insert(x,k):addsanelementatkthpositionIfwewant to just store value k at index x, andwedo not care about the value already stored in thatlocationthanthisoperationisdoneinO(1)constanttime.However, if we care about the previous values stored in an array, so insertions and deletions of anelementistimeconsumingaswehavetoshiftotherelementsbyonepositionrespectively.Inthiscase,insertionanddeletiontakeO(n)time.Delete(k):deleteelementatkthpositionIfwewanttomarkthatthereisnovaluestoredatindexk,thisoperationisdoneinO(1)constanttime.However, if we care about the other values stored in an array, so deletions of an element are timeconsumingaswehave to shift other elementsbyoneposition respectively. In this case,deletion takesO(n)time.FindKth(k):findelementatpositionkThebiggestadvantageofthearrayisthatifweknowindexofanelementthenitcanbeaccessedinO(1)TimeComplexity.WejustneedtoreturnA[k].Find(x):findpositionofelementIfthearrayisunsorted,thenfind(x)anelementwilltakeO(n)TimeComplexity.Ifthearrayissorted,thenfind(x)isfastusingbinarysearchandwilltakeO(logn)TimeComplexity.PrintList():displayalltheelementsinthelistPrintListJustrunthroughthearrayandprintoneelementatatime.Usesoneloop.LineartimeO(n).IsEmpty():checkifnumberofelementsarezeroSearchingifthereisnoelementstoredinarrayalsotakelineartimeO(n).
LinkedList
Linkedlistsaredynamicdatastructureandmemoryisallocatedatruntime.Theconceptoflinkedlistisnottostoredatacontiguously.Uselinksthatpointtothenextelements.Performance wise linked lists are slower than arrays because there is no direct access to linked listelements.Thelinkedlistisausefuldatastructurewhenwedonotknowthenumberofelementstobestoredaheadoftime.Therearemanyflavoursoflinkedlist:linear,circular,doubly,anddoublycircular.
LinkedListADTOperationsBelowistheAPIofLinkedlist.Insert(k):addsktothestartofthelistInsertanelementat thestartof the list. Justcreateanewelementandmovepointers.So that thisnewelementbecomesthenewelementofthelist.ThisoperationwilltakeO(1)constanttime.Delete():deleteelementatthestartofthelistDeleteanelementatthestartofthelist.Wejustneedtomoveonepointer.ThisoperationwillalsotakeO(1)constanttime.PrintList():displayalltheelementsofthelist.Startwiththefirstelementandthenfollowthepointers.ThisoperationwilltakeO(N)time.Find(k):findthepositionofelementwithvaluekStartwiththefirstelementandfollowthepointeruntilwegetthevaluewearelookingfororreachtheendofthelist.ThisoperationwilltakeO(N)time.Note:binarysearchdoesnotworkonlinkedlists.FindKth(k):findelementatpositionkStartfromthefirstelementandfollowthelinksuntilyoureachthekthelement.ThisoperationwilltakeO(N)time.IsEmpty():checkifthenumberofelementsinthelistarezero.JustchecktheheadpointerofthelistitshouldbeNull.Whichmeanslistisempty.ThisoperationwilltakeO(1)time.
Stack
StackisaspecialkindofdatastructurethatfollowsLast-In-First-Out(LIFO)strategy.Thismeansthattheelementthatisaddedtostacklastwillbethefirsttoberemoved.Thevariousapplicationsofstackare:1.Recursion:recursivecallsarepreformedusingsystemstack.2.Postfixevaluationofexpression.3.Backtracking4.Depth-firstsearchoftreesandgraphs.5.Convertingadecimalnumberintoabinarynumberetc.
StackADTOperationsPush(k):AddsanewitemtothetopofthestackPop():Removeanelementfromthetopofthestackandreturnitsvalue.Top():ReturnsthevalueoftheelementatthetopofthestackSize():ReturnsthenumberofelementsinthestackIsEmpty():determineswhetherthestackisempty.Itreturns1ifthestackisemptyorreturn0.Note:AlltheaboveStackoperationsareimplementedinO(1)TimeComplexity.
Queue
AqueueisaFirst-In-First-Out(FIFO)kindofdatastructure.Theelementthatisaddedtothequeuefirstwillbethefirsttoberemovedfromthequeueandsoon.Queuehasthefollowingapplicationuses:1.Accesstosharedresources(e.g.,printer)2.Multiprogramming3.Messagequeue
QueueADTOperations:Enqueue():Addanewelementtothebackofthequeue.Dequeue():removeanelementfromthefrontofthequeueandreturnitsvalue.Front():returnthevalueoftheelementatthefrontofthequeue.Size():returnsthenumberofelementsinsidethequeue.IsEmpty():returns1ifthequeueisemptyotherwisereturn0Note:AlltheaboveQueueoperationsareimplementedinO(1)TimeComplexity.
TreesTreeisahierarchicaldatastructure.Thetopelementofa treeiscalledtherootof thetree.Except therootelement,everyelementinatreehasaparentelement,andzeroormorechildelements.Thetreeisthemostusefuldatastructurewhenyouhavehierarchicalinformationtostore.Therearemanytypesoftrees,forexample,binary-tree,Red-blacktree,AVLtree,etc.
BinaryTreeAbinarytreeisatypeoftreeinwhicheachnodehasatmosttwochildren(0,1or2)whicharereferredasleftchildandrightchild.
BinarySearchTrees(BST)
Abinarysearchtree(BST)isabinarytreeonwhichnodesareorderedinthefollowingway:1.Thekeyintheleftsubtreeislessthanthekeyinitsparentnode.2.Thekeyintherightsubtreeisgreaterorequalthekeyinitsparentnode.
BinarySearchTreeADTOperationsInsert(k):Insertanelementkintothetree.Delete(k):Deleteanelementkfromthetree.Search(k):Searchaparticularvaluekintothetreeifitispresentornot.FindMax():Findthemaximumvaluestoredinthetree.FindMin():Findtheminimumvaluestoredinthetree.TheaverageTimeComplexityofall theaboveoperationsonabinarysearchtree isO(logn), thecasewhenthetreeisbalanced.Theworst-caseTimeComplexitywillbeO(n)whenthetreeisskewed.Abinarytreeisskewedwhentreeisnotbalanced.Therearetwotypesofskewedtree.1.RightSkewedbinarytree:Abinarytreeinwhicheachnodeishavingeitheronlyarightchildorno
childatall.2.LeftSkewedbinarytree:Abinarytreeinwhicheachnodeishavingeitheronlyaleftchildornochild
atall.
BalancedBinarysearchtreeTherearefewbinarysearchtree,whichalwayskeepsthemselvesbalanced.MostimportantamongthemareRed-BlackTree(RB-Tree)andAVLtree.Thestandardtemplatelibrary(STL)isimplementedusingthisRed-BlackTree(RB-Tree).
PriorityQueue(Heap)
Priorityqueueisimplementedusingabinaryheapdatastructure.Inaheap,therecordsarestoredinanarraysothateachkeyislargerthanitstwochildrenkeys.Eachnodeintheheapfollowthesamerulethattheparentvalueisgreaterthanitschildren.Therearetwotypesoftheheapdatastructure:1.Maxheap:eachnodeshouldbegreaterthanorequaltoeachofitschildren.2.Minheap:eachnodeshouldbesmallerthanorequaltoeachofitschildren.Aheapisausefuldatastructurewhenyouwanttogetmax/minonebyonefromdata.Heap-Sortusesmaxheaptosortdatainincreasing/decreasingorder.
HeapADTOperationsInsert()-Addinganewelementtotheheap.TheTimeComplexityofthisoperationisO(log(n))Extract()-Extractingmaxformaxheapcase(orminforminheapcase).TheTimeComplexityofthisoperationisO(log(n))Heapify()–Toconvertalistofnumbersinanarrayintoaheap.ThisoperationhasaTimeComplexityO(n)Delete()–Deleteanelementfromtheheap.TheTimeComplexityofthisoperationisO(log(n))
Hash-Table
AHash-Table isadatastructure thatmapskeys tovalues.Eachpositionof theHash-Table iscalledaslot.TheHash-Tableusesahash function tocalculatean indexofanarrayof slots.Weuse theHash-Tablewhenthenumberofkeysactuallystoredissmallrelativelytothenumberofpossiblekeys.Theprocessofstoringobjectsusingahashfunctionisasfollows:1.CreateanarrayofsizeMtostoreobjects,thisarrayiscalledHash-Table.2.Findahashcodeofanobjectbypassingitthroughthehashfunction.3.TakemoduleofhashcodebythesizeofHash-Tabletogettheindexofthetablewhereobjectswillbestored.4.Finallystoretheseobjectsinthedesignatedindex.TheprocessofsearchingobjectsinHash-Tableusingahashfunctionisasfollows:1.Findahashcodeoftheobjectwearesearchingforbypassingitthroughthehashfunction.2.Takemoduleofhashcodeby thesizeofHash-Table toget the indexof the tablewhereobjectsarestored.3.Finally,retrievetheobjectfromthedesignatedindex.
Hash-TableAbstractDataType(ADT)ADTofHash-Tablecontainsthefollowingfunctions:Insert(x):Addobjectxtothedataset.Delete(x):Deleteobjectxfromthedataset.Search(x):Searchobjectxindataset.TheHash-Tableisausefuldatastructureforimplementingdictionary.TheaveragetimetosearchforanelementinaHash-TableisO(1).AHashTablegeneralizesthenotionofanarray.
Dictionary/SymbolTableAsymboltableisamappingbetweenastring(key)andavalue,whichcanbeofanydatatype.Avaluecanbeanintegersuchasoccurrencecount,dictionarymeaningofawordandsoon.
BinarySearchTree(BST)forStringsBinarySearchTree(BST)isthesimplestwaytoimplementsymboltable.Simplestringcomparefunctioncan be used to compare two strings. If all the keys are random, and the tree is balanced. Then on anaveragekeylookupcanbedoneinlogarithmictime.
Hash-TableTheHash-Table is another data structure,which can be used for symbol table implementation.BelowHash-Tablediagram,wecanseethenameofthatpersonistakenasthekey,andtheirmeaningisthevalueofthesearch.Thefirstkeyisconvertedintoahashcodebypassingittoappropriatehashfunction.InsidehashfunctionthesizeofHash-Tableisalsopassed,whichisusedtofindtheactualindexwherevalueswillbestored.Finally,thevaluethatismeaningofnameisstoredintheHash-Table,oryoucanstoreareferencetothestringwhichstoremeaningcanbestoredintotheHash-Table.
Hash-Tablehasanexcellentlookupofconstanttime.Let us supposewewant to implement autocomplete the box feature ofGoogle search.When you typesomestringtosearchingooglesearch,itproposesomecompletestringevenbeforeyouhavedonetyping.BSTcannotsolvethisproblemasrelatedstringscanbeinbothrightandleftsubtree.TheHash-Tableisalsonotsuitedfor this job.OnecannotperformapartialmatchorrangequeryonaHash-Table.Hash function transforms string to a number.Moreover, a good hash functionwill give adistributedhashcodeevenforpartialstringandthereisnowaytorelatetwostringsinaHash-Table.Trie and Ternary Search tree are a special kind of tree, which solves partialmatch, and range queryproblemwell.
TrieTrieisatree,inwhichwestoreonlyonecharacterateachnode.Thisfinalkeyvaluepairisstoredintheleaves.EachnodehasKchildren,oneforeachpossiblecharacter.Forsimplicitypurpose,letusconsiderthatthecharactersetis26,correspondstodifferentcharactersofEnglishalphabets.Trie is an efficient data structure. Using Trie, we can search the key in O(M) time.WhereM is themaximumstringlength.Trieisalsosuitableforsolvingpartialmatchandrangequeryproblems.
TernarySearchTrie/TernarySearchTreeTrieshavingaverygoodsearchperformanceofO(M)whereMisthemaximumsizeofthesearchstring.However,trieshavingveryhighspacerequirement.EverynodeTriecontainreferencestomultiplenodes,each reference corresponds to possible characters of the key. To avoid this high space requirementTernarySearchTrie(TST)isused.
A TST avoid the heavy space requirement of the traditional Trie while still keeping many of itsadvantages. In aTST, eachnode contains a character, an endofkey indicator, and threepointers.Thethree pointers are corresponding to current char hold by the node(equal), characters less than andcharactergreaterthan.TheTimeComplexityofternarysearchtreeoperationisproportionaltotheheightoftheternarysearchtree.Intheworstcase,weneedtotraverseupto3timesthatmanylinks.However,thiscaseisrare.Therefore,TSTisaverygoodsolutionforimplementingSymbolTable,Partialmatchandrangequery.
Graphs
Agraphisadatastructurewhichrepresentsanetwork,thatconnectsacollectionofnodescalledvertices,andthereconnections,callededges.Anedgecanbeseenasapathbetweentwonodes.Theseedgescanbeeitherdirectedorundirected.Ifapathisdirectedthenyoucanmoveonlyinonedirection,whileinanundirectedpathyoucanmoveinboththedirections.
GraphAlgorithms
Depth-FirstSearch(DFS)TheDFSalgorithmwestartfromstartingpointandgointodepthofgraphuntilwereachadeadendandthenmoveup toparentnode(Backtrack). InDFS,weusestack toget thenextvertex tostartasearch.Alternatively,wecanuserecursion(systemstack)todothesame.
Breadth-FirstSearch(BFS)InBFS algorithm, a graph is traversed in layer-by-layer fashion. The graph is traversed closer to thestartingpoint.ThequeueisusedtoimplementBFS.
SortingAlgorithms
Sortingistheprocessofplacingelementsfromacollectionintoascendingordescendingorder.Sortingarrangesdataelementsinordersothatsearchingbecomeeasier.TherearegoodsortingfunctionsavailablewhichdoessortinginO(nlogn)time,sointhisbookwhenweneedsortingwewillusesort()functionandwillassumethatthesortingisdoneinO(nlogn)time.
CountingSortCountingsortisthesimplestandmostefficienttypeofsorting.Countingsorthasastrictrequirementofapredefinedrangeofdata.Like,sorthowmanypeopleareinwhichagegroup.Weknowthattheageofpeoplecanvarybetween1and130.
Ifweknowtherangeofinput,thensortingcanbedoneusingcountinginO(n+k).
http://www.bogotobogo.com/Algorithms/countingsort.php
EndnoteThis chapter have provided a brief introduction of the various data structures, algorithms and theircomplexities.Inthecomingchapterswewilllookintoallthesedatastructureindetails.Ifyouknowtheinterface of the various data structures, then you can use them while solving other problems withoutknowingtheinternaldetailshowtheyareimplemented.
CHAPTER5:SEARCHING
IntroductionInComputerScience,Searchingisthealgorithmicprocessoffindingaparticulariteminacollectionofitems.Theitemmaybeakeywordinafile,arecordinadatabase,anodeinatreeoravalueinanarrayetc.
WhySearching?Imagineyouareinalibrarywithmillionsofbooks.Youwanttogetaspecificbookwithspecifictitle.Howwill you find?Youwill just start searching by initial letter of the book title. Then you continuematchingwithawholebooktitleuntilyoufindyourdesiredbook.(Bydoingthissmallheuristicyouhavereducedthesearchspacebyafactorof26,considerwehaveanequalnumberofbookswhosetitlebeginwithparticularchar.)Similarly,computerstores lotsof informationand to retrieve this informationefficiently,weneedveryefficientsearchingalgorithms.Tomakesearchingefficient,wekeepthedatainsomeproperorder.Therearecertainwaysoforganizingthedata.Ifyoukeepthedatainproperorder,itiseasytosearchrequiredelement.Forexample,Sortingisoneoftheprocessformakingdataorganized.
DifferentSearchingAlgorithms·LinearSearch–UnsortedInput·LinearSearch–SortedInput·BinarySearch(SortedInput)·StringSearch:Tries,SuffixTrees,TernarySearch.·HashingandSymbolTables
LinearSearch–UnsortedInputWhenelementsofanarrayarenotorderedorsortedandwewant tosearchforaparticularvalue,weneedtoscanthefullarrayunlesswefindthedesiredvalue.Thiskindofalgorithmknownasunorderedlinear search.Themajor problemwith this algorithm is less performance or highTimeComplexity inworstcase.Example5.1intlinearSearchUnsorted(intarr[],intsize,intvalue){
inti=0;for(i=0;i<size;i++){if(value==arr[i])returni;}return-1;
}TimeComplexity:O(n).Asweneedtotraversethecompletearrayinworstcase.Worstcaseiswhenyourdesiredelementisatthelastpositionofthearray.Here,‘n’isthesizeofthearray.SpaceComplexity:O(1).Noextramemoryisusedtoallocatethearray.
LinearSearch–SortedIf elements of the array are sorted either in increasing order or in decreasing order, searching for adesiredelementwillbemuchmoreefficientthanunorderedlinearsearch.Inmanycases,wedonotneedtotraversethecompletearray.Followingexampleexplainswhenyouencounteragreaterelementfromthe increasing sorted array, you stop searching further. This is how this algorithm saves the time andimprovestheperformance.Example5.2intlinearSearchSorted(intarr[],intsize,intvalue){
inti=0;for(i=0;i<size;i++){if(value==arr[i])returni;elseif(value<arr[i])return-1;}return-1;
}TimeComplexity:O(n).Asweneedtotraversethecompletearrayinworstcase.Worstcaseiswhenyour desired element is at the last position of the sorted array. However, in the average case thisalgorithmismoreefficienteventhoughthegrowthrateissameasunsorted.SpaceComplexity:O(1).Noextramemoryisusedtoallocatethearray.
BinarySearchHowdowesearchawordinadictionary?Ingeneral,wegotosomeapproximatepage(mostlymiddle)andstartsearchingfromthatpoint. Ifweseethewordthatwearesearchingissamethenwearedonewiththesearch.Else,ifweseethepageisbeforetheselectedpages,thenapplythesameprocedureforthefirsthalfotherwisetothesecondhalf.BinarySearchalsoworksinthesameway.Wegetthemiddlepointfromthesortedarrayandstartcomparingwiththedesiredvalue.Note:Binarysearchrequiresthearraytobesortedotherwisebinarysearchcannotbeapplied.Example5.3/*BinarySearchAlgorithm–IterativeWay*/intBinarysearch(intarr[],intsize,intvalue){
intlow=0;inthigh=size-1;intmid;while(lowhigh)return-1;intmid=low+(high-low)/2;/*Toavoidtheoverflow*/if(arr[mid]==value)returnmid;elseif(arr[mid]<value)
returnBinarySearchRecursive(arr,mid+1,high,value);elsereturnBinarySearchRecursive(arr,low,mid-1,value);
}TimeComplexity:O(logn).SpaceComplexity:O(logn)forsystemstackinrecursion
StringSearchingAlgorithmsReferStringchapter.
HashingandSymbolTablesReferHash-Tablechapter.
HowsortingisusefulinSelectionAlgorithm?Selectionproblemscanbeconvertedtosortingproblems.Oncethearrayissorted,itiseasytofindtheminimum/maximum(ordesiredelement)fromthesortedarray.Themethod‘SortingandthenSelecting’isinefficientforselectingasingleelement,butitisefficientwhenmanyselectionsneedtobemadefromthearray. It is because only one initial expensive sort is needed, followed by many cheap selectionoperations.Forexample,ifwewanttogetthemaximumelementfromanarray.Aftersortingthearray,wecansimplyreturnthelastelementfromthearray.Whatifwewanttogetsecondmaximum.Now,wedonothavetosort thearrayagainandwecanreturn thesecondlastelementfromthesortedarray.Similarly,wecanreturnthekthmaximumelementbyjustonescanofthesortedlist.Sowiththeabovediscussion,sortingisusedtoimprovetheperformance.IngeneralthismethodrequiresO(nlogn)(forsorting)time.Withtheinitialsorting,wecanansweranyqueryinonescan,O(n).
ProblemsinSearching
PrintDuplicatesinArrayGivenanarrayofnnumbers,printtheduplicateelementsinthearray.Firstapproach:Exhaustive searchorBrute force, for eachelement in array find if there is someotherelementwiththesamevalue.Thisisdoneusingtwoforloop,firstlooptoselecttheelementandsecondlooptofinditsduplicateentry.Example5.5voidprintRepeating(intarr[],intsize){
inti,j;printf("Repeatingelementsare");for(i=0;i<size;i++)for(j=i+1;j<size;j++)if(arr[i]==arr[j])printf("%d",arr[i]);
}TheTimeComplexityisO(n2)andSpaceComplexityisO(1)Secondapproach:Sorting,Sortalltheelementsinthearrayandafterthisinasinglescan,wecanfindtheduplicates.Example5.6voidprintRepeating(intarr[],intsize){
inti;Sort(arr,size);printf("Repeatingelementsare");for(i=1;i<size;i++){if(arr[i]==arr[i-1])printf("%d",arr[i]);}
}SortingalgorithmstakeO(n.log(n))timeandsinglescantakeO(n)time.TheTimeComplexityofanalgorithmisO(n.log(n)) andSpaceComplexityisO(1)Thirdapproach:Hash-Table,usingHash-Table,wecankeeptrackoftheelementswehavealreadyseenandwecanfindtheduplicatesinjustonescan.Example5.7
voidprintRepeating(intarr[],intsize){
HashTableh;inti;printf("Repeatingelementsare");for(i=0;i<size;i++){if(findValue(h,arr[i]))printf("%d",arr[i]);elseaddValue(h,arr[i]);}
}Hash-TableinsertandfindtakeconstanttimeO(1)sothetotalTimeComplexityofthealgorithmisO(n)time.SpaceComplexityisalsoO(n)Forthapproach:Counting,thisapproachisonlypossibleifweknowtherangeoftheinput.Ifweknowthat,theelementsinthearrayareintherange0ton-1.Wecanreserveandarrayoflengthnandwhenweseeanelementwecanincreaseitscount.Injustonesinglescan,weknowtheduplicates.Ifweknowtherangeoftheelements,thenthisisthefastestwaytofindtheduplicates.Example5.8voidprintRepeating(intarr[],intsize){
int*count=(int*)calloc(sizeof(int),size);inti;printf("Repeatingelementsare");for(i=0;i<size;i++){if(count[arr[i]]==1)printf("%d",arr[i]);elsecount[arr[i]]++;}
}Counting approach just uses an array so insert and find take constant time O(1) so the total TimeComplexityofthealgorithmisO(n)time.SpaceComplexityforcreatingcountarrayisalsoO(n)
Findmax,appearingelementinanarrayGivenanarrayofnnumbers,findtheelement,whichappearsmaximumnumberoftimes.Firstapproach:Exhaustive search orBrute force, for each element in array find howmany times this
particularvalueappears inarray.Keep trackof themaxCountandwhensomeelementcount isgreaterthanmaxCountthenupdatethemaxCount.Thisisdoneusingtwoforloop,firstlooptoselecttheelementandsecondlooptocounttheoccurrenceofthatelement.TheTimeComplexityisO(n2),andSpaceComplexityisO(1)Example5.9intgetMax(intarr[],intsize){
inti,j;intmax=arr[0],count=1,maxCount=1;
for(i=0;i<size;i++){count=1;for(j=i+1;j<size;j++)if(arr[i]==arr[j])count++;if(count>maxCount){max=arr[i];maxCount=count;}}returnmax;
}Secondapproach:Sorting,Sortalltheelementsinthearrayandafterthisinasinglescan,wecanfindthecounts.SortingalgorithmstakeO(n.log(n))timeandsinglescantakeO(n)time.TheTimeComplexityofanalgorithmisO(n.log(n)) andSpaceComplexityisO(1)Example5.10intgetMax(intarr[],intsize){
intmax=arr[0],maxCount=1,curr=arr[0],currCount=1;inti;Sort(arr,size);for(i=1;i<size;i++){if(arr[i]==arr[i-1])currCount++;else{currCount=1;curr=arr[i];}
if(currCount>maxCount){maxCount=currCount;max=curr;}}returnmax;
}Thirdapproach:Counting,Thisapproachisonlypossibleifweknowtherangeoftheinput.Ifweknowthat,theelementsinthearrayareintherange0ton-1.Wecanreserveandarrayoflengthnandwhenweseeanelementwecanincreaseitscount.Injustonesinglescan,weknowtheduplicates.Ifweknowtherangeoftheelements,thenthisisthefastestwaytofindthemaxcount.Counting approach just uses array so to increase count take constant time O(1) so the total TimeComplexityofthealgorithmisO(n)time.SpaceComplexityforcreatingcountarrayisalsoO(n)Example5.11#defineN100intgetMax(intarr[],intsize){
intmax=arr[0],maxCount=1;int*count=(int*)calloc(sizeof(int),N);inti;
for(i=0;i<size;i++){count[arr[i]]++;if(count[arr[i]]>maxCount){maxCount=count[arr[i]];max=arr[i];}}returnmax;
}
Majoritye lementinanArrayGivenanarrayofnelements.Findthemajorityelement,whichappearsmorethann/2times.Return0incasethereisnomajorityelement.Firstapproach:Exhaustive search orBrute force, for each element in array find howmany times thisparticularvalueappears inarray.Keep trackof themaxCountandwhensomeelementcount isgreaterthanmaxCountthenupdatethemaxCount.Thisisdoneusingtwoforloop,firstlooptoselecttheelement
andsecondlooptocounttheoccurrenceofthatelement.Oncewehave the final,maxCountwe can see if it is greater than n/2, if it is greater thanwehave amajorityifnotwedonothaveanymajority.TheTimeComplexityisO(n2)+O(1)=O(n2)andSpaceComplexityisO(1)Example5.12intgetMajority(intarr[],intsize){
inti,j;intmax=0,count=0,maxCount=0;for(i=0;i<size;i++){for(j=i+1;j<size;j++)if(arr[i]==arr[j])count++;if(count>maxCount){max=arr[i];maxCount=count;}}if(maxCount>size/2)returnmax;elsereturn0;
}Secondapproach:Sorting,Sortalltheelementsinthearray.Ifthereisamajoritythanthemiddleelementattheindexn/2mustbethemajoritynumber.Sojustsinglescancanbeusedtofinditscountandseeifthemajorityisthereornot.SortingalgorithmstakeO(n.logn)timeandsinglescantakeO(n)time.TheTimeComplexityofanalgorithmisO(n.logn) andSpaceComplexityisO(1)Example5.13intgetMajority(intarr[],intsize){
intmajIndex=size/2,count=1;inti;intcandidate;Sort(arr,size);candidate=arr[majIndex];count=0;for(i=0;i<size;i++)
if(arr[i]==candidate)count++;if(count>size/2)returnarr[majIndex];elsereturn0;
}Thirdapproach: This is a cancelation approach (Moore’sVotingAlgorithm), if all the elements standagainstthemajorityandeachelementiscancelledwithoneelementofmajorityifthereismajoritythenmajorityprevails.·Setthefirstelementofthearrayasmajoritycandidateandinitializethecounttobe1.·Startscanningthearray.
oIfwegetsomeelementwhosevaluesameasamajoritycandidate,thenweincreasethecount.oIfwegetanelementwhosevalueisdifferentfromthemajoritycandidate,thenwedecrementthe
count.oIfcountbecome0,thatmeanswehaveanewmajoritycandidate.Makethecurrentcandidateas
majoritycandidateandresetcountto1.oAttheend,wewillhavetheonlyprobablemajoritycandidate.
·Nowscanthroughthearrayonceagaintoseeifthatcandidatewefoundabovehaveappearedmorethann/2times.
Countingapproachjustscansthrowarraytwotimes.TheTimeComplexityofthealgorithmisO(n)time.SpaceComplexityforcreatingcountarrayisalsoO(1)Example5.14intgetMajority(intarr[],intsize){
intmajIndex=0,count=1;inti;intcandidate;for(i=1;i<size;i++){if(arr[majIndex]==arr[i])count++;elsecount--;if(count==0){majIndex=i;count=1;}}candidate=arr[majIndex];count=0;
for(i=0;i<size;i++)if(arr[i]==candidate)count++;if(count>size/2)returnarr[majIndex];elsereturn0;
}
FindthemissingnumberinanArrayGivenanarrayofn-1elements,whichareintherangeof1ton.Therearenoduplicatesinthearray.Oneoftheintegerismissing.Findthemissingelement.Firstapproach:ExhaustivesearchorBruteforce,foreachvalueintherange1ton,findifthereissomeelementinarraywhichhavethesamevalue.Thisisdoneusingtwoforloop,firstlooptoselectvalueintherange1tonandthesecondlooptofindifthiselementisinthearrayornot.TheTimeComplexityisO(n2)andSpaceComplexityisO(1)Example5.15intfindMissingNumber(intarr[],intsize){
inti,j,found=0;for(i=1;i
Thirdapproach:Hash-Table,usingHash-Table,wecankeeptrackoftheelementswehavealreadyseenandwecanfindthemissingelementinjustonescan.Hash-TableinsertandfindtakeconstanttimeO(1)sothetotalTimeComplexityofthealgorithmisO(n)time.SpaceComplexityisalsoO(n)Forthapproach:Counting,weknowtherangeoftheinputsocountingwillwork.Asweknowthat, theelementsinthearrayareintherange0ton-1.Wecanreserveandarrayoflengthnandwhenweseeanelementwecanincreaseitscount.Injustonesinglescan,weknowthemissingelement.Counting approach just uses an array so insert and find take constant time O(1) so the total TimeComplexityofthealgorithmisO(n)time.SpaceComplexityforcreatingcountarrayisalsoO(n)Fifthapproach:Youareallowedtomodifythegiveninputarray.Modifythegiveninputarrayinsuchawaythatinthenextscanyoucanfindthemissingelement.Whenyouscanthroughthearray.Whenatindex“index”,thevaluestoredinthearraywillbearr[index]soadd thenumber“n+1” toarr[arr[ index]].Alwaysread thevaluefromthearrayusingareminderoperator“%”.Whenyouscanthearrayforthefirsttimeandmodifiedallthevalues,thenonesinglescanyou can see if there is some value in the arraywhich is smaller than “n+1” that index is themissingnumber.Inthisapproach,thearrayisscannedtwotimesandtheTimeComplexityofthisalgorithmisO(n).SpaceComplexityisO(1)Sixthapproach:Summationformulatofindthesumofnnumbersfrom1ton.Subtractthevaluesstoredinthearrayandyouwillhaveyourmissingnumber.TheTimeComplexityofthisalgorithmisO(n).SpaceComplexityisO(1)Seventhapproach:XORapproachtofindthesumofnnumbersfrom1ton.XORthevaluesstoredinthearrayandyouwillhaveyourmissingnumber.TheTimeComplexityofthisalgorithmisO(n).SpaceComplexityisO(1).Example5.16intfindMissingNumber(intarr[],intsize,intrange){
inti;intxorSum=0;//gettheXORofallthenumbersfrom1torangefor(i=1;i
xorSum^=arr[i];returnxorSum;
}Note: Same problem can be asked in many forms (sometimes you have to do the xor of the rangesometimeyoudonot):1.Therearenumbers in the rangeof1-noutofwhichall appears single timebutone that appear two
times.2.All theelements in the range1-nareappearing16 timesandoneelementappear17 times.Find the
elementthatappears17times.
FindPairinanArrayGivenanarrayofnnumbers,findtwoelementssuchthattheirsumisequalto“value”Firstapproach:Exhaustive searchorBrute force, for eachelement in array find if there is someotherelement, which sum up to the desired value. This is done using two for loop, first loop to select theelementandsecondlooptofindanotherelement.TheTimeComplexityisO(n2) andSpaceComplexityisO(1)Example5.17intFindPair(int*arr,intsize,intvalue){
inti,j;for(i=0;i<size;i++)for(j=i+1;j<size;j++)if((arr[i]+arr[j])==value){printf("Thepairis%d,%d",arr[i],arr[j]);return1;}return0;
}Secondapproach:Sorting,Stepsareasfollows:1.Sortalltheelementsinthearray.2.Taketwovariablefirstandsecond.Variablefirst=0andsecond=size-13.Computesum=arr[first]+arr[second]4.Ifthesumisequaltothedesiredvalue,thenwehavethesolution5.Ifthesumislessthanthedesiredvalue,thenwewillincreasefirst6.Ifthesumisgreaterthanthedesiredvalue,thenwewilldecreasethesecond7.Werepeattheaboveprocesstillwegetthedesiredpairorwegetfirst>=second(don’thaveapair)SortingalgorithmstakeO(n.logn)timeandsinglescantakeO(n)time.
TheTimeComplexityofanalgorithmisO(n.logn)andSpaceComplexityisO(1)Example5.18intFindPair(int*arr,intsize,intvalue){
intfirst=0,second=size-1;intcurr;Sort(arr,size);
while(first<second){curr=arr[first]+arr[second];if(curr==value){printf("Thepairis%d,%d",arr[first],arr[second]);return1;}elseif(curr<value)first++;elsesecond--;}return0;
}Thirdapproach:Hash-Table,usingHash-Table,wecankeeptrackoftheelementswehavealreadyseenandwecanfindthepairinjustonescan.1.Foreachelement,insertthevalueinHashtable.Letsaycurrentvalueisarr[index]2.Ifthevalue-arr[index]isalreadyinaHashtable.3.Ifvalue-arr[index]isintheHashtablethenwehavethedesiredpair.4.Else,proceedtothenextentryinthearray.Hash-TableinsertandfindtakeconstanttimeO(1)sothetotalTimeComplexityofthealgorithmisO(n)time.SpaceComplexityisalsoO(n)Example5.19intFindPair(int*arr,intsize,intvalue){
HashTableh;inti;for(i=0;i<size;i++){if(findValue(h,value-arr[i])){printf("Thepairis%d,%d",arr[i],value-arr[i]);
return1;}addValue(h,arr[i]);}return0;
}Forthapproach:Counting,Thisapproachisonlypossibleifweknowtherangeoftheinput.Ifweknowthat,theelementsinthearrayareintherange0ton-1.Wecanreserveandarrayoflengthnandwhenweseeanelementwecanincreaseitscount.InplaceoftheHashtableintheaboveapproach,wewillusethisarrayandwillfindoutthepair.Counting approach just uses an array so insert and find take constant time O(1) so the total TimeComplexity