- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Permutation of a number whose sum with the original number is equal to another given number

In this article, we'll delve into a fascinating problem that involves numbers and permutations: "Permutation of a number whose sum with the original number is equal to another given number". This problem offers a unique intersection of number theory and combinatorics, making it a compelling challenge to tackle.

To clarify, given an original number and a target number, we need to find a permutation of the original number such that, when we add the original number and its permutation, we get the target number.

## Understanding the Problem

In essence, this problem combines the concepts of number permutation, summation, and equality checking. The challenge lies in finding the correct permutation (or rearrangement of digits) that satisfies the condition provided.

## Algorithm Explanation

The algorithm to solve this problem is as follows −

Count the frequency of each digit in the original number and the target number.

Compare the frequencies. If they match, it means there is a valid permutation. If they do not match, there is no valid permutation.

### Example

Here is a C++ solution that employs the algorithm described above −

#include<bits/stdc++.h> using namespace std; bool isPermutation(int original, int target) { vector<int> countOriginal(10, 0), countTarget(10, 0); while (original > 0) { countOriginal[original % 10]++; original /= 10; } while (target > 0) { countTarget[target % 10]++; target /= 10; } for (int i = 0; i < 10; i++) { if (countOriginal[i] != countTarget[i]) { return false; } } return true; } int main() { int original = 1234; int target = 2468; if (isPermutation(original, target - original)) { cout << "Yes, there is a permutation of the original number that satisfies the condition." << endl; } else { cout << "No, there is no permutation of the original number that satisfies the condition." << endl; } return 0; }

### Output

Yes, there is a permutation of the original number that satisfies the condition.

In the isPermutation function, we first initialize two vectors countOriginal and countTarget to count the frequency of digits in the original number and the target number, respectively. We then iterate over each digit in the original and target number and increment the corresponding count. Finally, we compare the counts. If they match, we return true; otherwise, we return false.

The main function sets the original and target numbers and checks if there is a valid permutation of the original number that satisfies the condition.

## Testcase Example

Let's take the original number as 1234 and the target number as 2468. The difference of the target and the original number is 1234. So, we need to check if there is a permutation of 1234 that equals 1234 itself. As it is evident, the original number is a permutation of itself, so the output will be "Yes, there is a permutation of the original number that satisfies the condition."

## Time and Space Complexity

The time complexity of this algorithm is O(n), where n is the number of digits in the given numbers. This is because we are iterating over each digit in both the original number and the target number.

The space complexity is O(1), as the size of the vectors countOriginal and countTarget is constant (10), regardless of the input size.

## Conclusion

In this article, we explored a fascinating problem that blends the concepts of permutations, addition, and number equality. We implemented a C++ solution that leverages the frequency of digits in the original and target numbers.

This problem provides a unique challenge and offers a great way to practice your problem-solving skills, especially in number theory and combinatorics.