fbpx

The Logic Problem with Arrays and Loops

by

July 28, 2023

6Minute Read

A student sought my help with a logic problem concerning arrays and loops. The challenge was to determine if there is a 1 in the array with a 2 occurring later in the array. Let’s go through the student’s initial attempt at solving the problem:

The Initial Code

javaCopy codefor (int i = 0; i < nums.length; i++) {
    if (nums[i] == 1 && nums[i] == 2) {
        return true;
    }
}
return false;

The for loop appeared to be correct, iterating through the array’s length and accessing elements properly. However, the conditional check inside the loop had a small mistake.

The Key Component

The problem was that the code checked if any element in the array was both 1 and 2 at the same time, which is impossible. Instead, we need to check for a 1 first and then search for a 2 later in the array. To achieve this, we can use a boolean variable, let’s call it has1, initially set to false. This variable will help us remember if we have found a 1 while iterating through the array.

Corrected Solution

javaCopy codeboolean has1 = false;

for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 1) {
        has1 = true; // We found a 1
    } else if (has1 && nums[i] == 2) {
        return true; // We found a 2 after a 1
    }
}

return false; // 1 and 2 were not found in the required order

Now, we first check for 1, and if the number is not 1, we verify if we have already found a 1 (has1) and if the current number is 2 (nums[i] == 2). This approach helps us find a 2 occurring after a 1 in the array.

Conclusion

Remember, when your program needs to “remember” something, you should use a variable. Consider what information your program needs to retain and what data type can best represent it. In cases where you need a simple switch to indicate existence or absence, boolean variables are your solution.

Happy coding!

Start Your Journey Today

Interested in changing careers and becoming a software developer? Learn more about Promineo Tech’s programs and how we help students make this transition through affordable, low-risk technology education: Start Your Journey: Exploring Promineo Tech’s Coding Programs for Career Transition

Related Articles

12 Minute Read

24 Pro Tips for Balancing Full-Time Work and a Coding Bootcamp

You’re already juggling the demands of a full-time job, and now you’re adding a coding bootcamp into the mix. Sounds challenging, right? Well, you’re not alone. Most people who choose to take online coding bootcamps to improve their skills find themselves in a similar situation.  Coding bootcamps have become increasingly popular in recent years as […]

5 Minute Read

The Flexibility of Coding Bootcamps for Full-Time Workers

Are you a full-time worker that feels stuck in your current career?   Many people in your position find themselves at a crossroads, yearning for something more, but feel trapped by the demands of a 9-to-5 job—which can often leave little room for pursuing a new path.   However, there’s hope: coding bootcamps.   In this blog […]

8 Minute Read

Student Spotlight: Jolene Melanson

In high school, people called Jolene Melanson “bossy.” But, her classmates never objected to being in her group projects. After all, she knew how to lead and get everyone an A.   She’s taken these natural-born leadership skills with her throughout her post-high school career, too. Whether as a Tech Consultant at Target or a Team […]

In the News

Check out Promineo Tech in the news.