Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
136 views

Algo++ Classroom: Prateek Narang

The document summarizes a coding lecture that covered pointers, bitwise operators, and example problems. It discusses topics like address of operator, left and right bit shifting, setting and getting bits, counting set bits, and converting between binary and decimal. Example problems included finding unique numbers in an array, setting bits in a number, finding the missing number, and calculating the XOR sum of all subarrays. The lecture emphasized practicing example problems and assignments to improve coding skills.

Uploaded by

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

Algo++ Classroom: Prateek Narang

The document summarizes a coding lecture that covered pointers, bitwise operators, and example problems. It discusses topics like address of operator, left and right bit shifting, setting and getting bits, counting set bits, and converting between binary and decimal. Example problems included finding unique numbers in an array, setting bits in a number, finding the missing number, and calculating the XOR sum of all subarrays. The lecture emphasized practicing example problems and assignments to improve coding skills.

Uploaded by

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

Algo++ Classroom

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

>> Right Shift


Bitwise Operators
Left Shift & Right Shift!

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

- Get Last Bit Lorem ipsum dolor sit amet, consectetur


- Get ith Bit adipiscing elit, sed do eiusmod tempor
- Set ith Bit incididunt ut labore et dolore magna aliqua
- Count Set Bits

Time Saving Hack - Use __builtin_popcount()


method to count bits.
Convert Decimal Into Binary

Print Binary Representation of given number

Convert a Binary Number into Decimal


Time to Try - Incredible Hulk!
Problem - Unique Number-1
Problem - Unique Number-2

You are given a list of numbers where every number is


coming twice except two numbers.

Arr[] = {2,3,4,6,4,3,2,7,9,9};

2N + 2

Find out the unique two numbers.


Problem - Unique Number-3
Number Conversion

Write a function to determine the number of bits required to convert integer A to integer B.

Input: 31, 14

Output: 2
Explain this!

Explain what the following code does:

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/

Brute Force Complexity - ?

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)

You might also like