Generate all n digit numbers whose consecutive even and odd digit
The given problem is to generate all n-digit numbers where consecutive digits are alternatingly even and odd. In other words, we need to find all numbers such that the first digit is even, the second digit is odd, the third digit is even, and so on.
Problem Statement
We are given an integer n, and we need to generate all possible n-digit numbers that satisfy the condition mentioned above. For example, if n is 2, the valid numbers would be 21, 23, 25, and so on. If n is 3, the valid numbers would be 210, 212, 214, and so on.
The algorithm should be able to handle any positive integer n and generate all possible combinations efficiently.
Algorithm
To solve this problem, we can use a recursive approach. The algorithm can be outlined as follows:
- Define a function
findCombination
that takes an arrayresult
, an indexindex
, and the value ofn
as parameters. - Inside the
findCombination
function, check if the index is equal ton
. If it is, we have found a valid combination, so we call a helper functionprintSequence
to display the result. - If the index is greater than or equal to
n
, we have exceeded the number of digits, so we return from the function. - Loop through the digits from 0 to 9.
- If the index is even, check if the digit is even and not zero. If the condition is satisfied, assign the digit to the
result
array at the current index, and recursively callfindCombination
withindex+1
. - If the index is odd, check if the digit is odd. If the condition is satisfied, assign the digit to the
result
array at the current index, and recursively callfindCombination
withindex+1
. - Implement a helper function
printSequence
that takes theresult
array and the value ofk
(which represents the current index) as parameters. This function prints the digits in theresult
array. - Finally, implement a function
combination
that takes the value ofn
as a parameter. This function checks ifn
is valid (greater than 0) and then calls thefindCombination
function with initial values.
Pseudocode
function findCombination(result, index, n): if index == n: printSequence(result, index) if index >= n: return for i = 0 to 9: if index is even: if i is even and not (index is 0 and i is 0): result[index] = i findCombination(result, index + 1, n) else: if i is odd: result[index] = i findCombination(result, index + 1, n) function printSequence(result, k): for i = 0 to k: print result[i] function combination(n): if n <= 0: return print "Given n:", n result = array of size n findCombination(result, 0, n)
Program Solution
/*
C program for
Generate all n digit numbers whose consecutive even and odd digit
*/
#include <stdio.h>
// Display result
void printSequence(int result[], int k)
{
for (int i = 0; i < k; ++i)
{
printf("%d", result[i]);
}
printf(" ");
}
void findCombination(int result[], int index, int n)
{
if (index == n )
{
// Display calculated result
printSequence(result, index);
}
if(index >= n)
{
return;
}
for (int i = 0; i <= 9; ++i)
{
if(index%2==0 )
{
// Test condition
// ➀ number is even
// ➁ number is not start with zero
// Condition true then enter
if( i % 2 == 0 && !(index==0 && i == 0))
{
result[index] = i;
findCombination(result,index+1,n);
}
}
else
{
// Check number is Odd
if( (i % 2) != 0 )
{
result[index] = i;
findCombination(result,index+1,n);
}
}
}
}
// Handles the request of find combination of given n
void combination(int n)
{
if (n <= 0)
{
return;
}
printf("\n Given n : %d \n ", n);
// Use to collect result
int result[n];
// Test
findCombination(result, 0, n);
}
int main(int argc, char const *argv[])
{
// Test
combination(2);
combination(3);
return 0;
}
Output
Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898
// Java Program for
// Generate all n digit numbers whose consecutive even and odd digit
public class EvenOddCombination
{
// Display result
public void printSequence(int[] result, int k)
{
for (int i = 0; i < k; ++i)
{
System.out.print(result[i]);
}
System.out.print(" ");
}
public void findCombination(int[] result, int index, int n)
{
if (index == n)
{
// Display calculated result
printSequence(result, index);
}
if (index >= n)
{
return;
}
for (int i = 0; i <= 9; ++i)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// ➁ number is not start with zero
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result[index] = i;
findCombination(result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2) != 0)
{
result[index] = i;
findCombination(result, index + 1, n);
}
}
}
}
// Handles the request of find combination of given n
public void combination(int n)
{
if (n <= 0)
{
return;
}
System.out.println("\n Given n : " + n );
// Use to collect result
int[] result = new int[n];
// Test
findCombination(result, 0, n);
}
public static void main(String[] args)
{
EvenOddCombination task = new EvenOddCombination();
// Test
task.combination(2);
task.combination(3);
}
}
Output
Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898
// Include header file
#include <iostream>
using namespace std;
// C++ Program for
// Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination
{
public:
// Display result
void printSequence(int result[], int k)
{
for (int i = 0; i < k; ++i)
{
cout << result[i];
}
cout << " ";
}
void findCombination(int result[], int index, int n)
{
if (index == n)
{
// Display calculated result
this->printSequence(result, index);
}
if (index >= n)
{
return;
}
for (int i = 0; i <= 9; ++i)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// ➁ number is not start with zero
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result[index] = i;
this->findCombination(result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2) != 0)
{
result[index] = i;
this->findCombination(result, index + 1, n);
}
}
}
}
// Handles the request of find combination of given n
void combination(int n)
{
if (n <= 0)
{
return;
}
cout << "\n Given n : " << n << endl;
// Use to collect result
int result[n];
// Test
this->findCombination(result, 0, n);
}
};
int main()
{
EvenOddCombination *task = new EvenOddCombination();
// Test
task->combination(2);
task->combination(3);
return 0;
}
Output
Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898
package main
import "fmt"
// Go Program for
// Generate all n digit numbers whose consecutive even and odd digit
type EvenOddCombination struct {}
func getEvenOddCombination() * EvenOddCombination {
var me *EvenOddCombination = &EvenOddCombination {}
return me
}
// Display result
func(this EvenOddCombination) printSequence(result[] int, k int) {
for i := 0 ; i < k ; i++ {
fmt.Print(result[i])
}
fmt.Print(" ")
}
func(this EvenOddCombination) findCombination(result[] int,
index int, n int) {
if index == n {
// Display calculated result
this.printSequence(result, index)
}
if index >= n {
return
}
for i := 0 ; i <= 9 ; i++ {
if index % 2 == 0 {
// Test condition
// ➀ number is even
// ➁ number is not start with zero
// Condition true then enter
if i % 2 == 0 && !(index == 0 && i == 0) {
result[index] = i
this.findCombination(result, index + 1, n)
}
} else {
// Check number is Odd
if (i % 2) != 0 {
result[index] = i
this.findCombination(result, index + 1, n)
}
}
}
}
// Handles the request of find combination of given n
func(this EvenOddCombination) combination(n int) {
if n <= 0 {
return
}
fmt.Println("\n Given n : ", n)
// Use to collect result
var result = make([] int, n)
// Test
this.findCombination(result, 0, n)
}
func main() {
var task * EvenOddCombination = getEvenOddCombination()
// Test
task.combination(2)
task.combination(3)
}
Output
Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898
// Include namespace system
using System;
// Csharp Program for
// Generate all n digit numbers whose consecutive even and odd digit
public class EvenOddCombination
{
// Display result
public void printSequence(int[] result, int k)
{
for (int i = 0; i < k; ++i)
{
Console.Write(result[i]);
}
Console.Write(" ");
}
public void findCombination(int[] result, int index, int n)
{
if (index == n)
{
// Display calculated result
this.printSequence(result, index);
}
if (index >= n)
{
return;
}
for (int i = 0; i <= 9; ++i)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// ➁ number is not start with zero
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2) != 0)
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
}
}
// Handles the request of find combination of given n
public void combination(int n)
{
if (n <= 0)
{
return;
}
Console.WriteLine("\n Given n : " + n);
// Use to collect result
int[] result = new int[n];
// Test
this.findCombination(result, 0, n);
}
public static void Main(String[] args)
{
EvenOddCombination task = new EvenOddCombination();
// Test
task.combination(2);
task.combination(3);
}
}
Output
Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898
<?php
// Php Program for
// Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination
{
// Display result
public function printSequence($result, $k)
{
for ($i = 0; $i < $k; ++$i)
{
echo($result[$i]);
}
echo(" ");
}
public function findCombination($result, $index, $n)
{
if ($index == $n)
{
// Display calculated result
$this->printSequence($result, $index);
}
if ($index >= $n)
{
return;
}
for ($i = 0; $i <= 9; ++$i)
{
if ($index % 2 == 0)
{
// Test condition
// ➀ number is even
// ➁ number is not start with zero
// Condition true then enter
if ($i % 2 == 0 && !($index == 0 && $i == 0))
{
$result[$index] = $i;
$this->findCombination($result, $index + 1, $n);
}
}
else
{
// Check number is Odd
if (($i % 2) != 0)
{
$result[$index] = $i;
$this->findCombination($result, $index + 1, $n);
}
}
}
}
// Handles the request of find combination of given n
public function combination($n)
{
if ($n <= 0)
{
return;
}
echo("\n Given n : ".$n.
"\n");
// Use to collect result
$result = array_fill(0, $n, 0);
// Test
$this->findCombination($result, 0, $n);
}
}
function main()
{
$task = new EvenOddCombination();
// Test
$task->combination(2);
$task->combination(3);
}
main();
Output
Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898
// Node JS Program for
// Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination
{
// Display result
printSequence(result, k)
{
for (var i = 0; i < k; ++i)
{
process.stdout.write("" + result[i]);
}
process.stdout.write(" ");
}
findCombination(result, index, n)
{
if (index == n)
{
// Display calculated result
this.printSequence(result, index);
}
if (index >= n)
{
return;
}
for (var i = 0; i <= 9; ++i)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// ➁ number is not start with zero
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2) != 0)
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
}
}
// Handles the request of find combination of given n
combination(n)
{
if (n <= 0)
{
return;
}
console.log("\n Given n : " + n);
// Use to collect result
var result = Array(n).fill(0);
// Test
this.findCombination(result, 0, n);
}
}
function main()
{
var task = new EvenOddCombination();
// Test
task.combination(2);
task.combination(3);
}
main();
Output
Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898
# Python 3 Program for
# Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination :
# Display result
def printSequence(self, result, k) :
i = 0
while (i < k) :
print(result[i], end = "")
i += 1
print(" ", end = "")
def findCombination(self, result, index, n) :
if (index == n) :
# Display calculated result
self.printSequence(result, index)
if (index >= n) :
return
i = 0
while (i <= 9) :
if (index % 2 == 0) :
# Test condition
# ➀ number is even
# ➁ number is not start with zero
# Condition true then enter
if (i % 2 == 0 and not(index == 0 and i == 0)) :
result[index] = i
self.findCombination(result, index + 1, n)
else :
# Check number is Odd
if ((i % 2) != 0) :
result[index] = i
self.findCombination(result, index + 1, n)
i += 1
# Handles the request of find combination of given n
def combination(self, n) :
if (n <= 0) :
return
print("\n Given n : ", n)
# Use to collect result
result = [0] * (n)
# Test
self.findCombination(result, 0, n)
def main() :
task = EvenOddCombination()
# Test
task.combination(2)
task.combination(3)
if __name__ == "__main__": main()
Output
Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898
# Ruby Program for
# Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination
# Display result
def printSequence(result, k)
i = 0
while (i < k)
print(result[i])
i += 1
end
print(" ")
end
def findCombination(result, index, n)
if (index == n)
# Display calculated result
self.printSequence(result, index)
end
if (index >= n)
return
end
i = 0
while (i <= 9)
if (index % 2 == 0)
# Test condition
# ➀ number is even
# ➁ number is not start with zero
# Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
result[index] = i
self.findCombination(result, index + 1, n)
end
else
# Check number is Odd
if ((i % 2) != 0)
result[index] = i
self.findCombination(result, index + 1, n)
end
end
i += 1
end
end
# Handles the request of find combination of given n
def combination(n)
if (n <= 0)
return
end
print("\n Given n : ", n, "\n")
# Use to collect result
result = Array.new(n) {0}
# Test
self.findCombination(result, 0, n)
end
end
def main()
task = EvenOddCombination.new()
# Test
task.combination(2)
task.combination(3)
end
main()
Output
Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898
// Scala Program for
// Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination()
{
// Display result
def printSequence(result: Array[Int], k: Int): Unit = {
var i: Int = 0;
while (i < k)
{
print(result(i));
i += 1;
}
print(" ");
}
def findCombination(result: Array[Int], index: Int, n: Int): Unit = {
if (index == n)
{
// Display calculated result
printSequence(result, index);
}
if (index >= n)
{
return;
}
var i: Int = 0;
while (i <= 9)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// ➁ number is not start with zero
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result(index) = i;
findCombination(result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2) != 0)
{
result(index) = i;
findCombination(result, index + 1, n);
}
}
i += 1;
}
}
// Handles the request of find combination of given n
def combination(n: Int): Unit = {
if (n <= 0)
{
return;
}
println("\n Given n : " + n);
// Use to collect result
var result: Array[Int] = Array.fill[Int](n)(0);
// Test
findCombination(result, 0, n);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: EvenOddCombination = new EvenOddCombination();
// Test
task.combination(2);
task.combination(3);
}
}
Output
Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898
// Swift 4 Program for
// Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination
{
// Display result
func printSequence(_ result: [Int], _ k: Int)
{
var i: Int = 0;
while (i < k)
{
print(result[i], terminator: "");
i += 1;
}
print(" ", terminator: "");
}
func findCombination(_ result: inout[Int], _ index: Int, _ n: Int)
{
if (index == n)
{
// Display calculated result
self.printSequence(result, index);
}
if (index >= n)
{
return;
}
var i: Int = 0;
while (i <= 9)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// ➁ number is not start with zero
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result[index] = i;
self.findCombination(&result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2) != 0)
{
result[index] = i;
self.findCombination(&result, index + 1, n);
}
}
i += 1;
}
}
// Handles the request of find combination of given n
func combination(_ n: Int)
{
if (n <= 0)
{
return;
}
print("\n Given n : ", n);
// Use to collect result
var result: [Int] = Array(repeating: 0, count: n);
// Test
self.findCombination(&result, 0, n);
}
}
func main()
{
let task: EvenOddCombination = EvenOddCombination();
// Test
task.combination(2);
task.combination(3);
}
main();
Output
Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898
// Kotlin Program for
// Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination
{
// Display result
fun printSequence(result: Array < Int > , k: Int): Unit
{
var i: Int = 0;
while (i < k)
{
print(result[i]);
i += 1;
}
print(" ");
}
fun findCombination(result: Array < Int > , index: Int, n: Int): Unit
{
if (index == n)
{
// Display calculated result
this.printSequence(result, index);
}
if (index >= n)
{
return;
}
var i: Int = 0;
while (i <= 9)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// ➁ number is not start with zero
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2) != 0)
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
i += 1;
}
}
// Handles the request of find combination of given n
fun combination(n: Int): Unit
{
if (n <= 0)
{
return;
}
println("\n Given n : " + n);
// Use to collect result
val result: Array < Int > = Array(n)
{
0
};
// Test
this.findCombination(result, 0, n);
}
}
fun main(args: Array < String > ): Unit
{
val task: EvenOddCombination = EvenOddCombination();
// Test
task.combination(2);
task.combination(3);
}
Output
Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898
Output Explanation
Let's consider the given code example:
combination(2) combination(3)
For the first call, combination(2)
, the output is:
Given n: 2 21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
This output represents all possible 2-digit numbers where consecutive digits are alternatingly even and odd.
For the second call, combination(3)
, the output is:
Given n: 3 210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898
This output represents all possible 3-digit numbers where consecutive digits are alternatingly even and odd.
Time Complexity
The time complexity of this algorithm is O(5n), where n is the number of digits. This is because at each digit, we have a maximum of 5 possibilities (2 even digits and 3 odd digits). Since there are n digits, the total number of combinations would be 5n.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment