0% found this document useful (0 votes)
7 views5 pages

Prep Notes

The document discusses various algorithms for computing the GCD using the subtraction and division methods, as well as techniques for finding factors of a number, identifying a missing number in an array, sorting arrays with duplicates using a three-way quicksort, rotating arrays, counting frequencies in an array, and hashing for efficient data retrieval. Each method includes a brief explanation of the steps involved, conditions for completion, and time complexities. The document emphasizes the efficiency and applicability of these algorithms for different data scenarios.

Uploaded by

aashika222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Prep Notes

The document discusses various algorithms for computing the GCD using the subtraction and division methods, as well as techniques for finding factors of a number, identifying a missing number in an array, sorting arrays with duplicates using a three-way quicksort, rotating arrays, counting frequencies in an array, and hashing for efficient data retrieval. Each method includes a brief explanation of the steps involved, conditions for completion, and time complexities. The document emphasizes the efficiency and applicability of these algorithms for different data scenarios.

Uploaded by

aashika222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

GCD (Euclidean Algorithm)

-Subtraction Method

Repeatedly subtract the min value from the max

Initial: subtract the given two integers

Following steps: subtract the min value and the difference

Condition: Until the subtracted value becomes equal to the min value.

Result: The subtracted / min value is the GCD

Time Complexity: O(log min(a, b))


-Division Method

Divide largest / smallest

Initial: divide the given two integers

Following steps: divide the remainder and the divisor from the previous step

Condition: until the remainder is 0

Result: the divisor when the remainder becomes 0 is the GCD

Factors of a number
Eg: Factors of 12 is 1 2 3 4 6 12

We get 6 by dividing 12 by 2

We get 4 by dividing 12 by 3

Square root of 12 is 3.46 rounding it off to 3

We need to iterate till sqrt root of the number

If i divides the number, then the quotient is also taken into consideration

Finding Missing Number


Array has elements ranging from 1 to n except one integer and we have to find the missing
element

Find the difference between the sum of n natural numbers and the sum of the given array

The difference will give the missing integer


Sorting array with lots of duplicate numbers (doubt)
Three-way Quicksort Technique

We take a pivot element which can be the median element in the array

We divide the array into 3 sections:

Greater than pivot element

Lesser than pivot element

Equal to pivot element

Traverse the array using I (initially i=0):

Low = index 0

High = index n-1

If pivot == array[i]

i++

If pivot > array[i]

Swap array[low] and array[i]

i++

low++

If pivot < array[i]

Swap array[high] and array[i]

high--

Traverse Condition: Until i <= high

Reason it works: since we partition the array into three sections, and i is iterated only
once, the element is sorted into the position at the first trial itself.

This works for large and small array, even with large amount of distinct elements and duplicates values.
Most efficient and easy too
Rotating an array k times
For Python (Clockwise direction):

Use while loop for k times

Insert the last element in 0 index

Pop the last element

For Python (Anti-Clockwise direction):


Use while loop k times

Append the first element to the list

Remove the first element

Counting the frequencies of a range in an array


Problem: Given an array of length n with elements ranging from 1 to n, count and store the frequency of
the element in the index (where index=element)

Eg: Input : [2,3,2,3,5]

Output: [0,2,2,0,1]

Optimum time complexity: O(n)

Space complexity: O(1)

Solution:

We will store the result in the give array itself so we don’t need extra spaces

Problem for this kind of solution : when we try to store the count, we might lose the data in that
index

We store the frequency by using negative numbers. For each count, the value is decreased. At
the end we can find abs of that value
ALG:

We traverse using i

If arr[i]<=0:

#means there’s a frequency count or the value has been counted, we need to
skip over that one

i++

else:

#we calculate the frequency of that number

e=arr[i]-1 #the position where the frequency needs to be stored

if arr[e]<0

#meaning there’s already a count, we just need to decrease the value

arr[e]-=1

#now the element in the position i can be removed

arr[i]=0

i++

else:

#there’s a data present in this position, so we swap their places

(Swapping is not necessary. We just need to store the element present

arr[e] in arr[i]

arr[e],arr[i]=arr[e],arr[i]

#There’s a frequency count of 1 so we set the value as below

arr[e]=-1

#we don’t increament i because there’s a new value at index i and that
needs to be calculated

 Run a for loop to make the values abs of that value and return the same list
Hashing
A technique used to store and retrieve data in constant time

Worst case time complexity:


 Linear Search: O(n)
 Binary Search: O(log n)
 Hashing: O(1) (Always)

You might also like