5

A right array

Let's assume we have an array with an N length, made of capital letters A, B, and C. We call the array 'a right array' if between every two C letters which come one after another in the array, there are more A letters than B letters. My job is to discover whether or not a given array is 'right' and if so, I should print out "RIGHT", else I should print for how many pieces (places between to Cs) the given condition is untrue (there are more Bs than As).

Input: In the first line we enter the number of lettes in the array N (1 < N > 200). In the next line we enter the array without empty spaces in-between.

Output: Print out the answer in a single line.

Examples:

  • Input: 16 ABBCAABCACBAAACB Output: RIGHT

  • Input: 15 ABBCABCACBAAACB Output: 1

  • Input: 14 CABCABBCBAABBC Output: 3

Now, I have tried solving this problem, but the third example isn't working for me - I get an output of 2 and as given above I should get 3, other than that - it compiles perfectly fine.

    #include <iostream>

using namespace std;

int main()
{
    int N;
    cin >> N;
    char Ar[N];
    int A = 0;
    int B = 0;
    int piece = 0;
    int attempt = 0;

    for (int i = 0; i < N; i++) {
        cin >> Ar[i];
    }

    for (int i = 0;  i < N; i++) {
        if (Ar[i] == 'C') {
            for (int j = i + 1; i < N; j++) {
                if (Ar[j] == 'A') {
                    A++;
                } else if (Ar[j] == 'B') {
                    B++;
                } else if (Ar[j] == 'C') {
                    i = j;
                    break;
                }
            }
            if (A > B) {
                piece++;
                attempt++;
            } else if (A <= B) {
                attempt++;
            }
            A = 0;
            B = 0;
        }
    }


    if (piece == attempt) {
        cout << "RIGHT";
    } else {
        cout << attempt - piece;
    }

    return 0;
}
21
  • 4
    char Ar[N]; is not valid C++ Commented Mar 2, 2019 at 18:48
  • 1
    for 14 CABCABBCBAABBC the output is 2, not 3 Commented Mar 2, 2019 at 18:52
  • 1
    @N.T. so Output must be Expected output in the question to put it clear ^^ Commented Mar 2, 2019 at 18:57
  • 1
    @N.T. "isn't working for me" is not a useful problem description. What do you mean by "isn't working"? Does it not compile? If so, what is the error message? If not, does it behave different than you expected? How is it different? Commented Mar 2, 2019 at 19:11
  • 1
    @N.T. put the relevant information in the question. Commented Mar 2, 2019 at 19:15

4 Answers 4

3

The problem is in the case

        } else if (Ar[j] == 'C') {
            i = j;
            break;
        }

the reason is that once you get back to the main loop i will be incremented, so the ending C will not be considered to be the start of a new group. Your code is basically checking every other block.

You should set

i = j-1;

instead, so that after incrementing i will be the index of the C.

Also you should re-initialize A and B to zero when evaluating a section.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried, the program doesn’t work at all when I do that. I break out of the loop, I’m not sure if i++ works even if you break before the loop has finished.
break exits the inner loop, not the outer one.
@N.T.: there's also another error, you only set A and B to zero once, it should be done instead inside the outer loop, before processing a section.
I need it to break out of the second loop because it’s that loop which checks the elements, and I set the A and B to zero because I don’t need them I just need them for a comparason to know whether or not to add a successful piece to the code or just a useless attempt.
2

You have several problems, as outlined in the code comments below:

int N;
cin >> N;
std::vector<char> Ar(N);

for (int i = 0; i < N; i++) {
    cin >> Ar[i];
}

int piece = 0;
int attempt = 0;

for (int i = 0;  i < N - 1; i++) {
    if (Ar[i] != 'C') {
        // Skip letters until the first C
        continue;
    }
    int A = 0;
    int B = 0;
    int j = i + 1;
    for (; j < N; j++) {
        if (Ar[j] == 'A') {
            A++;
        } else if (Ar[j] == 'B') {
            B++;
        } else if (Ar[j] == 'C') {
            // We only account for blocks between Cs
            attempt++;
            if (A > B) {
                piece++;
            }
            break;
        }
    }
    // Next piece starts at j, i will be incremented by outer loop
    i = j - 1;
}

Comments

1
#include <iostream>

using namespace std;

int main() {
  int numChars;
  cin >> numChars;

  char array[numChars];

  for (int i = 0; i < numChars; ++i) {
    cin >> array[i];
  }

  int numBrokenPieces = 0;
  int numAs = 0;
  int numBs = 0;

  bool inPiece = false;
  for (int i = 0; i < numChars; ++i) {
    if (array[i] == 'C') {
      if (!inPiece) {
        inPiece = true;
        continue;
      } else {
        if (numBs >= numAs) {
          ++numBrokenPieces;
        }
        numAs = 0;
        numBs = 0;
      }
    } else {
      if (inPiece) {
        if (array[i] == 'A') {
          ++numAs;
        } else if (array[i] == 'B') {
          ++numBs;
        }
      }
    }
  }

  if (numBrokenPieces == 0) {
    cout << "RIGHT";
  } else {
    cout << numBrokenPieces;
  }

  return 0;
}

Comments

1

Well, you could also approach this a bit differently:

string str;
bool counting = false;
int counter = 0, notRightCounter = 0;

cout << "String: ";
cin >> str;                                  // user enters whole string at once 

for (char& c : str) {                        // for each char in string
    if (c == 'C') {                          // start or stop counting when C is found
        counting = !counting;
        if (!counting && counter <= 0) {     // Check if piece between Cs is right
            notRightCounter++;
            counting = !counting;
        }
        counter = 0;
        continue;                            // Continue to next char after 'C'
    }

    if (counting)                            // Keeping count of A's and B's
        switch (c) {
            case 'A':
            counter++;
            break;
        case 'B':
            counter--;
            break;
        }
}

// Print results
if (notRightCounter != 0)                   
    cout << "Not right! " << "Not right counter: " << notRightCounter;
else
    cout << "Right!";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.