Algo++ Classroom: Prateek Narang
Algo++ Classroom: Prateek Narang
Prateek Narang
Lecture 01 - Operators
Pointers, Bitwise Operators
Warm Up - Birthday Paradox!
Find number of people in the hall
such that the probability atleast 2
have their birthday on same date
is atleast ‘p’!
For eg - p = 0.5
Brush-up
Address Of Operator
Pointer Variable
Reference Variable
Bitwise Operators
& AND
| OR
^ XOR
~ NOT
Bitwise Operators
& AND
| OR
^ XOR
~ NOT
Left Shift is used to Multiply by any power of two and Right bit shifting to divide by any power of two.
Left Shift
a<<b = a * power(2,b)
Right Shift
a >> b = a/power(2^b)
Example Multiplication by 9?
Example Multiply x by 9 ?
x<<3 + x
Some Basic Operations
Arr[] = {2,3,4,6,4,3,2,7,9,9};
2N + 2
Write a function to determine the number of bits required to convert integer A to integer B.
Input: 31, 14
Output: 2
Explain this!
while(n>0){
n = n & (n-1)
}
Problem - Not So Easy Math!
--
Replace bits by M
You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all
bits between i and j in N equal to M (e.g., M becomes a substring of N located at i and starting at j).
EXAMPLE:
Input: N = 10000000000,
M = 10101, i = 2, j = 6
Output: N = 10001010100
Finding Missing Number
An array A[1...n] contains all the integers from 0 to n except for one number which is missing. In
this problem, we cannot access an entire integer in A with a single operation. The elements of A
are represented in binary, and the only operation we can use to access them is “fetch the jth bit
of A[i]”, which takes constant time. Write code to find the missing integer. Can you do it in O(n)
time?
Homework
Largest & Smallest Number
Given an integer, print the next smallest and next largest number that have the same number of 1
bits in their binary representation.
Question - Sum of XOR of all Subarrays
Given an array containing N positive integers, the task is to find the sum of XOR of all sub-arrays of
the array.
Hint - Sum of XOR of all Subarrays
Given an array containing N positive integers, the task is to find the sum of XOR of all sub-arrays of
the array.
https://www.geeksforgeeks.org/sum-of-xor-of-all-subarrays/
Prefix Array - ?
”
- From an expert
- Be Regular
- Solve Assignments
- Work Hard
See you in next
class!
Prateek Narang (Mentor)
prateek@codingblocks.com
TA’s
- Khushboo Verma
- Deepak
- Sanjeet (Co-mentor)