
- 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
Minimum number of palindromes required to express N as a sum using C++.
Problem statement
Given a number N, we have to find the minimum number of palindromes required to express N as a sum of them
If N = 15 then 2 palindromes are required i.e. 8 and 7.
Algorithm
1. Generate all the palindromes up to N in a sorted fashion 2. Find the size of the smallest subset such that its sum is N
Example
#include <iostream> #include <vector> #include <climits> #include <algorithm> using namespace std; vector<vector<long long>> table; int createPalindrome(int input, bool isOdd){ int n = input; int palindrome = input; if (isOdd) n /= 10; while (n > 0) { palindrome = palindrome * 10 + (n % 10); n /= 10; } return palindrome; } vector<int>generatePalindromes(int n){ vector<int> palindromes; int number; for (int j = 0; j < 2; j++) { int i = 1; while ((number = createPalindrome(i++, j)) <= n) palindromes.push_back(number); } return palindromes; } long long minSubsetSize(vector<int>& vec, int i, int j, int n){ if (n == 0) return 0; if (i > j || vec[i] > n) return INT_MAX; if (table[i][n]) return table[i][n]; table[i][n] = min(1 + minSubsetSize(vec, i + 1, j, n - vec[i]), minSubsetSize(vec, i + 1, j, n)); return table[i][n]; } int requiredPalindromes(int n){ vector<int> palindromes = generatePalindromes(n); sort(palindromes.begin(), palindromes.end()); table = vector<vector<long long>>(palindromes.size(), vector<long long>(n + 1, 0)); return minSubsetSize(palindromes, 0, palindromes.size() - 1, n); } int main(){ int n = 15; cout << "Minimum required palindromes = " << requiredPalindromes(n) << endl; return 0; }
Output
When you compile and execute the above program. It generates the following output −
Minimum required palindromes = 2
- Related Articles
- Minimum numbers needed to express every integer below N as a sum in C++
- Minimum number of operations required to sum to binary string S using C++.
- Minimum number of single digit primes required whose sum is equal to N in C++
- Minimum number of given moves required to make N divisible by 25 using C++.
- Minimum number of power terms with sum equal to n using C++.
- Minimum number of squares whose sum equals to given number n\n
- Count ways to express a number as sum of powers in C++
- Count ways to express ‘n’ as sum of odd integers in C++
- Count ways to express a number as sum of consecutive numbers in C++
- Minimum Number of Platforms Required for a Railway Station using C++.
- Finding minimum number of required operations to reach n from m in JavaScript
- Express $17^2$ as the sum of two consecutive number.
- Express the sum of 9 as an odd consecutive number.
- Minimum number of mails required to distribute all the questions using C++.
- Program to count number of palindromes after minimum number of split of the string in C++

Advertisements