Leetcode with dani
Open in Telegram
Join us and let's tackle leet code questions together: improve your problem-solving skills Preparing for coding interviews learning new algorithms and data structures connect with other coding enthusiasts
Show more1 258
Subscribers
No data24 hours
+17 days
-730 days
Posts Archive
1 258
can u solve this question
βTitle: 201. Bitwise AND of Numbers Range
Difficulty: Medium
Description:
Given two integers
left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.
Example 1:
Input: left = 5, right = 7 Output: 4Explanation:
5 = 101 6 = 110 7 = 111Bitwise AND of all numbers in the range [5, 7] is 4 (binary 100). Example 2:
Input: left = 0, right = 0 Output: 0Example 3:
Input: left = 1, right = 2147483647 Output: 0Explanation: There is no common bit position that stays 1 throughout the range, so the result is 0. Constraints:
0 <= left <= right <= 2^31 - 1
1 258
Repost from Hanix
Summer Camp and Internships
1. INSA Cyber Talent Challenge β 2017 E.C.
Tracks: Cybersecurity | Development | Embedded Systems | Aerospace
Apply: Here
2.iCog Labs AI Internship β 2025 Batch 1
Tracks: AI | ML | Robotics | Bioinformatics | Blockchain
Deadline: July 31
Apply:Here
3. Kuraz Technologies Hybrid Internship
Type: Summer | Unpaid | Hybrid
Deadline: May 31
Apply: Here
4. Tewanay Engineering Summer Internship
Tracks: Front-End | Back-End | Mobile App Development
Deadline: May 31
Apply: Here
@HanixJourney
#Internship
1 258
---
βBitwise Tricks in Python
β1. Check Even/Odd
def is_odd(n: int) -> bool:
return bool(n & 1)
# Usage:
n = 7
print("Odd" if is_odd(n) else "Even") # Output: Odd
Try: βCheck if a Number is Odd or Evenβ on GeeksforGeeks (GfG)
---
β2. Swap Two Numbers Without a Temp Variable
def xor_swap(a: int, b: int) -> tuple[int, int]:
a ^= b
b ^= a
a ^= b
return a, b
# Usage:
x, y = 5, 9
x, y = xor_swap(x, y)
print(x, y) # Output: 9 5
Note: In real Python, you can simply use x, y = y, x.
---
β3. Turn Off the Rightmost Set Bit
def clear_lowest_bit(n: int) -> int:
return n & (n - 1)
# Usage:
n = 0b10110 # 22
print(bin(clear_lowest_bit(n))) # Output: 0b10100 (20)
Try: βCount Set Bits in an Integerβ on GfG (use this in a loop)
---
β4. Isolate the Rightmost Set Bit
def lowest_bit(n: int) -> int:
return n & -n
# Usage:
n = 0b10110 # 22
print(bin(lowest_bit(n))) # Output: 0b10 (2)
Try: βFind Position of Rightmost Different Bitβ on GfG
---
β5. Check Power of Two
def is_power_of_two(n: int) -> bool:
return n > 0 and (n & (n - 1)) == 0
# Usage:
for x in [1, 2, 3, 16, 18]:
print(x, is_power_of_two(x))
Try: βCheck if a Number is a Power of Twoβ on GfG
---
β6. Count Set Bits (Brian Kernighanβs Algorithm)
def count_set_bits(n: int) -> int:
count = 0
while n:
n &= (n - 1)
count += 1
return count
# Usage:
print(count_set_bits(29)) # Output: 4, since 29 is 11101β
Try: βCount Set Bitsβ on GfG (Python)
---
β7. Add Two Numbers Without β+β
def add(a: int, b: int) -> int:
MASK = 0xFFFFFFFF
while b != 0:
carry = (a & b) & MASK
a = (a ^ b) & MASK
b = (carry << 1) & MASK
return a if a <= 0x7FFFFFFF else ~(a ^ MASK)
# Usage:
print(add(15, 27)) # Output: 42
print(add(-5, 3)) # Output: -2
Try: βSum of Two Integersβ on LeetCode (Python)
---
β8. Test Opposite Signs
def opposite_signs(a: int, b: int) -> bool:
return (a ^ b) < 0
# Usage:
print(opposite_signs(5, -3)) # Output: True
print(opposite_signs(-4, -2)) # Output: False
Try: βCheck if Two Numbers Have Opposite Signsβ on GfG
---
β9. Reverse Bits of a 32-bit Integer
def reverse_bits(n: int) -> int:
result = 0
for _ in range(32):
result = (result << 1) | (n & 1)
n >>= 1
return result & 0xFFFFFFFF
# Usage:
x = 0b00000010100101000001111010011100
print(bin(reverse_bits(x)))
Try: βReverse Bitsβ on LeetCode (Python)
---
β10. Gray Code Conversion
def to_gray(n: int) -> int:
return n ^ (n >> 1)
def gray_to_binary(g: int) -> int:
mask = g
while mask:
mask >>= 1
g ^= mask
return g
# Usage:
for i in range(8):
g = to_gray(i)
print(f"{i:3} β Gray {g:3} β Back {gray_to_binary(g):3}")
Try: βGray Codeβ on LeetCode (Python)
---
βπ― Next Steps:
1. Pick one or two of these tricks.
2. Find the corresponding GfG problem in Python and implement it.
3. Time yourself and compare it to a straightforward solution.1 258
βBitwise Tricks and Techniques for Competitive Programming
β1. Check Even/Odd
β’ Trick:
n & 1
β’ Why it works: The least significant bit (LSB) of an integer is 1 if itβs odd, 0 if itβs even.
β’ Usage:
if (n & 1) // n is odd
else // n is even
β’ Try it on: βCount Odd Numbers in an Interval Rangeβ (easy)
---
β2. Swap Two Numbers Without a Temp Variable
β’ Trick: a ^= b; b ^= a; a ^= b;
β’ Why it works: XORing twice cancels out, effectively swapping the values.
β’ Usage:
a ^= b;
b ^= a;
a ^= b;
β’ Try it on: βSwap Two Numbersβ (basic exercise)
---
β3. Turn Off the Rightmost Set Bit
β’ Trick: n & (n - 1)
β’ Why it works: Subtracting 1 flips all bits from the rightmost 1 to the end; ANDing clears that rightmost 1.
β’ Usage:
n = n & (n - 1); // removes lowest set bit
β’ Try it on: βCount Set Bits in an Integerβ (you can loop with this trick)
---
β4. Isolate the Rightmost Set Bit
β’ Trick: n & -n
β’ Why it works: The twoβs complement of n (-n) has only the rightmost set bit in common with n.
β’ Usage:
int lowbit = n & -n;
β’ Try it on: βPower of Twoβ (check n & (n - 1) == 0, then use n & -n to identify which bit it is)
---
β5. Check Power of Two
β’ Trick: (n & (n - 1)) == 0 and n > 0
β’ Why it works: Powers of two have exactly one set bit; subtracting 1 clears that bit.
β’ Usage:
bool isPow2 = (n > 0) && ((n & (n - 1)) == 0);
β’ Try it on: βCheck if a Number is a Power of Twoβ (classic)
---
β6. Count Set Bits Faster (Brian Kernighanβs Algorithm)
β’ Trick: Repeatedly do n &= (n - 1) until zero, counting iterations.
β’ Why it works: Each operation removes one set bit, so the loop runs in O(# bits set).
β’ Usage:
int count = 0;
while (n) {
n &= (n - 1);
++count;
}
β’ Try it on: βCount Set Bits in an Integerβ (better than O(32) per bit)
---
β7. Compute a + b Without Plus
β’ Trick: Use XOR and AND with shifts:
while (b) {
unsigned carry = a & b;
a = a ^ b;
b = carry << 1;
} // a now holds the sum
β’ Why it works: XOR gives sum without carry; AND + shift gives carry bits until none remain.
β’ Try it on: LeetCode βSum of Two Integersβ
---
β8. Test if Two Integers Have Opposite Signs
β’ Trick: (a ^ b) < 0
β’ Why it works: The sign bit differs if XORβs sign bit is 1.
β’ Usage:
bool oppSigns = (a ^ b) < 0;
β’ Try it on: βCheck if Two Integers Have Opposite Signsβ (easy)
---
β9. Reverse Bits
β’ Trick: Use lookup tables for small chunks (e.g., 8 bits) or iterative bitwise swaps:
n = (n >> 16) | (n << 16);
n = ((n & 0xff00ff00) >> 8) | ((n & 0x00ff00ff) << 8);
// β¦and so on
β’ Why it works: Repeatedly swap bit-groups to reverse order in logβ(width) steps.
β’ Try it on: LeetCode βReverse Bitsβ
---
β10. Gray Code Conversion
β’ Trick to get Gray from binary: g = n ^ (n >> 1)
β’ Trick to get Binary from Gray: Loop with shifting and XOR.
β’ Try it on: βGray Codeβ generation problems
---1 258
Repost from AAU Software Engineering community
Summer Opportunity for third year and above students
βπSummer University 2025: Learn, Travel, Change the World!
β‘For whom?
Undergraduate students (3rd or 4th year) from universities outside of Russia.
β‘What?
Free two-week intensive (July 21 β August 3, 2025):
Λ Masterclasses from expert speakers.
Λ Project work in international teams.
Λ Cultural exchange and excursions.
β‘Why you?
Λ Organizers cover flights, accommodation, meals, and all activities.
Λ Level up your skills, add global experience to your resume.
Λ Make friends from around the globe.
β‘How to participate?
1. Register on the website summeruniversity.ru. by May 12, 2025.
2. Submit a motivational video (2-3 minutes): explain why you want this project and what ideas you want to implement.
3. Await selection results β announced by May 30!
π£Don't delay! Places are limited, competition is high. Show you're ready to learn, not just relax β this isn't a vacation!
βProgram details and requirements are available on their website summeruniversity.ru
From SiTE Notice Board
Sharing is caring π₯°
Deadline: May 12, 2025
1 258
Repost from Hanix
iCog Labs AI Internship β 2025 Batch 1
Gain hands-on experience in AI, ML, Robotics, Bioinformatics, Blockchain, and more!
Open to students, graduates & self-taught learners across Africa & Asia.
10 hrs/week in-person for Ethiopians in Ethiopia.
Deadline:July 31
Apply Here.
@HanixJourney
#iCogLabs #AIInternship
1 258
π₯β¨ Ready to transform your financial future?!
β¨π₯Welcome to a world of profit, luck, and real investment powerβ
MEXCOMPANY
A trusted platform with 6 years of success and over 40,000 happy users βStart investing from just $39!β
Why MEX is a game-changer:
β
1%β5% daily profit
β
Compound interest with reinvestment β»οΈ
β
Pay with 33 cryptocurrencies
β
Withdraw in 12 major stablecoins (USDT, USDC, etc.)
β
Withdraw up to $1000 per day
β
Extra income based on your active points
β
Daily lucky spin (from 50 to 100,000 points!)
β
Token airdrop based on secondary point balance
β
3-level referral: 10% - 2% - 1%β
No more tiny returns! Start BIG with just $39!β
Sign up now:
https://6thseason.mexcompany.org/register
Join our Telegram Channel:
https://t.me/mexcompany6th
Start now β Your profitable journey begins today!β‘οΈππ°
1 258
π₯β¨ Ready to transform your financial future?!
β¨π₯Welcome to a world of profit, luck, and real investment powerβ
MEXCOMPANY
A trusted platform with 6 years of success and over 40,000 happy users βStart investing from just $39!β
Why MEX is a game-changer:
β
1%β5% daily profit
β
Compound interest with reinvestment β»οΈ
β
Pay with 33 cryptocurrencies
β
Withdraw in 12 major stablecoins (USDT, USDC, etc.)
β
Withdraw up to $1000 per day
β
Extra income based on your active points
β
Daily lucky spin (from 50 to 100,000 points!)
β
Token airdrop based on secondary point balance
β
3-level referral: 10% - 2% - 1%β
No more tiny returns! Start BIG with just $39!β
Sign up now:
https://6thseason.mexcompany.org/register
Join our Telegram Channel:
https://t.me/mexcompany6th
Start now β Your profitable journey begins today!β‘οΈππ°
Available now! Telegram Research 2025 β the year's key insights 
