Breaking

Monday, March 6, 2023

Geek hates too many 1s || GFG problem of day || 6 March 2023



Problem Link :- GFG POTD

Problem statement

Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.

Significance of problem

Picture this: you're in the programming world, hanging out with a computer that's a champ at dealing with numbers, especially in a language called C++. Now, the challenge is to turn a regular number into a special kind of number called binary, which is just a bunch of 0s and 1s. The goal? Make this binary number as big as you can, but here's the twist – no three '1's can be in a row.

Now, why does this matter? It might sound like a game, but it's actually a sneak peek into how computers manage memory and work efficiently. Computers store and process info using binary, and being savvy with manipulating these 0s and 1s is key for writing speedy and clever computer programs.

By cracking this problem, you're delving into the basics of how computers handle info. You're getting the hang of "bitwise manipulation," a cool trick where you play with individual 0s and 1s to get what you want. It's like decoding the secret language that computers use for everything.

Think of this exercise as a little adventure. It's like a journey that helps you sharpen your problem-solving skills and get cozy with the tools programmers use every day. So, it's not just a puzzle; it's like peeking behind the curtain of computers, turning you into a bit of a digital wizard who can make machines pull off some seriously cool tricks!

What You will Learn

Solving this problem means figuring out how to change a non-negative number to its binary form, and then tweaking it to get the highest possible value without having three consecutive '1' bits in a row. The code provided is a starting point written in C++, which uses a technique called bitwise manipulation.

In simpler terms, the code looks at the binary representation of the input number and checks if there are three '1' bits in a row. If it finds them, it changes the third '1' to '0'. After making these adjustments, the code converts the modified binary back to a regular number, giving the maximum value possible after following these rules.

Learning from this problem involves getting comfortable with the idea of working with binary numbers (the 1s and 0s). You'll also learn how to use specific functions in C++ that help with these operations. This problem helps you practice thinking step by step and finding solutions that meet certain conditions, like not having three '1's in a row.

By understanding and working on this problem, you'll improve your skills in working with binary numbers, using certain programming functions, and creating solutions for specific rules or constraints. It's a good exercise for anyone learning to program because it challenges you to think logically and come up with efficient solutions.

In summary, this problem is like a puzzle where you manipulate binary numbers to get the highest value possible without breaking the rule of having three '1's in a row. It's a great way to practice important programming skills and gain confidence in solving similar problems.

Easiest Explanation:-

Imagine you have a number that is made up of a bunch of "on" and "off" switches, like a string of Christmas lights. We want to turn off as many of these switches as possible, but we have to be careful not to turn off three "on" switches in a row, because that would break the rules.

So we can only turn off switches that are already "on", and we want to turn off as many as possible without breaking the rules. What is the highest number we can get, where no three "on" switches are next to each other?

A generalized approach to solve this problem in a way that can be implemented in any programming language:

1. Convert the input number n to a binary string.
2. Iterate through the string from left to right.
3. If three characters in string are "1" in sequence (rule voilates here), make last string charcter of that sequence to "0"
4. Convert the modified string back to an integer and simply return the answer.

I hope that explanation helps! If you find any difficulty in solving, feel free to comment with your doubts.
CPP Code
class Solution { public: int noConseBits(int n) { string str = bitset<32>(n).to_string(); for(int i=0;i<str.size()-1;i++){ // checking if three characters in string are "1" in sequence if(str[i]=='1' and str[i+1]=='1' and str[i+2]=='1'){ str[i+2]='0'; } } return stoi(str, 0, 2); } };

Here's how the function works:

The implementation first uses the bitset class to create a binary string representation of n with 32 bits, and assigns it to the string variable called str. The loop then iterates through the binary string str, and for each bit, checks if the current bit and the next two bits are all set to 1. If so, the third bit is set to 0 to avoid having three consecutive 1's in the binary string. This is done to ensure that the modified binary number has no consecutive three 1's. Finally, the modified binary string is converted back to an integer using the stoi() function and returned.

It is important to note that this implementation assumes that the input integer n is non-negative and can be represented by 32 bits.

Conclusion

In summary, taking on the challenge of this problem introduces beginners to the core ideas of programming and computer science. When you play with the 0s and 1s in binary numbers, avoiding three '1' bits in a row, you're actually diving into the nuts and bolts of how computers work. It's like solving a puzzle, but the real value comes from learning to use bitwise operations—essential for writing code that runs efficiently.

This exercise isn't just a game; it's a hands-on journey into understanding computer memory and crafting optimized algorithms for practical use. Beginners get to grasp the inner workings of computers, gaining insight into how information is handled at its most basic level. Aspiring programmers don't just improve their problem-solving skills; they build a foundation for diving into more advanced computer science topics. So, what seems like a playful challenge is, in fact, a door opening to the deep and practical aspects of programming, setting the stage for further exploration and expertise in the coding world.


Stay up-to-date with our latest content by subscribing to our channel! Don't miss out on our next video - make sure to subscribe today.



No comments: