Imprima n 0 y m 1 de manera que no haya dos 0 ni tres 1 juntos

Dados dos enteros n y m donde n es el número de 0s ym es el número de 1s . La tarea es imprimir todos los 0 y 1 en una sola fila de modo que no haya dos 0 juntos ni tres 1 juntos. Si no es posible organizar 0 y 1 de acuerdo con la condición, imprima -1 . Ejemplos:
 
 

Entrada: n = 1, m = 2 
Salida: 011
Entrada: n = 4, m = 8 
Salida: 110110110101 
 

Enfoque: Solo tenemos respuestas cuando, ( (n – 1) ≤ m y m ≤ 2 * (n + 1)
 

  • Si (m == n – 1) entonces imprima el patrón 010101… comenzando desde 0 hasta que se hayan usado todos los 0 y 1 .
  • Si (m > n) y m ≤ 2 * (n + 1) imprima el patrón 110110110… hasta que haya un exceso de 1 y cambie al patrón 0101010… cuando m sea igual a n – 1 .

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

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the required pattern
void printPattern(int n, int m)
{
 
    // When condition fails
    if (m > 2 * (n + 1) || m < n - 1) {
        cout << "-1";
    }
 
    // When m = n - 1
    else if (abs(n - m) <= 1) {
        while (n > 0 && m > 0) {
            cout << "01";
            n--;
            m--;
        }
        if (n != 0) {
            cout << "0";
        }
        if (m != 0) {
            cout << "1";
        }
    }
    else {
        while (m - n > 1 && n > 0) {
            cout << "110";
            m = m - 2;
            n = n - 1;
        }
        while (n > 0) {
            cout << "10";
            n--;
            m--;
        }
        while (m > 0) {
            cout << "1";
            m--;
        }
    }
}
 
// Driver program
int main()
{
    int n = 4, m = 8;
    printPattern(n, m);
    return 0;
}

Java

// Java implementation of the above approach
class GFG
{
    // Function to print the required pattern
    static void printPattern(int n, int m)
    {
        // When condition fails
        if (m > 2 * (n + 1) || m < n - 1)
        {
            System.out.print("-1");
        }
         
        // When m = n - 1
        else if (Math.abs(n - m) <= 1)
        {
            while (n > 0 && m > 0)
            {
                System.out.print("01");
                n--;
                m--;
                 
            }
            if (n != 0)
            {
                System.out.print("0");
            }
            if (m != 0)
            {
                System.out.print("1");
            }
        }
        else
        {
            while (m - n > 1 && n > 0)
            {
                System.out.print("110");
                m = m - 2;
                n = n - 1;
            }
            while (n > 0)
            {
                System.out.print("10");
                n--;
                m--;
            }
            while (m > 0)
            {
                System.out.print("1");
                m--;
            }
        }
    }
 
    // Driver code
    public static void main(String []args)
    {
        int n = 4, m = 8;
        printPattern(n, m);
    }
}
 
// This code is contributed by Ita_c.

Python3

# Python 3 implementation of the approach
 
# Function to print the required pattern
def printPattern(n, m):
     
    # When condition fails
    if (m > 2 * (n + 1) or m < n - 1):
        print("-1", end = "")
 
    # When m = n - 1
    elif (abs(n - m) <= 1):
        while (n > 0 and m > 0):
            print("01", end = "");
            n -= 1
            m -= 1
         
        if (n != 0):
            print("0", end = "")
        if (m != 0):
            print("1", end = "")
    else:
        while (m - n > 1 and n > 0):
            print("110", end = "")
            m = m - 2
            n = n - 1
         
        while (n > 0):
            print("10", end = "")
            n -= 1
            m -= 1
         
        while (m > 0):
            print("1", end = "")
            m -= 1
     
# Driver Code
if __name__ == '__main__':
    n = 4
    m = 8
    printPattern(n, m)
 
# This code is contributed by
# Surendra_Gangwar

C#

// C# implementation of the above approach
using System;
 
class GFG
{
    // Function to print the required pattern
    static void printPattern(int n, int m)
    {
        // When condition fails
        if (m > 2 * (n + 1) || m < n - 1)
        {
            Console.Write("-1");
        }
        // When m = n - 1
        else if (Math.Abs(n - m) <= 1)
        {
            while (n > 0 && m > 0)
            {
                Console.Write("01");
                n--;
                m--;
                 
            }
            if (n != 0)
            {
                Console.Write("0");
            }
            if (m != 0)
            {
                Console.Write("1");
            }
        }
        else
        {
            while (m - n > 1 && n > 0) 
            {
                Console.Write("110");
                m = m - 2;
                n = n - 1;
            }
            while (n > 0)
            {
                Console.Write("10");
                n--;
                m--;
            }
            while (m > 0)
            {
                Console.Write("1");
                m--;
            }
        }
    }
 
    // Driver code
    public static void Main()
    {
        int n = 4, m = 8;
        printPattern(n, m);
    }
}
 
// This code is contributed by Ryuga

PHP

<?php
// PHP implementation of the above approach
 
// Function to print the required pattern
function printPattern($n, $m)
{
    // When condition fails
    if ($m > 2 * ($n + 1) || $m < $n - 1)
    {
        echo("-1");
    }
     
    // When m = n - 1
    else if (abs($n - $m) <= 1)
    {
        while ($n > 0 && $m > 0)
        {
            System.out.print("01");
            $n--;
            $m--;
             
        }
        if ($n != 0)
        {
            echo("0");
        }
        if ($m != 0)
        {
            echo("1");
        }
    }
    else
    {
        while ($m - $n > 1 && $n > 0)
        {
            echo("110");
            $m = $m - 2;
            $n = $n - 1;
        }
        while ($n > 0)
        {
            echo("10");
            $n--;
            $m--;
        }
        while ($m > 0)
        {
            echo("1");
            $m--;
        }
    }
}
 
// Driver code
$n = 4; $m = 8;    
printPattern($n, $m);
 
// This code is contributed by
// Mukul Singh.
?>

Javascript

<script>
 
// JavaScript implementation of the above approach
 
// Function to print the required pattern
function printPattern( n,  m)
{
 
    // When condition fails
    if (m > 2 * (n + 1) || m < n - 1) {
        document.write("-1");
    }
 
    // When m = n - 1
    else if (Math.abs(n - m) <= 1) {
        while (n > 0 && m > 0) {
            document.write("01");
            n--;
            m--;
        }
        if (n != 0) {
            document.write("0");
        }
        if (m != 0) {
            document.write("1");
        }
    }
    else {
        while (m - n > 1 && n > 0) {
            document.write("110");
            m = m - 2;
            n = n - 1;
        }
        while (n > 0) {
            document.write("10");
            n--;
            m--;
        }
        while (m > 0) {
            document.write("1");
            m--;
        }
    }
}
 
var n = 4, m = 8;
    printPattern(n, m);
 
</script>
Producción

110110110101

Publicación traducida automáticamente

Artículo escrito por himanshu_rathore 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 *