Practo Interview Question
Development Support EngineersCountry: United States
Interview Type: Phone Interview
#include "bits/stdc++.h"
#define FOR(i, a, n) for(int i = a; i < n; i++)
#define ft first
#define sd second
#define so(x) cout<<(x)<<"\n";
#define si(x) cin>>x;
#define ll long long
#define fastScan ios_base::sync_with_stdio(0); cin.tie(NULL);
#define mod 1000000007
using namespace std;
int main() {
int a, b;
cin>>a>>b;
bool marked[b+1];
for(int i = 0; i <= b; i++)
marked[i] = false;
string s = "", s1 = "";
ll res = 0;
int count = 0;
for(int i = a; i <= b; i++) {
if(marked[i])
continue;
marked[i] = true;
stringstream ss;
ss << i;
s = ss.str();
s1 = s+s;
int size = s.length();
count = 1;
for(int j = 1; j < size; j++) {
string s2 = s1.substr(j, size);
int x = atoi(s2.c_str());
if(x >= a && x <= b && !marked[x]){
count++;
marked[x] = true;
}
}
res += (count*(count-1))/2;
}
cout<<res<<"\n";
}
You've asked for "pairs" when depending on the input A and B you may have arbitrary n-tuples. Could you please clarify the problem.
- Dave September 01, 2015