Permutation of a number whose sum with the original number is equal to another given number
Last Updated :
25 May, 2021
Given two integers A and C, the task is to check if there exists a permutation of the number A such that the sum of the number A and its permutation is equal to C.
Examples:
Input: A = 133, C = 446
Output: Yes
Explanation: One of the permutation of A is 313. Therefore, sum = 133 + 313 = 446, which is equal to C.
Input: A = 200, C = 201
Output: No
Naive Approach: The simplest approach to solve the problem is to generate all permutations of the number A and add it with the original value of A. Now, check if their sum is equal to C or not.
Time Complexity: O((log10A)!)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to subtract the value of A from C and check if there exists a permutation of A which is equal to the difference between C and A or not. Follow the steps below to solve the problem:
- Subtract A from C and check if C is less than 0 or not. If found to be true, then print “No”.
- Otherwise, perform the following steps:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void checkPermutation( int a, int c)
{
c -= a;
if (c <= 0) {
cout << "No" ;
return ;
}
string s = to_string(a);
string k = to_string(c);
sort(s.begin(), s.end());
sort(k.begin(), k.end());
if (k == s) {
cout << "Yes\n"
<< c;
}
else {
cout << "No" ;
}
}
int main()
{
int A = 133, C = 446;
checkPermutation(A, C);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void checkPermutation( int a, int c)
{
c -= a;
if (c <= 0 )
{
System.out.println( "No" );
return ;
}
String s = sortString(Integer.toString(a));
String k = sortString(Integer.toString(c));
if (k.equals(s))
{
System.out.println( "Yes" );
System.out.println(c);
}
else
{
System.out.println( "No" );
}
}
public static String sortString(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
public static void main(String[] args)
{
int A = 133 , C = 446 ;
checkPermutation(A, C);
}
}
|
Python3
def checkPermutation(a, c):
c - = a
if (c < = 0 ):
print ( "No" )
return
s = str (a)
k = str (c)
res = ''.join( sorted (s))
s = str (res)
res = ''.join( sorted (k))
k = str (res)
if (k = = s):
print ( "Yes" )
print (c)
else :
print ( "No" )
if __name__ = = '__main__' :
A = 133
C = 446
checkPermutation(A, C)
|
C#
using System;
class GFG
{
static void checkPermutation( int a, int c)
{
c -= a;
if (c <= 0) {
Console.Write( "No" );
return ;
}
string s = a.ToString();
string k = c.ToString();
char [] arr = s.ToCharArray();
Array.Sort(arr);
char [] brr = k.ToCharArray();
Array.Sort(brr);
if (String.Join( "" , brr) == String.Join( "" , arr)) {
Console.Write( "Yes\n" + c);
}
else {
Console.WriteLine( "No" );
}
}
public static void Main()
{
int A = 133, C = 446;
checkPermutation(A, C);
}
}
|
Javascript
<script>
function checkPermutation(a, c)
{
c -= a;
if (c <= 0) {
print( "No" );
return ;
}
var s = a.toString();
var k = c.toString();
s= s.split( '' ).sort((a, b) => a.localeCompare(b)).join( '' );
k = k.split( '' ).sort((a, b) => a.localeCompare(b)).join( '' );
if (k == s) {
document.write( "Yes" + "<br>" + c);
}
else {
document.write( "No" );
}
}
var A = 133, C = 446;
checkPermutation(A, C);
</script>
|
Time Complexity: O((log10A)*log (log10A))
Auxiliary Space: O(log10A)
Share your thoughts in the comments
Please Login to comment...