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're the solutions that employs the algorithm described above −

#include <stdio.h>
int isPermutation(int original, int target) {
   int countOriginal[10] = {0};
   int 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 0;
      }
   }
   return 1;
}
int main() {
   int original = 1234;
   int target = 2468;

   if (isPermutation(original, target - original)) {
      printf("Yes, there is a permutation of the original number that satisfies the condition.\n");
   } else {
      printf("No, there is no permutation of the original number that satisfies the condition.\n");
   }
   return 0;
}

Output

Yes, there is a permutation of the original number that satisfies the condition.
#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.
public class Main {
   static boolean isPermutation(int original, int target) {
      int[] countOriginal = new int[10];
      int[] countTarget = new int[10];

      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;
   }

   public static void main(String[] args) {
      int original = 1234;
      int target = 2468;

      if (isPermutation(original, target - original)) {
         System.out.println("Yes, there is a permutation of the original number that satisfies the condition.");
      } else {
         System.out.println("No, there is no permutation of the original number that satisfies the condition.");
      }
   }
}

Output

Yes, there is a permutation of the original number that satisfies the condition.
def is_permutation(original, target):
   count_original = [0] * 10
   count_target = [0] * 10

   while original > 0:
      count_original[original % 10] += 1
      original //= 10

   while target > 0:
      count_target[target % 10] += 1
      target //= 10

   return count_original == count_target

original = 1234
target = 2468

if is_permutation(original, target - original):
   print("Yes, there is a permutation of the original number that satisfies the condition.")
else:
   print("No, there is no permutation of the original number that satisfies the condition.")

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 the solutions 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.

Updated on: 27-Oct-2023

224 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements