0% found this document useful (0 votes)
4 views

Copy of Algorithms

Uploaded by

rehanaparbin1002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Copy of Algorithms

Uploaded by

rehanaparbin1002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Yaad Rakhne Wali Cheeze !!!

Average Number of comparisons k liye (a+b)/2 Nahiiii karna hai.

Probability of second element being x = (Probability pehla nhi hai)*(dusra hai)


Answer O(n2) jesa lagta hai par theta pucha hai.

For comparison based sorting we already have known algorithms like heap sort and
merge sort, which solves the problem in
O(nlogn) (See O) and now if we show that this is the best possible by any algorithm our
answer becomes Θ(nlogn).
Kisi research paper me likha hai : Minimum number of comparisons =⌈log(n!)⌉

Answer should be : C
Dono hi quick sort k worst cases honge.

Dekh kar lagta hai option B hoga kyuki most of the times randomised quick sort
nlogn hi deta hai par worst case me n2 ja sakta hai. So the correct ans is C.

Is ques ko optimal merge pattern (greedy) jese karna hai.


To merge to array each of size “m” and “n” , no of comparisons required = m+n-1
Dhyaan se padho “central” element likha hai na ki “median”
Agar median likha hota to and C hota par central likha hai isiliye ans should be A.

Solution dekh lo iska :


The worst case time complexity when the range of elements is small when compared
to the number of elements is O(n) for radix sort.
Radix sort TC = O(nw) where n is the number of elements in array and w is the
number of digits in each array element.

Why does Radix sort require stable digit sorts?


Integers that are equal in some digit aren't always equal (e.g., 12 and 13).
Let's sort [13, 12] using a LSD-first radix sort (base 10). But we'll use an unstable sort for
each digit.
First we sort by the 1's place, so the array becomes [12, 13].
Now we sort by the 10's places, but 12 and 13 have the same digit. Since the sort is
unstable, the resulting array could be [12, 13] or [13, 12]. We don't know.
If the sort were stable we would be guaranteed to get a correctly sorted array.
Practical use of counting sort :

Pehle laga ki DFS ka O(n) hoga and Kth smallest ka O(n2) hoga par ye sahi hai :
I thought it would be n-1 but :

Answer is (D) None of these.

We just require n/2 swaps in the worst case. The algorithm is as given below:
Find positive number from left side and negative number from right side and do
exchange. Since, at least one of them must be less than or equal to n/2, there cannot be
more than n/2 exchanges.

Sawal padhte time end me bracket wala part miss kr diya aur dimag bhi nhi chala :

https://gateoverflow.in/258/Gate-cse-2003-question-66

Question ko galat meaning me le gye :


Yaha pucha hai “to determine if an integer appears more than n/2 times”
To determine karne k liye pehle check krna pdega binary search se O(logn)
If agar ques me diya hota ki “there is an integer which appears more than n/2 time and
you have to find that element” to iska ans O(1) hota direct middle element return kr dete.

f1 () ki time complexity galat nikal li:

F1() ka recursive tree ekdm fibonacci type banega to ans : O(2^n)


Log2n ka matlab hota hai (log log n) :

Answer B diya hai par galat lag raha hai : |

Just solve it like T(n)=T(n/3)+n


F(n)= n and n^(logba)= n^0 to ans will be O(n)
Fibonacci samjh k 2^n laga diya :)

If we draw the recursion tree we have 3 children for each n>3, as the height of the
tree is n-1 so the number of subproblems is limited by 3^n. Each computation will
require constant time and hence the time complexity is O(3^n).

Masters theorem se answer 3^k aaya par actual ans B hai.


https://gateoverflow.in/807/Gate-cse-2002-question-1-3
Series hi solve nhi ho rhi:

Last step me dimag nhi chala :

(loglogn)
Ans aaya tha 2
Further solve krte ye property use krke: a^logb=b^loga
lognlog2 = logn which is option B
Ese ques me example leke solve karo :

https://gateoverflow.in/497/Gate-cse-2008-question-78

Remember this :

Masters theorem Case I :

Ans is Option A
Trick:

Size of the problem will reduce as we go down a level each time in the tree.

Hence, in this case root will be the dominant part.

Final ans is T(n)=Θ(n)(order of the root)

(you have to ans in less than 30 sec!)


If we use Master theorem we get option B. But one must know that Master theorem
is used to find the asymptotic bound and not an EXACT value. And in the question
here it explicitly says "evaluates to".
Thande Dimag se socho !

https://gateoverflow.in/1826/Gate-cse-2006-question-49
Thande Dimag se socho !

https://gateoverflow.in/61/Gate-cse-2013-question-44

Thande Dimag se socho !

https://gateoverflow.in/39667/Gate-cse-2016-set-1-question-10
Thande Dimag se socho !

https://gateoverflow.in/39684/Gate-cse-2016-set-1-question-41

Give up in this :

if we assume left associativity for + (default), we get ABCD+∗F/+DE∗+ but this is not
among the options.So, considering right associativity for + we get ABCD+∗F/DE∗++

Correct Answer: B
Thande Dimag se socho !

https://gateoverflow.in/951/Gate-cse-2003-question-64

Confusing ques !

