Algorithms: Solve 'Lonley Integer' Using Bit Manipulation
Skills:
Algorithm Basics80%
Key Takeaways
The video demonstrates how to solve the 'Lonely Integer' problem using bit manipulation, specifically by utilizing the XOR operation to find the integer that appears only once in an array.
Full Transcript
hi I'm Gail lock McDow author of crack and coding interview today I'm going to cover the lonely integer problem so suppose we have an array of integers and all the numbers appear twice except for one number that only appears once so how do we find that number the lonely integer well one way is using a hashmap so we can store all of the elements in a hash map and count how many times each character appears or each integer appears if something only appears once then that is of course the lonely integer but how can we reduce the space complexity even further we can't reduce the time complexity because well we have to touch every element anyway so how can we reduce the space complexity well let's think about these numbers in binary the reason why binary is a good bet here is that if we want to reduce a space complexity we're talk probably talking about constant space and that means we can't keep really any data structures so we don't have that much else to work with other than some sort of crazy bit issue so let's think about the bits here consider just that last bit there if every number appeared twice and literally every single number then we'd see an even number of ones if we don't see an even number of ones then so we' see an odd number of ones then that means that there's a one in that bit for the lonely integer so we could go through and actually count the number of ones another thing we could do is just exort everything because there's an even number of ones we'll wind up with a zero which means that the lonely integer has a zero but if there's an odd number of ones we'll wind up with a one so xoring all of those in this case will give us a one here now what about this next bit here well if we exort all of these again we're going to get a one and if we exort all of these here we're going to get a zero because we have an even number of ones and one xor one is zero and if we exor all of these again we have even number of ones we have two ones so we're going to get a zero here so what we can do actually rather than walking through this bit by bit by bit we can walk through our whole list exort all the values and interestingly that resulting value will be the lonely integer so implenting this is now almost trivial we just have some result which starts off at zero walk through each value in the my array and exort everything together and then just return my result so bit manipulation problems are often are one of those problems that do kind of involve AA moment sometimes so one tip off is if you're asked to keep the time complexity really really fast but still reduce the space complexity even more especially for like constant time that's sometimes where you're going to want some s bit manipulation approach the other tip is when you are tackling a bit manipulation problem try looking at it bit by bit rather than with all the integers at once and explore what the result of different operations like anding and xoring and shifting would give you so keep those things in mind and you know practice bit manipulation problems they aren't necessarily that hard once you get a hang of them once you get over the like intimidation Factor good luck
Original Description
Learn how to solve 'Lonely Integer' using bit manipulation. This video is a part of HackerRank's Cracking The Coding Interview Tutorial with Gayle Laakmann McDowell.
http://www.hackerrank.com/domains/tutorials/cracking-the-coding-interview?utm_source=video&utm_medium=youtube&utm_campaign=ctci
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from HackerRank · HackerRank · 16 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
▶
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
HackerRank for Professors
HackerRank
Why CareerCup's Gayle Laakmann McDowell Loves HackerRank [Testimonial]
HackerRank
How to Leverage Data-Driven Insights to Improve Recruiting Results
HackerRank
Hire Top Linux Talent and System Admins with SudoRank [Webinar]
HackerRank
How Zenefits Hired 10 Engineers in 10 Days with HackerRank's CodeSprint
HackerRank
Hire the hottest SQL talent with DbRank from HackerRank [Tutorial]
HackerRank
How to Use HackerRank's CodePair Tool [Tutorial]
HackerRank
Find Linux Talent with SudoRank challenges [Tutorial]
HackerRank
VMware's Mat Connot details his favorite new way to engage top programmers
HackerRank
An Inside Look at Coinbase's Recruiting Strategy [webinar]
HackerRank
How Evernote Enhanced Their Tech Recruiting Process
HackerRank
5 strategies for being a tech recruiting champion with HackerRank [Webinar]
HackerRank
HackerRank Story - Lets make the world flat!
HackerRank
Data Structures: Hash Tables
HackerRank
Data Structures: Anagram Problem Solution
HackerRank
Algorithms: Solve 'Lonley Integer' Using Bit Manipulation
HackerRank
Big O Notation
HackerRank
Algorithms: Graph Search, DFS and BFS
HackerRank
Algorithms: Bit Manipulation
HackerRank
Data Structures: Solve 'Find the Running Median' Using Heaps
HackerRank
Data Structures: Heaps
HackerRank
Algorithms: Memoization and Dynamic Programming
HackerRank
Algorithms: Sort An Array with Comparator
HackerRank
Algorithms: Solve 'Coin Change' Using Memoization and DP
HackerRank
Data Structures: Solve 'Contacts' Using Tries
HackerRank
Data Structures: Tries
HackerRank
Algorithms: Bubble Sort
HackerRank
Algorithms: Merge Sort
HackerRank
Algorithms: Quicksort
HackerRank
Data Structures: Binary Search Tree
HackerRank
Data Structures: Trees
HackerRank
Algorithms: Solve 'Shortest Reach' Using BFS
HackerRank
Algorithms: Solve 'Connected Cells' Using DFS
HackerRank
Algorithms: Solve 'Recursive Staircase' Using Recursion
HackerRank
Algorithms: Binary Search
HackerRank
Algorithms: Solve 'Ice Cream Parlor' Using Binary Search
HackerRank
Data Structures: Linked Lists
HackerRank
Data Structures: Cycles in a Linked List
HackerRank
Data Structures: Stacks and Queues
HackerRank
Data Structures: Queue With Two Stacks
HackerRank
Data Structures: Balanced Parentheses in Expression
HackerRank
Algorithms: Recursion
HackerRank
How to Hire for Your Startup [Collision Panel Video]
HackerRank
HackerRank main() San Francisco | November 8, 2018
HackerRank
The Enterprise Playbook for Technical Hiring
HackerRank
Remote Hiring Colloquy with EY's Dipika Kapadia & BigBasket's Rupesh Kumar
HackerRank
Remote Hiring Colloquy with Hackerrank's Hari K, Akamai's Neha Akhouri & Infosys's Rafee Tarafdar
HackerRank
Building a Culture for Developer Innovation
HackerRank
Developer Trends in 2020 and Beyond
HackerRank
HackerRank's Livestream: Accelerating Developer Innovation at PhonePe
HackerRank
Aadil Bandukwala converses with Rahul Chari about HackerRank's upcoming livestream
HackerRank
Accelerating Developer Innovation at PhonePe with Rahul Chari
HackerRank
The Future of Work for Developers
HackerRank
Scaling AI from Proof of Concept to Production
HackerRank
The Secret Sauce to getting hired in Myntra’s Tech Team
HackerRank
Reimagining Money with Paypal
HackerRank
Engineering Predictions for 2021 and Beyond! 🎬
HackerRank
Future-forward Tech & Careers with BNY Mellon
HackerRank
How Atlassian Approaches Diversity and Inclusion with Balance & Belonging
HackerRank
Connecting Global Tech Ecosystems: Andela Shines a Light on Africa’s Developer Talent
HackerRank
More on: Algorithm Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
🎓
Tutor Explanation
DeepCamp AI