- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# C++ program to find minimum how many operations needed to make number 0

Suppose we have a numeric string S with n digits. Consider S represents a digital clock and whole string shows an integer from 0 to 10^n - 1. If there are a smaller number of digits, it will show leading 0s. Follow the operations −

Decrease the number on clock by 1, or

Swap two digits

We want the clock will show 0 with minimum number of operations needed. We have to count the number of operations needed to do that.

So, if the input is like S = "1000", then the output will be 2, because we can swap first 1 with last 0, so the string will be "0001" now decrease it by 1 to get "0000".

## Steps

To solve this, we will follow these steps −

n := size of S x := digit at place S[n - 1] for initialize i := 0, when i <= n - 2, update (increase i by 1), do: if S[i] is not equal to '0', then: x := x + (digit at place S[i]) + 1 return x

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; int solve(string S) { int n = S.size(); int x = S[n - 1] - '0'; for (int i = 0; i <= n - 2; i++) if (S[i] != '0') x = x + S[i] + 1 - '0'; return x; } int main() { string S = "1000"; cout << solve(S) << endl; }

## Input

"1000"

## Output

2

- Related Articles
- C++ program to count minimum number of operations needed to make number n to 1
- Program to find minimum operations needed to make two arrays sum equal in Python
- Find minimum operations needed to make an Array beautiful in C++
- C++ program to find minimum number of operations needed to make all cells at r row c columns black
- Program to find number of operations needed to decrease n to 0 in C++
- Program to find minimum number of operations to make string sorted in Python
- C++ program to find minimum how many coins needed to buy binary string
- Program to find minimum number of operations required to make one number to another in Python
- C++ program to find minimum number of punches are needed to make way to reach target
- Program to find minimum operations to make array equal using Python
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Minimum number of operations on an array to make all elements 0 using C++.
- Program to find minimum operations to make the array increasing using Python
- Program to check minimum number of characters needed to make string palindrome in Python
- C++ program to count at least how many operations needed for given conditions

Advertisements