https://gateoverflow.in/3758/Gate-it-2005-question-13
1. A tree edge (T) is an edge in a DFS-tree.
2. A back edge (B) connects a vertex to an ancestor in a DFS-tree. (Includes a
self-loop and this indicates the presence of a cycle)
3. A forward edge (F) is a non-tree edge that connects a vertex to a descendent in a
DFS-tree.
4. A cross edge (C) is any other edge in graph
5. G.
6. G. It either connects vertices in two different DFS tree or two vertices in the same
DFS tree neither of which is the ancestor of the other.

https://gateoverflow.in/88152/Gate-cse-1989-question-4-vii
https://gateoverflow.in/3813/Gate-it-2005-question-52

Good question !
Mujhe laga range 0 to 100 hai to array of 100 integers would be the ans but but but :

As we our area of interest is only the 50 numbers so take An array of 50 numbers where
A[0] corresponds to 51...A[49] corresponds to 100 then after reading an input just
increment the counter in correct position as said above.
Correct Answer: A

Zyada extra dimag mat chalaya karo : |

https://gateoverflow.in/2076/Gate-cse-2014-set-3-question-42
Remember how to solve this :

Minus 1 krna bhool gye…

Mene ans likha 512…actual ans is for n elements, no of comparisons = n-1


= 512-1 =511

Independent set is a set of vertices such that any two vertices in the set do not have
a direct edge between them.
Maximal independent set is an independent set having highest number of vertices.
Tricky :
Answer is (A).
Here, lower bound imply best algorithm which works for all cases and hence we should
consider worst-case.
Max-Heapify(root)

Correct Option: A

The inorder traversal order of a ternary tree is left → root → middle → right.
Aliasing in the context of programming languages refers to multiple variables having the
same memory location.

https://gateoverflow.in/2124/Gate-cse-2011-question-22
Tricky one

https://gateoverflow.in/3701/Gate-it-2004-question-58
Mene B option laga diya tha :

But a+2 is c.

https://gateoverflow.in/204076/Gate-cse-2018-question-2

Good question : https://gateoverflow.in/3592/Gate-it-2006-question-49


Mene normal array wale formula jese solve kr diya :)

Ek element k liye O(n) hoga to n elements k liye n*O(n) = O(n2)


Confusing af :

https://gateoverflow.in/735/Gate-cse-2001-question-2-17-ugcnet-aug2016-iii-21

Here c is passed by value, mene c ko pass by


reference krke ans nikal liya.

https://gateoverflow.in/483/Gate-cse-2008-question-60
You don’t even know ki double hashing kese apply krte hai :|

Double hashing:

Thande dimag se socho !!

https://gateoverflow.in/2272/Gate-cse-1997-question-12
Are bhaiya dekho pehle 3 slots khali rakhne hai to pehle insertion me ye 3 chod k baki
slot fill ho jaay iski probability = 97/100

Similarly second and third insertion me bhi 97/100.

To ans will be A par mene B laga diya tha.

https://gateoverflow.in/2074/Gate-cse-2014-set-3-question-40

Solution ka second case thoda samajh nhi aaya !!


Thande Dimag se socho !
Ghanta kuch samajh nhi aaya !

https://gateoverflow.in/82129/Gate-cse-2005-question-82b#google_vignette

REMEMBER THIS :

Answer is A: Queue
We can find single source shortest path in unweighted graph by using Breadth First
Search (BFS) algorithm by using "Queue" data structure , in time O(m+n) (i.e. linear with
respect to the number of vertices and edges. )

NAI SAMAJH AAYA : |

https://gateoverflow.in/1824/Gate-cse-2006-question-48

Good question !!

https://gateoverflow.in/39620/Gate-cse-2016-set-2-question-41
Nai samajh aaya !!

https://gateoverflow.in/333191/Gate-cse-2020-question-40

Good question !
https://gateoverflow.in/357494/Gate-cse-2021-set-2-question-46

Do try this once again :

https://gateoverflow.in/3856/Gate-it-2005-question-84a
https://gateoverflow.in/3457/Gate-it-2007-question-24
Good question :
Thoda Dimag lagane wala ques:
Question acche se padha karo didi !!

https://gateoverflow.in/204095/Gate-cse-2018-question-21

Sab kuch sahi kiya par printf me counter ki value print honi thi aur me c*c*c nikal
rhi thi : (
Remember this prime no wala concept:

Silly mistake Ig:


Nice logic remember this :

Mera dimag nahi chala :


Probe wale collisions bhi ginne hote hai :

Kya yaar ye to bnna chahiye tha


Tricky one !!

If else ki matching sahi se nhi dekhi

<> is nothing but not equal to in Pascal, means if we write x<>y it is true if x is not equal to y. you can
equate with x!=y in C.
https://gateoverflow.in/2615/Gate-cse-1995-question-2-3

Exceptional Example sochna pdega !

https://gateoverflow.in/1501/Gate-cse-1999-question-2-24
Confused while solving this… lots of calculations

https://gateoverflow.in/8060/Gate-cse-2015-set-2-question-11

If k andar ki condition par dhyaan nhi diya to question gaya !

https://gateoverflow.in/302822/Gate-cse-2019-question-26
Ye to mene nahi padha :

https://gateoverflow.in/43484/Gate-cse-2008-question-81

Dobara padho https://gateoverflow.in/43476/Gate-cse-2009-question-54

Related to definition of DP :https://gateoverflow.in/2127/Gate-cse-2011-question-25

The algorithm is storing the optimal solutions to subproblems at each point (for each i),
and then using it to derive the optimal solution of a bigger problem. And that is a
dynamic programming approach.

You might also like