# longest common subsequence how this conditional operator is being used here

#include <iostream>

#include <string>

#include <algorithm>

using namespace std;

int longestCommonSubsequence(const string& a, const string& b)

{

int A = a.size(), B = b.size();

int L[2][B + 1];

for (int i = 0; i <= 1; ++i) L[i][0] = 0;

for (int i = 0; i <= B; ++i) L[0][i] = 0;

for (int i = 1; i <= A; ++i) {

``````int this_i = i % 2, pre_i = this_i ? 0 : 1;//explain the conditional operator how it is being used here

for (int j = 1; j <= B; ++j) {

if (a[i - 1] == b[j - 1]) L[this_i][j] = 1 + L[pre_i][j - 1];

else L[this_i][j] = max(L[pre_i][j], L[this_i][j - 1]);//expalain why we are calculating L[pre_i][j], L[this_i][j - 1]

}
``````

}

return max(L[0][B], L[1][B]);

}

int main() {

string a, b;

cin >> a >> b;

cout << longestCommonSubsequence(a, b) << endl;

}
expalain the use of conditional operator being used and why we are calculating L[pre_i][j], L[this_i][j - 1]

(int this_i = i % 2, pre_i = this_i )?( 0) 1)

Here **int this_i = i % 2, pre_i = this_i **, 0, and 1 are expressions. Notice the use and placement of the colon. The value of a ? expression is determined like this: **int this_i = i % 2, pre_i = this_i ** is evaluated. If it is true, then 0 is evaluated and becomes the value of the entire ? expression. If int this_i = i % 2, pre_i = this_i is false, then 1 is evaluated and its value becomes the value of the expression.

The ? is called a ternary operator because it requires three operands and can be used to replace if-else statements, which have the following form:

``````if(condition)
{
var = X;
}
else
{
var = Y;
}
``````

For example, consider the following code:

``````if(y < 10)
{
var = 30;
}
else
{
var = 40;
}
``````

Above code can be rewritten like this:

var = (y < 10) ? 30 : 40;

1 Like

The format of the conditional operator is

``````expression1 ? expression2 : expression3.
``````

Now let me explain this.

If expression1 evaluates to true ( that is 1 ), then the value of the whole expression is the value of expression2, otherwise, the value of the whole expression is expression3.

Now take this simple example

``````result = marks >= 50 ? 'P' : 'F' ;
``````

result will have the value `'P'` if the expression `marks >= 50` evaluates to true ( or 1 ), otherwise, result will get `'F'`.

Now let us move on to your case

``````int this_i = i % 2, pre_i = this_i ? 0 : 1;
``````

`this_i` is assigned the value of `i % 2`, that is, if i is an even number, `this_i` gets the value `0`, otherwise, if i is an odd number, `this_i` gets the value `1`.

Now moving onto your conditional statement, as mentioned above, `this_i` will have the value `1` or `0` if `i` is odd or even.

Your conditional statement checks if `this_i` is true, that is `1`. If `this_i` has the value `1` then `pre_i` gets the value `0`, otherwise, `pre_i` gets the value `1`.

1 Like

@saiavinashiitr where is the condition in this case since it is a conditional operator and we are doing assignment how can it be??

@arun_as thanks for the explaination

The proper break down should be

``````int (this_i = i % 2), (pre_i = (this_i ? 0 : 1))
``````

That is two variable declarations/initializations happening there.

`this_i` is set to `i % 2`

Based on whether `this_i` is `0` or not, `pre_i` will be set to a particular value. Specifically, `pre_i` is `0`, if `this_i` is `1` and vice versa.

@tijoforyou can you explain the L[this_i][j] = max(L[pre_i][j], L[this_i][j - 1]) part and how the final answer will be in max(L[0][B], L[1][B]);