Question: Airtel Payments Bank, Recently asked Online Assessment Questions (19th September 2023) | Largest Value Possible
0
Entering edit mode

ADD COMMENTlink 14 months ago Delta 2.9k
0
Entering edit mode
// Hoping this is correct // correct me if i am wrong #includeusing namespace std; bool cmp(string &s1, string &s2) { if (s1.length() == s2.length()) { return s1 >= s2; } else { if (s1.length() < s2.length()) { int fl = 0; for (int i = 0; i < s1.length(); i++) { if (s1[i] > s2[i]) { fl = 1; break; } if (s2[i] > s1[i]) { fl = 2; break; } } if (fl == 1) return 1; if (fl == 2) return 0; else { int val = s1[s1.length() - 1] - '0'; for (int i = s1.length(); i < s2.length(); i++) { if ((s2[i] - '0') < val) return 1; if ((s2[i] - '0') > val) return 0; } return 1; } } else { int fl = 0; for (int i = 0; i < s2.length(); i++) { if (s1[i] > s2[i]) { fl = 1; break; } if (s2[i] > s1[i]) { fl = 2; break; } } if (fl == 1) return 1; if (fl == 2) return 0; else { int val = s2[s2.length() - 1] - '0'; for (int i = s2.length(); i < s1.length(); i++) { if ((s1[i] - '0') > val) return 1; if ((s1[i] - '0') < val) return 0; } return 1; } } } } int main() { int n; cin >> n; vectorv; for (int i = 0; i < n; i++) { string s; cin >> s; v.push_back(s); } sort(v.begin(), v.end(), cmp); for (auto k : v) cout << k << " "; return 0; }
ADD COMMENTlink 14 months ago ------------- • 70
0
Entering edit mode
#includeusing namespace std; bool cmp(string &s1, string &s2) { if (s1.length() == s2.length()) { return s1 >= s2; } else { if (s1.length() < s2.length()) { int fl = 0; for (int i = 0; i < s1.length(); i++) { if (s1[i] > s2[i]) { fl = 1; break; } if (s2[i] > s1[i]) { fl = 2; break; } } if (fl == 1) return 1; if (fl == 2) return 0; else { int val = s1[s1.length() - 1] - '0'; for (int i = s1.length(); i < s2.length(); i++) { if ((s2[i] - '0') < val) return 1; if ((s2[i] - '0') > val) return 0; } return 1; } } else { int fl = 0; for (int i = 0; i < s2.length(); i++) { if (s1[i] > s2[i]) { fl = 1; break; } if (s2[i] > s1[i]) { fl = 2; break; } } if (fl == 1) return 1; if (fl == 2) return 0; else { int val = s2[s2.length() - 1] - '0'; for (int i = s2.length(); i < s1.length(); i++) { if ((s1[i] - '0') > val) return 1; if ((s1[i] - '0') < val) return 0; } return 1; } } } } int main() { int n; cin >> n; vectorv; for (int i = 0; i < n; i++) { string s; cin >> s; v.push_back(s); } sort(v.begin(), v.end(), cmp); for (auto k : v) cout << k << " "; return 0; }
ADD COMMENTlink 14 months ago ------------- • 70

Login before adding your answer.

Similar Posts
Loading Similar Posts