Find an anagram of a number A that generates a sum C when added to A
Given two positive integers A and C, the task is to check if a number B exists such that A + B = C and B is an anagram of A. If found to be true, then print “YES”. Otherwise, print “NO”.
Input: A = 123, C = 354
Output: YES
Explanation:
231 is an anagram of A and 123 + 231 = 354
Therefore, the required output is “YES”.
Input: A = 123, C = 234
Output: NO
Naive Approach: The simplest approach to solve the problem is to generate all possible permutations of digits of A and check if the sum of A and the current permutation of digits of A is equal to C or not. If found to be true, then print “YES”. Otherwise, if no such permutation is found, print “NO”.
Time Complexity: O(log10(N)!)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:
A + B = C
B = C – A
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void CheckExistsABCAnagramOfA( int A, int C)
{
int B = C - A;
string a = to_string(A);
string b = to_string(B);
sort(a.begin(), a.end());
sort(b.begin(), b.end());
if (a == b) {
cout << "YES\n" ;
}
else {
cout << "NO\n" ;
}
}
int main()
{
int A = 123, C = 354;
CheckExistsABCAnagramOfA(A, C);
}
|
Java
import java.util.*;
class GFG
{
static void CheckExistsABCAnagramOfA( int A, int C)
{
int B = C - A;
String a = String.valueOf(A);
String b = String.valueOf(B);
a = sortString(a);
b = sortString(b);
if (a.equals(b))
{
System.out.print( "YES\n" );
}
else
{
System.out.print( "NO\n" );
}
}
static String sortString(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
public static void main(String[] args)
{
int A = 123 , C = 354 ;
CheckExistsABCAnagramOfA(A, C);
}
}
|
Python3
def CheckExistsABCAnagramOfA(A, C):
B = C - A
a = [ int (x) for x in str (A)]
b = [ int (x) for x in str (B)]
a.sort()
b.sort()
if (a = = b):
print ( "YES" )
else :
print ( "NO" )
A = 123
C = 354
CheckExistsABCAnagramOfA(A, C)
|
C#
using System;
class GFG{
static void CheckExistsABCAnagramOfA( int A, int C)
{
int B = C - A;
String a = String.Join( "" , A);
String b = String.Join( "" , B);
a = sortString(a);
b = sortString(b);
if (a.Equals(b))
{
Console.Write( "YES\n" );
}
else
{
Console.Write( "NO\n" );
}
}
static String sortString(String inputString)
{
char []tempArray = inputString.ToCharArray();
Array.Sort(tempArray);
return new String(tempArray);
}
public static void Main(String[] args)
{
int A = 123, C = 354;
CheckExistsABCAnagramOfA(A, C);
}
}
|
Javascript
<script>
function CheckExistsABCAnagramOfA(A, C)
{
var B = C - A;
var a = (A.toString());
var b = (B.toString());
a = a.split( '' ).sort().join( '' );
b = b.split( '' ).sort().join( '' );
if (a == b) {
document.write( "YES" );
}
else {
document.write( "NO" );
}
}
var A = 123, C = 354;
CheckExistsABCAnagramOfA(A, C);
</script>
|
Time Complexity: O(log10(A)*log(log10(A)))
Auxiliary Space: O(log10(A))
Last Updated :
14 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...