Skip to content

Instantly share code, notes, and snippets.

@manosriram
Created July 15, 2021 13:39
Show Gist options
  • Save manosriram/06e47fac7f6b0091fefa4aba490fef2b to your computer and use it in GitHub Desktop.
Save manosriram/06e47fac7f6b0091fefa4aba490fef2b to your computer and use it in GitHub Desktop.
Technical Round 2 - ManoSriram
int Solve(vector<int> a) {
map<int, int> mp;
For (int t=0;t<a.size();++t) {
++mp[a[t]];
If (mp[a[t]] > 1) return a[t];
}
Return -1;
}
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int getNextSymbol(string s, int in) {
for (int t=in;t<s.length();++t) {
if (s[t] == '+' || s[t] == '*') return t;
}
return -1;
}
int Solve(string s) {
int res = 0;
int n = s.length();
for (int t=0;t<n;++t) {
int ind = getNextSymbol(s, t+1);
if (ind == -1) {
}
if (s[t] == '+' && s[ind] == '*') {
string left;
int lIndex = t-1;
while (lIndex >= 0 && (s[lIndex] != '*' || s[lIndex] != '+')) {
left = s[lIndex] + left;
--lIndex;
}
cout << left << endl;
res += stoi(left);
continue;
} if (s[t] == '*') {
string left, right;
int lIndex = t-1, rIndex = t+1;
while (lIndex >= 0 && (s[lIndex] != '*' || s[lIndex] != '+')) {
left += s[lIndex];
--lIndex;
}
while (rIndex < n && (s[rIndex] != '*' || s[rIndex] != '+')) {
right += s[rIndex];
++rIndex;
}
res += stoi(left) * stoi(right);
t += rIndex + 1;
} if (s[t] == '+') {
// cout << ind << endl;
string left, right;
int lIndex = t-1, rIndex = t+1;
while (lIndex >= 0 && (s[lIndex] != '*' && s[lIndex] != '+')) {
left = s[lIndex] + left;
--lIndex;
}
while (rIndex < n && (s[rIndex] != '*' && s[rIndex] != '+')) {
right += s[rIndex];
++rIndex;
}
res += stoi(left) + stoi(right);
t += rIndex;
}
}
int index = n - 1;
string final = "";
while (index >= 0 && s[index] != '+') {
if (s[index] == '*') {
final = "";
break;
}
else final = s[index] + final;
index--;
}
// cout << final << endl;
res += stoi(final);
return res;
}
int main()
{
cout << Solve("1+2*10+30+2");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment