Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth Costly Phone Number problem solution

YASH PAL, 31 July 2024
In this HackerEarth Costly Phone Number problem solution A cell phone company is trying out its new model of cell phone. Here’s how its structure is:
The keypad has 11 buttons corresponding to digits from 0 to 9 and one additional button called Add. After pressing any button from 0 to 9, the corresponding digit appears on the screen. The Add button replaces the last two digits appearing on the screen with their sum taken modulo 10. (See sample test for more clarity). If there are less than two digits currently on the screen, pressing Add does nothing.
Each button has a non-negative cost of pressing associated with it. The cost of pressing Add button is always 0. Given the cost of pressing each button and the target phone number, find the minimum cost of feeding that number into the phone screen using a sequence of button presses.
HackerEarth Costly Phone Number problem solution

HackerEarth Costly Phone Number problem solution.

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int MAX_COST = 1000;
const int MAX_TEST = 1000;
const int MAX_LEN = 1000;

int main() {

//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);

int T, cost[10], ans, n;
string s;
bool ok;

scanf("%d", &T);
assert(T <= MAX_TEST && T);

while (T--) {

ok = true;
ans = 0;

for (int i = 0; i < 10; ++i) {
scanf("%d", cost + i);
assert(cost[i] <= MAX_COST);
}

while (ok) {
ok = false;
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
if (cost[(i + j) % 10] > cost[i] + cost[j]) {
cost[(i + j) % 10] = cost[i] + cost[j];
ok = true;
}
}
}
}

scanf("%d", &n);

cin >> s;

assert(s.size() == n && n <= MAX_LEN && n);

for (int i = 0; i < n; ++i) {
assert(s[i] >= '0' && s[i] <= '9');
ans += cost[s[i] - '0'];
}

printf("%dn", ans);
}
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes