Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

991. Broken Calculator

Medium54.8% Acceptance
MathGreedy
Asked by:
A
Arcesium
ProblemSolutions (12)VideosCompanies (1)Notes

Problem Statement

There is a broken calculator that has the integer startValue on its display initially. In one operation, you can:

  • multiply the number on display by 2, or
  • subtract 1 from the number on display.

Given two integers startValue and target, return the minimum number of operations needed to display target on the calculator.

Example 1:

Input: startValue = 2, target = 3
Output: 2
Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.

Example 2:

Input: startValue = 5, target = 8
Output: 2
Explanation: Use decrement and then double {5 -> 4 -> 8}.

Example 3:

Input: startValue = 3, target = 10
Output: 3
Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}.

Constraints:

  • 1 <= startValue, target <= 109
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

The key to solving #991 Broken Calculator is realizing that working backwards from the target is more efficient than building forward from the start value. The calculator allows two operations: *2 and -1. If we simulate forward, the search space grows quickly.

Instead, apply a greedy strategy by reducing the target toward startValue. If the target is greater than the start value, check whether it is even or odd. When the target is even, it likely came from a doubling operation, so dividing by 2 reverses that step. When the target is odd, incrementing it makes it even so it can be halved in the next step. This minimizes unnecessary operations.

Once the target becomes less than or equal to the start value, the remaining difference can be handled with simple decrement operations. This greedy reverse simulation keeps the solution efficient with a logarithmic number of steps.

Complexity

ApproachTime ComplexitySpace Complexity
Greedy Reverse SimulationO(log target)O(1)

Video Solution Available

Sajjaad Khader

View all video solutions

Solutions (12)

Reverse Operations Approach

Instead of moving from the startValue to target, consider reversing the operations. Start from the target and attempt to reach the startValue. If the target is even, divide it by 2. If it is odd, add 1 to it. Continue until the target is less than or equal to the startValue. This allows you to efficiently compute the minimum steps by potentially making large reductions through division by 2.

Time Complexity: O(log n), where n is the target due to halving operation.
Space Complexity: O(1) as no extra space is used.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3int brokenCalc(int startValue, int target) {
4    int operations = 0;
5    while (

Explanation

This C program uses a loop to apply reverse operations to reduce the target to a value no larger than the startValue. If target is even, it is divided by 2; if odd, it is incremented by 1. Lastly, the remaining difference between startValue and target is added to the operations, as this represents the needed decrements.

Greedy Approach

Using a greedy approach, always attempt the operation that brings the target closer to the startValue significantly, which is multiplication by 2 in the reverse. Only when all doubling possibilities are exhausted, should we use subtraction/addition operations.

Time Complexity: O(log n) due to division operations.
Space Complexity: O(1).

CC++JavaPythonC#JavaScript
1#include

Video Solutions

Watch expert explanations and walkthroughs

Bro Destroyed Leetcode 💀 then got kicked out 😭

Sajjaad Khader
0:5024,375 views

Asked By Companies

1 companies
A
Arcesium

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Minimum FactorizationMedium
Maximum SwapMedium
Monotone Increasing DigitsMedium
Rabbits in ForestMedium
More similar problems

Related Topics

MathGreedy

Problem Stats

Acceptance Rate54.8%
DifficultyMedium
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Broken Calculator asked in FAANG interviews?

Yes, problems similar to Broken Calculator appear in FAANG-style interviews because they test greedy thinking and reverse problem-solving strategies. It evaluates a candidate's ability to recognize optimal transformations.

What is the optimal approach for Broken Calculator?

The optimal approach uses a greedy reverse strategy. Instead of transforming startValue to target, you work backward from the target by dividing by 2 when even or incrementing when odd until it reaches the start value.

Why is the reverse greedy method used in Broken Calculator?

Forward simulation can lead to many unnecessary operations and a larger search space. By reversing the process, you always make the most impactful move—halving when possible—resulting in a logarithmic number of steps.

What data structure is required for the Broken Calculator problem?

No special data structure is needed. The solution relies on simple arithmetic operations and a loop to repeatedly adjust the target value until it matches the start value.

target
>
startValue
)
{
6
operations
++
;
7
if
(
target
%
2
==
1
)
{
8
target
++
;
9
}
else
{
10
target
/=
2
;
11
}
12
}
13
return
operations
+
(
startValue
-
target
)
;
14
}
15
16
int
main
(
)
{
17
int
startValue
=
3
,
target
=
10
;
18
printf
(
"%d\n"
,
brokenCalc
(
startValue
,
target
)
)
;
19
return
0
;
20
}
<stdio.h>
2
3
int
brokenCalc
(
int
startValue
,
int
target
)
{
4
int
operations
=
0
;
5
while
(
target
!=
startValue
)
{
6
if
(
target
<
startValue
)
{
7
return
operations
+
(
startValue
-
target
)
;
8
}
9
if
(
target
%
2
!=
0
)
{
10
target
++
;
11
}
else
{
12
target
/=
2
;
13
}
14
operations
++
;
15
}
16
return
operations
;
17
}
18
19
int
main
(
)
{
20
int
startValue
=
3
,
target
=
10
;
21
printf
(
"%d\n"
,
brokenCalc
(
startValue
,
target
)
)
;
22
return
0
;
23
}

Explanation

This C solution looks at the current relation between target and startValue, opting to expedite reduction using division when possible, and slow down with addition or subtraction when necessary.