Encuentra dos números cuya suma sea N y no contenga ninguna cifra como K

Dado un número entero N , la tarea es encontrar dos números a y b tales que a + b = N , donde a y b no contienen ninguno de los dígitos como K . Imprima -1 si no es posible.

Ejemplos:

Entrada: N = 100, K = 0
Salida: 1 99
Explicación:
1 + 99 = 100 y ninguno de los números tiene 0.

 Entrada: N = 123456789, K = 2
Salida: 3456790 119999999 
Explicación:
3456790 + 119999999 = 123456789 y ninguno de los números tiene 2.

 

Enfoque: la idea es iterar un bucle desde i = 1 hasta n-1 y verificar si i y (n – i) no contienen K. Si no contiene ningún dígito, imprima los dos números y salga del ciclo. 

Por ejemplo: N = 11, k = 0
i = 1, N – 1 = 10 pero 10 contiene 0, entonces incremente i
i = 2, N – 1 = 9, entonces imprima esta i y n – i.

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program for
// the above approach
#include<bits/stdc++.h>
using namespace std;
 
int freqCount(string str, char k)
{
    int count = 0;
    for(int i = 0;
            i < str.size(); i++)
    {
        if (str[i] == k)
        count++;
    }
    return count;
}
 
// Function to find two
// numbers whose sum
// is N and do not
// contain any digit as k
void findAandB(int n, int k)
{
    int flag = 0;
     
    // Check every number i and (n-i)
    for(int i = 1; i < n; i++)
    {
        // Check if i and n-i doesn't
        // contain k in them print i and n-i
        if (freqCount(to_string(i),
                     (char)(k + 48)) == 0 and
            freqCount(to_string(n - i),
                     (char)(k + 48)) == 0)
        {
            cout << "(" << i << ", "
                 << n - i << ")";
            flag = 1;
            break;
        }
    }
     
    // Check if flag is 0
    // then print -1
    if (flag == 0)
        cout << -1;
}
 
// Driver Code
int main()
{
     
    // Given N and K
    int N = 100;
    int K = 0;
     
    // Function call
    findAandB(N, K);
    return 0;
}
 
// This code is contributed by Rajput-Ji

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
 
static int freqCount(String str, char k)
{
    int count = 0;
    for(int i = 0; i < str.length(); i++)
    {
        if (str.charAt(i) == k)
            count++;
    }
    return count;
}
 
// Function to find two numbers
// whose sum is N and do not
// contain any digit as k
static void findAandB(int n, int k)
{
    int flag = 0;
 
    // Check every number i and (n-i)
    for(int i = 1; i < n; i++)
    {
         
        // Check if i and n-i doesn't
        // contain k in them print i and n-i
        if (freqCount(Integer.toString(i),
                     (char)(k + 48)) == 0 &&
            freqCount(Integer.toString(n - i),
                     (char)(k + 48)) == 0)
        {
            System.out.print("(" + i + ", " +
                              (n - i) + ")");
            flag = 1;
            break;
        }
    }
 
    // Check if flag is 0
    // then print -1
    if (flag == 0)
        System.out.print(-1);
}
 
// Driver code
public static void main(String[] args)
{
 
    // Given N and K
    int N = 100;
    int K = 0;
 
    // Function call
    findAandB(N, K);
}
}
 
// This code is contributed by offbeat

Python

# Python program for the above approach
 
# Function to find two numbers whose sum
# is N and do not contain any digit as k
def findAandB(n, k):
   
    flag = 0
 
    # Check every number i and (n-i)
    for i in range(1, n):
 
        # Check if i and n-i doesn't
        # contain k in them print i and n-i
        if str(i).count(chr(k + 48)) == 0 \
        and str(n-i).count(chr(k + 48)) == 0:
            print(i, n-i)
            flag = 1
            break
 
    # check if flag is 0 then print -1
    if(flag == 0):
        print(-1)
 
# Driver Code
if __name__ == '__main__':
   
  # Given N and K
    N = 100
    K = 0
     
    # Function Call
    findAandB(N, K)

C#

// C# program for the
// above approach
using System;
class GFG{
 
static int freqCount(String str,
                     char k)
{
  int count = 0;
   
  for(int i = 0; i < str.Length; i++)
  {
    if (str[i] == k)
      count++;
  }
   
  return count;
}
 
// Function to find two numbers
// whose sum is N and do not
// contain any digit as k
static void findAandB(int n, int k)
{
  int flag = 0;
 
  // Check every number i and (n-i)
  for(int i = 1; i < n; i++)
  {
    // Check if i and n-i doesn't
    // contain k in them print i and n-i
    if (freqCount(i.ToString(),
                 (char)(k + 48)) == 0 &&
        freqCount((n-i).ToString(),
                  (char)(k + 48)) == 0)
    {
      Console.Write("(" + i + ", " +
                    (n - i) + ")");
      flag = 1;
      break;
    }
  }
 
  // Check if flag is 0
  // then print -1
  if (flag == 0)
    Console.Write(-1);
}
 
// Driver code
public static void Main(String[] args)
{
  // Given N and K
  int N = 100;
  int K = 0;
 
  // Function call
  findAandB(N, K);
}
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
 
// Javascript program for
// the above approach
 
function freqCount(str, k)
{
    var count = 0;
    for(var i = 0;
            i < str.length; i++)
    {
        if (str[i] == k)
        count++;
    }
    return count;
}
 
// Function to find two
// numbers whose sum
// is N and do not
// contain any digit as k
function findAandB(n, k)
{
    var flag = 0;
     
    // Check every number i and (n-i)
    for(var i = 1; i < n; i++)
    {
        // Check if i and n-i doesn't
        // contain k in them print i and n-i
        if (freqCount(i.toString(),
                     String.fromCharCode(k + 48)) == 0 &&
            freqCount((n - i).toString(),
                     String.fromCharCode(k + 48)) == 0)
        {
            document.write( "(" + i + ", "
                 + (n - i) + ")");
            flag = 1;
            break;
        }
    }
     
    // Check if flag is 0
    // then print -1
    if (flag == 0)
        cout + -1;
}
 
// Driver Code
// Given N and K
var N = 100;
var K = 0;
 
// Function call
findAandB(N, K);
 
// This code is contributed by rrrtnx.
</script>
Producción: 

(1, 99)

 

Complejidad temporal: O(N)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por spp____ y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *