Where is the faulty logic in my solution to this problem?

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Logic
Click For Summary

Discussion Overview

The discussion revolves around a programming problem related to sorting and counting operations involving even and odd numbers in a list. Participants analyze the logic of a provided solution and explore potential errors and debugging strategies.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares a code snippet and expresses uncertainty about where their logic may have failed in solving the problem.
  • Another participant suggests using print statements or a debugger to verify computations and identify issues in the code.
  • A participant introduces a mathematical approach involving sets of odds and evens, proposing that if the sizes of these sets are equal, a certain number of operations can achieve the desired outcome.
  • Concerns are raised about edge cases, specifically regarding the transformation of pairs of numbers and the implications of having surplus items after operations.
  • Another participant points out the importance of commenting code for clarity and questions whether the original poster has a reference solution to compare against.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of the original logic and the debugging process. There is no consensus on the correctness of the proposed solution or the debugging strategies suggested.

Contextual Notes

Some assumptions about the problem's requirements and the behavior of the code remain unverified. The discussion includes unresolved questions about the correctness of the logic and the handling of specific cases.

SlurrerOfSpeech
Messages
141
Reaction score
11
I was working on https://www.hackerrank.com/contests/101hack50/challenges/even-and-odd-boxes for 2.5 hours and couldn't get it right. The programming competition is over, so now it's ok to discuss. I can't figure out where I was going wrong.

Java:
    static void OnesToFront(List<int> list)
    {
        for(int i = 0, j = list.Count - 1; i < j; )
        {
            if(list[i] == 1)
                ++i;
            else if(list[j] != 1)
                --j;
            else
            {
                int temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }
        }
    }
  
    static bool IsEven(int i) { return i % 2 == 0; }
  
    static int MinimumChocolateMoves(int n, int[] X) {
        var shouldBeEven = X.Where((x, i) => IsEven(i) && !IsEven(x)).ToList();
        var shouldBeOdd = X.Where((x, i) => !IsEven(i) && IsEven(x)).ToList();
        if(shouldBeEven.Count == shouldBeOdd.Count)
            return shouldBeEven.Count;
        OnesToFront(shouldBeEven);      
        var q1 = new Queue<int>(shouldBeEven);
        var q2 = new Queue<int>(shouldBeOdd);
        while(q1.Count > 0 && q2.Count > 0)
        {
            q1.Dequeue();
            q2.Dequeue();
        }
        int remaining = q1.Count + q2.Count;
        if(!IsEven(remaining))
            return -1;  
        if(q1.Count > 0)
        {
            int ones = q1.Count(i => i == 1);
            if(ones > q1.Count / 2)
                return -1;
        }      
        return remaining / 2;
    }
 
Last edited by a moderator:
Technology news on Phys.org
With any kind of programming problem, its good to put print statements between key steps to verify that things were computed correctly.

If you have a debugger that's even better for stepping through your code.

In your case, you might want to print out snapshots of your queue to see what got added and where things got tripped up. Basically use the computer to help you debug its problem since programmers don't make mistakes right?

Its much harder for another programmer unfamiliar with your code or the problem its supposed to solve to debug it. However, by talking through your solution and verifying that the computer did it as expected you might discover it for yourself.
 
(I meant to post this in the Programming room)

<< moved... --mentor >>

Ok, let me explain my logic.

Let U be the set of all odds that should be even, and let V be the set of all evens that should be odd.

Fact: for any u in U and v in V, we can make u even and v odd by subtracting from one and adding to the other.

Using the above fact, if |U| = |V| we can perform exactly |U| such operations to get the desired pattern.

If |U| != |V|, we can perform exactly m = Min(|U|, |V|) operations and then we will have a surplus of s = Max(|U|, |V|) - m numbers from one of the sets. If s is odd, then we can not possibly turn those surplus items to the other side. If s is even, then we can.* For example, if we have a pair of evens, like (4,18), we can turn that odd by (4+1,18-1)=(5,17), and we can do that for every pair of evens. Or, if we have a pair of odds, like (7, 19), we can turn that even by (7-1,19+1)=(6,20), and we can do that for every pair of odds.

*Edge case: We can't turn pairs (1,1) into evens.
 
Last edited by a moderator:
SlurrerOfSpeech said:
Ok, let me explain my logic.
Did you figure it out or don't care? Anyway,
1. Write comments that explain the logic, for yourself and especially if posting it for review
2. It seems you forgot to count the chocolates moved in the Dequeue loop.
3. How do you know your program does not work? Do you have a working program to compare against? Is your program giving less or more than the correct answer, or both?
 
  • Like
Likes   Reactions: FactChecker

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
12
Views
3K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 75 ·
3
Replies
75
Views
7K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
55
Views
7K
  • · Replies 97 ·
4
Replies
97
Views
10K