shankhs
BAN USER 0of 0 votes
AnswersBuild a function that takes one string and one regex expression in inputs and output true if the string matches the regex expression.
 shankhs in United States
string: az
regex: az + * (where '*' matches 0 or more character and '+' matches one character) Report Duplicate  Flag  PURGE
Yahoo Software Engineer / Developer
#include <iostream>
#include <limits.h>
using namespace std;
void kadane(int a[], int n){
int gstart=0;
int gend=0;
int lmax=0;
int gmax=INT_MIN;
for(int i=0;i<n;i++){
lmax=max(lmax+a[i],a[i]);
gmax=max(lmax,gmax);
if(gmax==a[i]){
gstart=i;
gend=i;
}
else if(gmax==lmax){
gend=i;
}
}
if(gstart==gend){
gmax=INT_MIN;
for(int i=1;i<n;i++){
gmax=max(gmax,a[i1]+a[i]);
if(gmax==(a[i1]+a[i])){
gstart=i1;
gend=i;
}
}
}
cout<<gstart<<" "<<gend<<" "<<gmax<<endl;
}
int main(){
//int a[]={1,2,3,4,5,9};
int a[]={1,2,3,4,5,9};
int n=sizeof(a)/sizeof(a[0]);
kadane(a,n);
return 0;
}

shankhs
October 20, 2014 #include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
int getOctal(int n){
int oct=0;
int mul=1;
while(n>0){
oct+=(n%8)*mul;
mul*=10;
n/=8;
}
return oct;
}
bool isPal(int n){
stringstream ss;
ss<<n;
string str;
ss>>str;
string revStr = str;
reverse(revStr.begin(),revStr.end());
if(str==revStr) return true;
return false;
}
int main(){
int lo=1;
int hi=100000;
for(int i=lo;i<=hi;i++){
int octal = getOctal(i);
if(isPal(i)&&isPal(octal)){
cout<<i<<" "<<octal<<endl;
}
}
return 0;
}

shankhs
October 20, 2014 The iterators in case of vector is random_access_iterator so effectively the iterator acts like a pointer and can point to any item in the vector. On the other hand in case of list, the iterator is bidirectional_iterator.
If I understand the question correctly, if the prior links are deleted the iterators point to the begin of the vector or the list.
First of all XOR all nums will give result only when there is only one digit that is repeated odd number of times. Clearly here there are multiple digits that are repeated odd number of times. So your first algo will fail. Your second method says sorting which takes nlogn.
 shankhs February 27, 2014
@alokkumar since I cant post reply to your comment:
 shankhs July 18, 2016Consider the array: {6,3,5,2,4,0,1} , n = 6 (1 indicates null)
MinHeap of indexes with min element: 5,3,1,4,2,0 (these are indexes so arr[5] = 0)
Step 1: m = 0, minElement: index = 5, element = 0, ans = 651 = 0 (i think there is a typo in step 1 of your algo)
minHeap = {3,4,1,1,2,0,1}, i = 5
Step 2.1: m=1, minElement: index = 3, element = 2, ans = max(ans, 6  5  1) = 0 which is correct there are no element on the right of 2 greater than 2
minHeap = {1,4,0,1,2,1,1}, i = 3
Step 2.2: m=2, minElement: index = 1, element = 3, ans = max(ans, 632) = 1 which is wrong there are 2 elements on the right greater than 3 (5 and 4) so this algo doesnt work.
Can you please tell me if I have misunderstood something? Thanks