Finding max depth of nested parentheses The following code finds the max depth of nested parentheses in the given string - for example, for {}{}{{}} it outputs 2. If the given string doesn't contain correct sequence of parentheses (like {{}), it outputs "NIE". User enters the length of the string and the string.

My concerns: the algorithm is somewhat slow, and I don't think it's really the best way to solve this problem.

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;
using namespace std;

int maxDepth(string S)
{
    int current_max = 0; 
    int max = 0;    
    int n = S.length();


    for (int i = 0; i&lt; n; i++)
    {
        if ((S[i] == '(') || (S[i] == '{') || (S[i] == '['))
        {
            current_max++;


            if (current_max&gt; max)
                max = current_max;
        }
        else if ((S[i] == ')') || (S[i] == '}') || (S[i] == ']'))
        {
            if (current_max&gt;0)
                current_max--;
            else
                return -1;
        }
    }

    if (current_max != 0)
        return -1;

    return max;
}


int main()
{
    int n, i;
    cin &gt;&gt; n;
    string s;
    cin &gt;&gt; s;
    for (i=0; i&lt;n; i++)
    {

        if (count(s.begin(), s.end(), '(')!=count(s.begin(), s.end(), ')'))
        {

            cout &lt;&lt; "NIE\n";

            return 0;

        };

        if (count(s.begin(), s.end(), '{')!=count(s.begin(), s.end(), '}'))
        {

            cout &lt;&lt; "NIE\n";

            return 0;

        };

            if (count(s.begin(), s.end(), '[')!=count(s.begin(), s.end(), ']'))
        {

            cout &lt;&lt; "NIE\n";

            return 0;

        };

    };
    cout &lt;&lt; maxDepth(s) &lt;&lt; "\n";
    return 0;
}
Soooooooo many iterations

You can check if the nested braces are valid in a single run through the string. Your code currently does 6n+1 iterations (!!) - and I have no idea what n is there for. You have a loop which does not reference i at all, so at the very least you could just do that part once (which would drop you to just 7 iterations), but really you should do the loop zero times. That's why it's slow. Soooo much unnecessary work!

Wrong output

If maxDepth returns -1, you will print -1 instead of "NIE". 

A better algorithm

This is a problem that calls for a stack. When you get an open brace, you add it to the stack (updating maximum size as necessary). When you get a close brace, you check the top value of the stack - if it matches, you're good and you pop. If it doesn't match or the stack is empty - it's invalid and you return -1. If at the end of the string the stack isn't empty, it's invalid and you return -1 too. 

That's one single pass. It will also handle the case I mentioned in comments of ({)} which your code would incorrectly return 2.

Code specifics


Avoid using namespace std;
Prefer C++11's range based for loops. In maxDepth(), iterate for (char c : S). 
When you're comparing against multiple things like you do, when you're not actually differentiating between the different values, it's clearer to just use a switch statement:

for (char c : S) {
    switch (c) {
    case '(':
    case '{':
    case '[': 
       ...
       break;
    case ')':
    case '}':
    case ']': 
       ...
       break;
    }
}

In maxDepth(), you don't alter the string, so you should take it by reference to const. 
Declare variables when you need them. In main(), you loop over i so declare i in the for-loop itself, not up front. This isn't C.