Prep Notes
Prep Notes
-Subtraction Method
Condition: Until the subtracted value becomes equal to the min value.
Following steps: divide the remainder and the divisor from the previous step
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
If i divides the number, then the quotient is also taken into consideration
Find the difference between the sum of n natural numbers and the sum of the given array
We take a pivot element which can be the median element in the array
Low = index 0
If pivot == array[i]
i++
i++
low++
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):
Output: [0,2,2,0,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:
if arr[e]<0
arr[e]-=1
arr[i]=0
i++
else:
arr[e] in arr[i]
arr[e],arr[i]=arr[e],arr[i]
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