diff --git a/Parenthesis-Checker.cpp b/Parenthesis-Checker.cpp new file mode 100644 index 0000000..84fc017 --- /dev/null +++ b/Parenthesis-Checker.cpp @@ -0,0 +1,70 @@ +#include +#include + +using namespace std; + +// Given an expression string x. Examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp. +// For example, the function should return 'true' for exp = “[()]{}{[()()]()}” and 'false' for exp = “[(])”. +bool ispar(string expr) +{ + + stack s; + char x; + + for (int i = 0; i < expr.length(); i++) + { + if (expr[i] == '(' || expr[i] == '[' + || expr[i] == '{') + { + s.push(expr[i]); + continue; + } + + if (s.empty()) + return false; + + switch (expr[i]) { + case ')': + + x = s.top(); + s.pop(); + if (x == '{' || x == '[') + return false; + break; + + case '}': + + x = s.top(); + s.pop(); + if (x == '(' || x == '[') + return false; + break; + + case ']': + x = s.top(); + s.pop(); + if (x == '(' || x == '{') + return false; + break; + } + } + + + return (s.empty()); + +} + + + +int main() +{ + string s; + cin>>s; + + if(ispar(s)) + cout<<"Balanced"< +#include + +using namespace std; + +// A peak element in an array is the one that is not smaller than its neighbours. +// Given an array arr[] of size N, find the index of any one of its peak elements. + +int findPeakUtil(int arr[], int low, int high, int n) +{ + int mid = low + (high - low) / 2; + + if ((mid == 0 || arr[mid - 1] <= arr[mid]) && (mid == n - 1 || arr[mid + 1] <= arr[mid])) + return mid; + + else if (mid > 0 && arr[mid - 1] > arr[mid]) + return findPeakUtil(arr, low, (mid - 1), n); + + else + return findPeakUtil(arr, (mid + 1), high, n); +} + + +int peakElement(int arr[], int n) +{ + return findPeakUtil(arr, 0, n - 1, n); + +} + + +int main() +{ + int n; + cin>>n; + int arr[n]; + for(int i=0;i>arr[i]; + + cout<