//Enter you code here.
//Please indent properly.
<?php
//program to find the common elements of the two array
//here we have to array A and B from which w have to find the common element
//first we sort then using merge sort and after then for traversing through
//the array in one iteration we can find the comman elements the given array
//this is an inspace algorithm meansno extra space is needed
//best case time complexity=O(nlogn)
//O(nlogn)-> for sorting
//O(n)-> for while loop to find comman element
//average case time complexity=O(nlogn)
//O(nlogn)-> for sorting
//O(n)-> for while loop to find comman element
//worst case time complexity =O(nlogn)
//O(nlogn)-> for sorting
//O(n)-> for while loop to find comman element
$A = array ( 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 36 , 58 , 27 , 48 ) ; $B = array ( 3 , 10 , 4 , 5 , 6 , 8 , 12 , 24 , 37 , 27 , 50 ) ; $counter1 = 0 ;
$counter2 = 0 ;
while ( ( $counter1 < $size1 ) && ( $counter2 ) < ( $size2 ) ) //traversing through the array
{
if ( $A [ $counter1 ] == $B [ $counter2 ] )
{
array_push ( $commonArray , $A [ $counter1 ] ) ; //to enter comman element in the output array $counter1 = $counter1 + 1 ;
$counter2 = $counter2 + 1 ;
}
else if ( $A [ $counter1 ] < $B [ $counter2 ] )
{
$counter1 = $counter1 + 1 ; }
else
{
$counter2 = $counter2 + 1 ;
}
}
print_r ( $commonArray ) ; //to print the output array ?>
Ly9FbnRlciB5b3UgY29kZSBoZXJlLgovL1BsZWFzZSBpbmRlbnQgcHJvcGVybHkuCgo8P3BocAovL3Byb2dyYW0gdG8gZmluZCB0aGUgY29tbW9uIGVsZW1lbnRzIG9mIHRoZSB0d28gYXJyYXkKLy9oZXJlIHdlIGhhdmUgdG8gYXJyYXkgQSBhbmQgQiBmcm9tIHdoaWNoIHcgaGF2ZSB0byBmaW5kIHRoZSBjb21tb24gZWxlbWVudAovL2ZpcnN0IHdlIHNvcnQgdGhlbiB1c2luZyBtZXJnZSBzb3J0IGFuZCBhZnRlciB0aGVuIGZvciB0cmF2ZXJzaW5nIHRocm91Z2gKLy90aGUgYXJyYXkgaW4gb25lIGl0ZXJhdGlvbiB3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HdpdGggaXRzIHNpZ24KICAgICAgc2lnbiA9IDE7ICAgICAgICAgICAvLyBTZXQgc2lnbiBmb3IgbmV4dCBudW1iZXIKICAgICAgbnVtID0gMDsgICAgICAgICAgICAvLyBSZXNldCBjdXJyZW50IG51bWJlcgogICAgfSBlbHNlIGlmIChjID09ICctJy5jb2RlVW5pdEF0KDApKSB7CiAgICAgIGFucyArPSBzaWduICogbnVtOwogICAgICBzaWduID0gLTE7CiAgICAgIG51bSA9IDA7CiAgICB9IGVsc2UgaWYgKGMgPT0gJygnLmNvZGVVbml0QXQoMCkpIHsKICAgICAgLy8gUHVzaCBjdXJyZW50IHJlc3VsdCBhbmQgc2lnbiB0byBzdGFjayBmb3IgcGFyZW50aGVzZXMgY29udGV4dAogICAgICBzdGFjay5hZGQoYW5zKTsKICAgICAgc3RhY2suYWRkKHNpZ24pOwogICAgICBhbnMgPSAwOyAgLy8gUmVzZXQgcmVzdWx0IGluc2lkZSBwYXJlbnRoZXNlcwogICAgICBzaWduID0gMTsgLy8gUmVzZXQgc2lnbiBpbnNpZGUgcGFyZW50aGVzZXMKICAgIH0gZWxzZSBpZiAoYyA9PSAnKScuY29kZVVuaXRBdCgwKSkgewogICAgICBhbnMgKz0gc2lnbiAqIG51bTsgIC8vIFByb2Nlc3MgbnVtYmVyIGJlZm9yZSBjbG9zaW5nIHBhcmVudGhlc2VzCiAgICAgIG51bSA9IDA7ICAgICAgICAgICAgLy8gUmVzZXQgbnVtYmVyCiAgICAgIHNpZ24gPSBzdGFjay5yZW1vdmVMYXN0KCk7ICAvLyBHZXQgcHJldmlvdXMgc2lnbgogICAgICBhbnMgPSBzdGFjay5yZW1vdmVMYXN0KCkgKyBzaWduICogYW5zOyAgLy8gQWRkIHRvIHByZXZpb3VzIHJlc3VsdAogICAgfQogIH0KCiAgLy8gQWRkIGFueSByZW1haW5pbmcgbnVtYmVyIGFmdGVyIHRoZSBsb29wCiAgcmV0dXJuIGFucyArIHNpZ24gKiBudW07Cn0K
int calculate(String s) {
int ans = 0; // Final result
int num = 0; // Current number being processed
int sign = 1; // Current sign (1 for positive, -1 for negative)
List<int> stack = []; // Stack to hold previous results and signs
for (int i = 0; i < s.length; i++) {
int c = s.codeUnitAt(i);
if (c >= '0'.codeUnitAt(0) && c <= '9'.codeUnitAt(0)) {
num = num * 10 + (c - '0'.codeUnitAt(0));
} else if (c == '+'.codeUnitAt(0)) {
ans += sign * num; // Process the current number with its sign
sign = 1; // Set sign for next number
num = 0; // Reset current number
} else if (c == '-'.codeUnitAt(0)) {
ans += sign * num;
sign = -1;
num = 0;
} else if (c == '('.codeUnitAt(0)) {
// Push current result and sign to stack for parentheses context
stack.add(ans);
stack.add(sign);
ans = 0; // Reset result inside parentheses
sign = 1; // Reset sign inside parentheses
} else if (c == ')'.codeUnitAt(0)) {
ans += sign * num; // Process number before closing parentheses
num = 0; // Reset number
sign = stack.removeLast(); // Get previous sign
ans = stack.removeLast() + sign * ans; // Add to previous result
}
}
// Add any remaining number after the loop
return ans + sign * num;
}