Full download Data structures and algorithms made easy in Java data structure and algorithmic puzzles 2nd Edition Narasimha Karumanchi pdf docx
Full download Data structures and algorithms made easy in Java data structure and algorithmic puzzles 2nd Edition Narasimha Karumanchi pdf docx
https://ebookgate.com
https://ebookgate.com/product/data-structures-and-
algorithms-made-easy-in-java-data-structure-and-
algorithmic-puzzles-2nd-edition-narasimha-
karumanchi/
https://ebookgate.com/product/data-structures-algorithms-and-
applications-in-c-2nd-edition-sartaj-sahni/
ebookgate.com
https://ebookgate.com/product/java-structures-data-structures-in-java-
for-the-principled-programmer-2nd-edition-duane-bailey/
ebookgate.com
https://ebookgate.com/product/data-structures-and-abstractions-with-
java-2nd-ed-edition-carrano/
ebookgate.com
https://ebookgate.com/product/data-structures-and-algorithms-in-c-4th-
edition-adam-drozdek/
ebookgate.com
Data Structures and Algorithms in C 1st Edition Michael
Mcmillan
https://ebookgate.com/product/data-structures-and-algorithms-in-c-1st-
edition-michael-mcmillan/
ebookgate.com
https://ebookgate.com/product/data-structures-and-algorithms-
using-c-1st-edition-michael-mcmillan/
ebookgate.com
https://ebookgate.com/product/java-software-structures-designing-and-
using-data-structures-3rd-edition-john-lewis/
ebookgate.com
https://ebookgate.com/product/java-foundations-introduction-to-
program-design-and-data-structures-2nd-edition-john-lewis/
ebookgate.com
https://ebookgate.com/product/data-structures-outside-in-with-
java-1st-edition-sesh-venugopal/
ebookgate.com
Data Structures
And
Algorithms
Made Easy In JAVA
Data Structures and Algorithmic Puzzles
By
Narasimha Karumanchi
©
Copyright 2020 CareerMonk Publications. All rights reserved.
All rights reserved. No part of this book may be reproduced in any form or by any electronic or mechanical means, including information
storage and retrieval systems, without written permission from the publisher or author.
This book has been published with all efforts taken to make the material error-free after the consent of the author. However, the author and
the publisher do not assume and hereby disclaim any liability to any party for any loss, damage, or disruption caused by errors or omissions,
whether such errors or omissions result from negligence, accident, or any other cause.
While every effort has been made to avoid any mistake or omission, this publication is being sold on the condition and understanding that
neither the author nor the publishers or printers would be liable in any manner to any person by reason of any mistake or omission in this
publication or for any action taken or omitted to be taken or advice rendered or accepted on the basis of this work. For any defect in printing
or binding the publishers will be liable only to replace the defective copy by another copy of this work then available.
Acknowledgements
ℎ and ℎ , it is impossible to thank you adequately for everything you have done, from loving me unconditionally to raising me in
a stable household, where your persistent efforts and traditional values taught your children to celebrate and embrace life. I could not have
asked for better parents or role-models. You showed me that anything is possible with faith, hard work and determination.
This book would not have been possible without the help of many people. I would like to express my gratitude to all of the people who
provided support, talked things over, read, wrote, offered comments, allowed me to quote their remarks and assisted in the editing,
proofreading and design. In particular, I would like to thank the following individuals:
- ℎ ℎ
M-Tech,
Founder, .
Preface
Dear Reader,
Please hold on! I know many people typically do not read the Preface of a book. But I strongly recommend that you read this particular
Preface.
It is not the main objective of this book to present you with the theorems and proofs on and ℎ . I have followed
a pattern of improving the problem solutions with different complexities (for each problem, you will find multiple solutions with different,
and reduced, complexities). Basically, it’s an enumeration of possible solutions. With this approach, even if you get a new question, it will
show you a way to think about the possible solutions. You will find this book useful for interview preparation, competitive exams preparation,
and campus interview preparations.
As a , if you read the complete book, I am sure you will be able to challenge the interviewers. If you read it as an , it
will help you to deliver lectures with an approach that is easy to follow, and as a result your students will appreciate the fact that they have
opted for Computer Science / Information Technology as their degree.
This book is also useful for and during their academic preparations. In all the
chapters you will see that there is more emphasis on problems and their analysis rather than on theory. In each chapter, you will first read
about the basic required theory, which is then followed by a section on problem sets. In total, there are approximately 700 algorithmic
problems, all with solutions.
If you read the book as a preparing for competitive exams for Computer Science / Information Technology, the content covers
the topics in full detail. While writing this book, my main focus was to help students who are preparing for these exams.
In all the chapters you will see more emphasis on problems and analysis rather than on theory. In each chapter, you will first see the basic
required theory followed by various problems.
For many problems, solutions are provided with different levels of complexity. We start with the solution and slowly
move toward the possible for that problem. For each problem, we endeavor to understand how much time the algorithm takes
and how much memory the algorithm uses.
It is recommended that the reader does at least one complete reading of this book to gain a full understanding of all the topics that are
covered. Then, in subsequent readings you can skip directly to any chapter to refer to a specific topic. Even though many readings have been
done for the purpose of correcting errors, there could still be some minor typos in the book. If any are found, they will be updated
at . . . You can monitor this site for any corrections and also for new problems and solutions. Also, please provide
your valuable suggestions at: @ . .
I wish you all the best and I am confident that you will find this book useful.
- ℎ ℎ
M-Tech,
Founder, .
Other Books by Narasimha Karumanchi
IT Interview Questions
Elements of Computer Networking
Data Structures and Algorithmic Thinking with Python
Data Structures and Algorithms Made Easy (C/C++)
Coding Interview Questions
Data Structures and Algorithms for GATE
Peeling Design Patterns
Algorithm Design Techniques
Data Structure Operations Cheat Sheet
Space
Average Case Time Complexity Worst Case Time Complexity
Data Structure Complexity
Name Accessing Accessing
Search Insertion Deletion Search Insertion Deletion Worst Case
element element
Arrays O(1) O( ) O( ) O( ) O(1) O( ) O( ) O( ) O( )
Stacks O( ) O( ) O(1) O(1) O( ) O( ) O(1) O(1) O( )
Queues O( ) O( ) O(1) O(1) O( ) O( ) O(1) O(1) O( )
Binary Trees O( ) O( ) O( ) O( ) O( ) O( ) O( ) O( ) O( )
Binary Search
O( ) O( ) O( ) O( ) O( ) O( ) O( ) O( ) O( )
Trees
Balanced
Binary Search O( ) O( ) O( ) O( ) O( ) O( ) O( ) O( ) O( )
Trees
Hash Tables N/A O(1) O(1) O(1) N/A O( ) O( ) O( ) O( )
Note: For best case operations, the time complexities are O(1).
Chapter
INTRODUCTION 1
The objective of this chapter is to explain the importance of the analysis of algorithms, their notations, relationships and solving as many
problems as possible. Let us first focus on understanding the basic elements of algorithms, the importance of algorithm analysis, and then
slowly move toward the other topics as mentioned above. After completing this chapter, you should be able to find the complexity of any
given algorithm (especially recursive functions).
1.1 Variables
Before getting in to the definition of variables, let us relate them to an old mathematical equation. Many of us would have solved many
mathematical equations since childhood. As an example, consider the equation below:
+2 −2 =1
We don’t have to worry about the use of this equation. The important thing that we need to understand is that the equation has names ( and
), which hold values (data). That means the ( and ) are placeholders for representing data. Similarly, in computer science
programming we need something for holding data, and is the way to do that.
1.1 Variables 15
Data Structures and Algorithms Made Easy in Java Introduction
…
private char data;
//Operations
}
The diagram below shows the relationship between different rates of growth.
4 D
e
c
2 r
e
a
s
i
log n
g
log ( !)
R
a
t
e
s
2
O
f
G
r
o
w
t
h
log log
( )
( )
Rate of growth
Input size,
Let us see the O−notation with a little more detail. O−notation defined as O( ( )) = { ( ): there exist positive constants and such
that 0 ≤ ( ) ≤ ( ) for all ≥ }. ( ) is an asymptotic tight upper bound for ( ). Our objective is to give the smallest rate of
growth ( ) which is greater than or equal to the given algorithms’ rate of growth ( ).
Generally, we discard lower values of . That means the rate of growth at lower values of is not important. In the figure, is the point
from which we need to consider the rate of growth for a given algorithm. Below , the rate of growth could be different. is called threshold
for the given function.
Big-O Visualization
O( ( )) is the set of functions with smaller or the same order of growth as ( ). For example; O( ) includes O(1), O( ), O( ), etc.
Note: Analyze the algorithms at larger values of only. What this means is, below we do not care about the rate of growth.
Big-O Examples
Example-1 Find upper bound for ( ) = 3 + 8
Solution: 3 + 8 ≤ 4 , for all ≥ 8
∴ 3 + 8 = O( ) with c = 4 and =8
Example-2 Find upper bound for ( ) = + 1
Solution: + 1 ≤ 2 , for all ≥ 1
∴ + 1 = O( ) with = 2 and = 1
Example-3 Find upper bound for ( ) = + 100 + 50
Solution: + 100 + 50 ≤ 2 , for all ≥ 11
∴ + 100 + 50 = O( ) with = 2 and = 11
Example-4 Find upper bound for ( ) = 2 − 2
Solution: 2 − 2 ≤ 2 , for all ≥ 1
∴ 2 − 2 = O( ) with = 2 and =1
Example-5 Find upper bound for ( ) =
Solution: ≤ , for all ≥ 1
∴ = O( ) with = 1 and =1
Example-6 Find upper bound for ( ) = 410
Solution: 410 ≤ 410, for all ≥ 1
∴ 410 = O(1 ) with = 1 and =1
No Uniqueness?
There is no unique set of values for and in proving the asymptotic bounds. Let us consider, 100 + 5 = O( ). For this function there
are multiple and values possible.
Solution1: 100 + 5 ≤ 100 + = 101 ≤ 101 , for all ≥ 5, = 5 and = 101 is a solution.
Solution2: 100 + 5 ≤ 100 + 5 = 105 ≤ 105 , for all ≥ 1, = 1 and = 105 is also a solution.
( )
( ))
Rate of growth
Input size,
The Ω notation can be defined as Ω( ( )) = { ( ): there exist positive constants c and such that 0 ≤ ( ) ≤ ( ) for all ≥
}. ( ) is an asymptotic tight lower bound for ( ). Our objective is to give the largest rate of growth ( ) which is less than or equal to
the given algorithm’s rate of growth ( ).
Ω Examples
Example-1 Find lower bound for ( ) = 5 .
Solution: , Such that: 0 5 5 = 5 and =1
∴5 = ( ) with = 5 and =1
Example-2 Prove ( ) = 100 + 5 ≠ ( ).
Solution: c, Such that: 0 100 + 5
100 + 5 100 + 5 ( 1) = 105
105 ( – 105) 0
Since is positive – 105 0 105/
Contradiction: cannot be smaller than a constant
Example-3 2 = ( ), = ( ), = ( ).
Rate of growth c ( )
Input size,
Twelve separate cities feed the Harvard purse from their traction
lines, and more than half a hundred pay tribute from their lighting,
heating, gas and power plants. Harvard has two million dollars in the
traction game. The two-cent transfer charge on New York City
trolleys goes to pay the interest on three-quarters of a million
dollars’ worth of traction bonds in Harvard ownership, and Boston
ten-cent fare goes partially to Harvard’s third of a million in Boston
traction bonds.
B. These bonds have just been paid off, but the ability to pay them
off was of course assured by American intervention.