Print all n-digit numbers with sum of even and odd position digits whose absolute difference are one
Here given code implementation process.
/*
C program for
Print all n-digit numbers with sum of even and odd position
digits whose absolute difference are one.
*/
#include <stdio.h>
// Display result
void printSequence(int result[], int k)
{
for (int i = 0; i < k; ++i)
{
printf("%d", result[i]);
}
printf(" ");
}
int absValue(int x)
{
if (x < 0)
{
return -x;
}
return x;
}
void findCombination(int result[],
int index, int even, int odd, int n)
{
if (index == n && absValue(even - odd) == 1)
{
// Display calculated result
printSequence(result, index);
}
if (index >= n)
{
return;
}
for (int i = 0; i <= 9; ++i)
{
result[index] = i;
if (!(index == 0 && i == 0))
{
if (index % 2 == 0)
{
// When positions are Even
findCombination(result, index + 1, even + i, odd, n);
}
else
{
// When positions are Odd
findCombination(result, index + 1, even, odd + i, n);
}
}
}
}
// Handles the request of find combination of given n
void combination(int n)
{
if (n <= 0)
{
return;
}
// N is length
printf("\n Given n : %d \n ", n);
// Collect result
int result[n];
// Test
findCombination(result, 0, 0, 0, n);
}
int main(int argc, char const *argv[])
{
// n = 2
// 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
// Here
// Value Digit Difference
// 10 [1,0] = 1
// 12 [1,2] = 1
// etc
combination(2);
// n = 3
/*
100 111 120 122 131 133 142 144 153 155 164 166
175 177 186 188 197 199 210 221 230 232 241 243
252 254 263 265 274 276 285 287 296 298 320 331
340 342 351 353 362 364 373 375 384 386 395 397
430 441 450 452 461 463 472 474 483 485 494 496
540 551 560 562 571 573 582 584 593 595 650 661
670 672 681 683 692 694 760 771 780 782 791 793
870 881 890 892 980 991
*/
// suppose num = 351
// 5 = 5
// Even Position ↑
// 3 5 1
// Odd Position ↓ ↓
// 3 + 1 = 4
// ----------------------------------
// 5 - 4 = 1 Difference is 1
combination(3);
return 0;
}
Output
Given n : 2
10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
Given n : 3
100 111 120 122 131 133 142 144 153 155 164 166 175 177 186 188 197 199 210 221 230 232 241 243 252 254 263 265 274 276 285 287 296 298 320 331 340 342 351 353 362 364 373 375 384 386 395 397 430 441 450 452 461 463 472 474 483 485 494 496 540 551 560 562 571 573 582 584 593 595 650 661 670 672 681 683 692 694 760 771 780 782 791 793 870 881 890 892 980 991
/*
Java Program
Print all n-digit numbers with sum of even and odd position
digits whose absolute difference are one.
*/
public class Combinations
{
// 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 int absValue(int x)
{
if (x < 0)
{
return -x;
}
return x;
}
public void findCombination(int[] result,
int index,
int even,
int odd,
int n)
{
if (index == n && absValue(even - odd) == 1)
{
// Display calculated result
printSequence(result, index);
}
if (index >= n)
{
return;
}
for (int i = 0; i <= 9; ++i)
{
result[index] = i;
if (!(index == 0 && i == 0))
{
if (index % 2 == 0)
{
// When positions are Even
findCombination(result, index + 1, even + i, odd, n);
}
else
{
// When positions are Odd
findCombination(result, index + 1, even, odd + i, n);
}
}
}
}
// Handles the request of find combination of given n
public void combination(int n)
{
if (n <= 0)
{
return;
}
// N is length
System.out.println("\n Given n : " + n);
// Collect result
int[] result = new int[n];
// Test
findCombination(result, 0, 0, 0, n);
}
public static void main(String[] args)
{
Combinations task = new Combinations();
// n = 2
// 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
// Here
// Value Digit Difference
// 10 [1,0] = 1
// 12 [1,2] = 1
// etc
task.combination(2);
/*
n = 3
100 111 120 122 131 133 142 144 153 155 164 166
175 177 186 188 197 199 210 221 230 232 241 243
252 254 263 265 274 276 285 287 296 298 320 331
340 342 351 353 362 364 373 375 384 386 395 397
430 441 450 452 461 463 472 474 483 485 494 496
540 551 560 562 571 573 582 584 593 595 650 661
670 672 681 683 692 694 760 771 780 782 791 793
870 881 890 892 980 991
*/
// suppose num = 351
// 5 = 5
// Even Position ↑
// 3 5 1
// Odd Position ↓ ↓
// 3 + 1 = 4
// ----------------------------------
// 5 - 4 = 1 Difference is 1
task.combination(3);
}
}
Output
Given n : 2
10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
Given n : 3
100 111 120 122 131 133 142 144 153 155 164 166 175 177 186 188 197 199 210 221 230 232 241 243 252 254 263 265 274 276 285 287 296 298 320 331 340 342 351 353 362 364 373 375 384 386 395 397 430 441 450 452 461 463 472 474 483 485 494 496 540 551 560 562 571 573 582 584 593 595 650 661 670 672 681 683 692 694 760 771 780 782 791 793 870 881 890 892 980 991
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Print all n-digit numbers with sum of even and odd position
digits whose absolute difference are one.
*/
class Combinations
{
public:
// Display result
void printSequence(int result[], int k)
{
for (int i = 0; i < k; ++i)
{
cout << result[i];
}
cout << " ";
}
int absValue(int x)
{
if (x < 0)
{
return -x;
}
return x;
}
void findCombination(int result[], int index, int even, int odd, int n)
{
if (index == n && this->absValue(even - odd) == 1)
{
// Display calculated result
this->printSequence(result, index);
}
if (index >= n)
{
return;
}
for (int i = 0; i <= 9; ++i)
{
result[index] = i;
if (!(index == 0 && i == 0))
{
if (index % 2 == 0)
{
// When positions are Even
this->findCombination(result, index + 1, even + i, odd, n);
}
else
{
// When positions are Odd
this->findCombination(result, index + 1, even, odd + i, n);
}
}
}
}
// Handles the request of find combination of given n
void combination(int n)
{
if (n <= 0)
{
return;
}
// N is length
cout << "\n Given n : " << n << endl;
// Collect result
int result[n];
// Test
this->findCombination(result, 0, 0, 0, n);
}
};
int main()
{
Combinations *task = new Combinations();
// n = 2
// 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
// Here
// Value Digit Difference
// 10 [1,0] = 1
// 12 [1,2] = 1
// etc
task->combination(2);
/*
n = 3
100 111 120 122 131 133 142 144 153 155 164 166
175 177 186 188 197 199 210 221 230 232 241 243
252 254 263 265 274 276 285 287 296 298 320 331
340 342 351 353 362 364 373 375 384 386 395 397
430 441 450 452 461 463 472 474 483 485 494 496
540 551 560 562 571 573 582 584 593 595 650 661
670 672 681 683 692 694 760 771 780 782 791 793
870 881 890 892 980 991
*/
// suppose num = 351
// 5 = 5
// Even Position ↑
// 3 5 1
// Odd Position ↓ ↓
// 3 + 1 = 4
// ----------------------------------
// 5 - 4 = 1 Difference is 1
task->combination(3);
return 0;
}
Output
Given n : 2
10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
Given n : 3
100 111 120 122 131 133 142 144 153 155 164 166 175 177 186 188 197 199 210 221 230 232 241 243 252 254 263 265 274 276 285 287 296 298 320 331 340 342 351 353 362 364 373 375 384 386 395 397 430 441 450 452 461 463 472 474 483 485 494 496 540 551 560 562 571 573 582 584 593 595 650 661 670 672 681 683 692 694 760 771 780 782 791 793 870 881 890 892 980 991
package main
import "fmt"
/*
Go Program
Print all n-digit numbers with sum of even and odd position
digits whose absolute difference are one.
*/
type Combinations struct {}
func getCombinations() * Combinations {
var me *Combinations = &Combinations {}
return me
}
// Display result
func(this Combinations) printSequence(result[] int, k int) {
for i := 0 ; i < k ; i++ {
fmt.Print(result[i])
}
fmt.Print(" ")
}
func(this Combinations) absValue(x int) int {
if x < 0 {
return -x
}
return x
}
func(this Combinations) findCombination(result[] int,
index int, even int,
odd int, n int) {
if index == n && this.absValue(even - odd) == 1 {
// Display calculated result
this.printSequence(result, index)
}
if index >= n {
return
}
for i := 0 ; i <= 9 ; i++ {
result[index] = i
if !(index == 0 && i == 0) {
if index % 2 == 0 {
// When positions are Even
this.findCombination(result, index + 1, even + i, odd, n)
} else {
// When positions are Odd
this.findCombination(result, index + 1, even, odd + i, n)
}
}
}
}
// Handles the request of find combination of given n
func(this Combinations) combination(n int) {
if n <= 0 {
return
}
// N is length
fmt.Println("\n Given n : ", n)
// Collect result
var result = make([] int, n)
// Test
this.findCombination(result, 0, 0, 0, n)
}
func main() {
var task * Combinations = getCombinations()
// n = 2
// 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
// Here
// Value Digit Difference
// 10 [1,0] = 1
// 12 [1,2] = 1
// etc
task.combination(2)
/*
n = 3
100 111 120 122 131 133 142 144 153 155 164 166
175 177 186 188 197 199 210 221 230 232 241 243
252 254 263 265 274 276 285 287 296 298 320 331
340 342 351 353 362 364 373 375 384 386 395 397
430 441 450 452 461 463 472 474 483 485 494 496
540 551 560 562 571 573 582 584 593 595 650 661
670 672 681 683 692 694 760 771 780 782 791 793
870 881 890 892 980 991
*/
// suppose num = 351
// 5 = 5
// Even Position ↑
// 3 5 1
// Odd Position ↓ ↓
// 3 + 1 = 4
// ----------------------------------
// 5 - 4 = 1 Difference is 1
task.combination(3)
}
Output
Given n : 2
10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
Given n : 3
100 111 120 122 131 133 142 144 153 155 164 166 175 177 186 188 197 199 210 221 230 232 241 243 252 254 263 265 274 276 285 287 296 298 320 331 340 342 351 353 362 364 373 375 384 386 395 397 430 441 450 452 461 463 472 474 483 485 494 496 540 551 560 562 571 573 582 584 593 595 650 661 670 672 681 683 692 694 760 771 780 782 791 793 870 881 890 892 980 991
// Include namespace system
using System;
/*
Csharp Program
Print all n-digit numbers with sum of even and odd position
digits whose absolute difference are one.
*/
public class Combinations
{
// Display result
public void printSequence(int[] result, int k)
{
for (int i = 0; i < k; ++i)
{
Console.Write(result[i]);
}
Console.Write(" ");
}
public int absValue(int x)
{
if (x < 0)
{
return -x;
}
return x;
}
public void findCombination(int[] result,
int index,
int even,
int odd,
int n)
{
if (index == n && this.absValue(even - odd) == 1)
{
// Display calculated result
this.printSequence(result, index);
}
if (index >= n)
{
return;
}
for (int i = 0; i <= 9; ++i)
{
result[index] = i;
if (!(index == 0 && i == 0))
{
if (index % 2 == 0)
{
// When positions are Even
this.findCombination(result,
index + 1,
even + i,
odd,
n);
}
else
{
// When positions are Odd
this.findCombination(result,
index + 1,
even,
odd + i,
n);
}
}
}
}
// Handles the request of find combination of given n
public void combination(int n)
{
if (n <= 0)
{
return;
}
// N is length
Console.WriteLine("\n Given n : " + n);
// Collect result
int[] result = new int[n];
// Test
this.findCombination(result, 0, 0, 0, n);
}
public static void Main(String[] args)
{
Combinations task = new Combinations();
// n = 2
// 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
// Here
// Value Digit Difference
// 10 [1,0] = 1
// 12 [1,2] = 1
// etc
task.combination(2);
/*
n = 3
100 111 120 122 131 133 142 144 153 155 164 166
175 177 186 188 197 199 210 221 230 232 241 243
252 254 263 265 274 276 285 287 296 298 320 331
340 342 351 353 362 364 373 375 384 386 395 397
430 441 450 452 461 463 472 474 483 485 494 496
540 551 560 562 571 573 582 584 593 595 650 661
670 672 681 683 692 694 760 771 780 782 791 793
870 881 890 892 980 991
*/
// suppose num = 351
// 5 = 5
// Even Position ↑
// 3 5 1
// Odd Position ↓ ↓
// 3 + 1 = 4
// ----------------------------------
// 5 - 4 = 1 Difference is 1
task.combination(3);
}
}
Output
Given n : 2
10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
Given n : 3
100 111 120 122 131 133 142 144 153 155 164 166 175 177 186 188 197 199 210 221 230 232 241 243 252 254 263 265 274 276 285 287 296 298 320 331 340 342 351 353 362 364 373 375 384 386 395 397 430 441 450 452 461 463 472 474 483 485 494 496 540 551 560 562 571 573 582 584 593 595 650 661 670 672 681 683 692 694 760 771 780 782 791 793 870 881 890 892 980 991
<?php
/*
Php Program
Print all n-digit numbers with sum of even and odd position
digits whose absolute difference are one.
*/
class Combinations
{
// Display result
public function printSequence($result, $k)
{
for ($i = 0; $i < $k; ++$i)
{
echo($result[$i]);
}
echo(" ");
}
public function absValue($x)
{
if ($x < 0)
{
return -$x;
}
return $x;
}
public function findCombination($result, $index, $even, $odd, $n)
{
if ($index == $n && $this->absValue($even - $odd) == 1)
{
// Display calculated result
$this->printSequence($result, $index);
}
if ($index >= $n)
{
return;
}
for ($i = 0; $i <= 9; ++$i)
{
$result[$index] = $i;
if (!($index == 0 && $i == 0))
{
if ($index % 2 == 0)
{
// When positions are Even
$this->findCombination($result,
$index + 1,
$even + $i,
$odd, $n);
}
else
{
// When positions are Odd
$this->findCombination($result,
$index + 1,
$even,
$odd + $i, $n);
}
}
}
}
// Handles the request of find combination of given n
public function combination($n)
{
if ($n <= 0)
{
return;
}
// N is length
echo("\n Given n : ".$n.
"\n");
// Collect result
$result = array_fill(0, $n, 0);
// Test
$this->findCombination($result, 0, 0, 0, $n);
}
}
function main()
{
$task = new Combinations();
// n = 2
// 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
// Here
// Value Digit Difference
// 10 [1,0] = 1
// 12 [1,2] = 1
// etc
$task->combination(2);
/*
n = 3
100 111 120 122 131 133 142 144 153 155 164 166
175 177 186 188 197 199 210 221 230 232 241 243
252 254 263 265 274 276 285 287 296 298 320 331
340 342 351 353 362 364 373 375 384 386 395 397
430 441 450 452 461 463 472 474 483 485 494 496
540 551 560 562 571 573 582 584 593 595 650 661
670 672 681 683 692 694 760 771 780 782 791 793
870 881 890 892 980 991
*/
// suppose num = 351
// 5 = 5
// Even Position ↑
// 3 5 1
// Odd Position ↓ ↓
// 3 + 1 = 4
// ----------------------------------
// 5 - 4 = 1 Difference is 1
$task->combination(3);
}
main();
Output
Given n : 2
10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
Given n : 3
100 111 120 122 131 133 142 144 153 155 164 166 175 177 186 188 197 199 210 221 230 232 241 243 252 254 263 265 274 276 285 287 296 298 320 331 340 342 351 353 362 364 373 375 384 386 395 397 430 441 450 452 461 463 472 474 483 485 494 496 540 551 560 562 571 573 582 584 593 595 650 661 670 672 681 683 692 694 760 771 780 782 791 793 870 881 890 892 980 991
/*
Node JS Program
Print all n-digit numbers with sum of even and odd position
digits whose absolute difference are one.
*/
class Combinations
{
// Display result
printSequence(result, k)
{
for (var i = 0; i < k; ++i)
{
process.stdout.write("" + result[i]);
}
process.stdout.write(" ");
}
absValue(x)
{
if (x < 0)
{
return -x;
}
return x;
}
findCombination(result, index, even, odd, n)
{
if (index == n && this.absValue(even - odd) == 1)
{
// Display calculated result
this.printSequence(result, index);
}
if (index >= n)
{
return;
}
for (var i = 0; i <= 9; ++i)
{
result[index] = i;
if (!(index == 0 && i == 0))
{
if (index % 2 == 0)
{
// When positions are Even
this.findCombination(result,
index + 1,
even + i,
odd, n);
}
else
{
// When positions are Odd
this.findCombination(result,
index + 1,
even, odd + i, n);
}
}
}
}
// Handles the request of find combination of given n
combination(n)
{
if (n <= 0)
{
return;
}
// N is length
console.log("\n Given n : " + n);
// Collect result
var result = Array(n).fill(0);
// Test
this.findCombination(result, 0, 0, 0, n);
}
}
function main()
{
var task = new Combinations();
// n = 2
// 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
// Here
// Value Digit Difference
// 10 [1,0] = 1
// 12 [1,2] = 1
// etc
task.combination(2);
/*
n = 3
100 111 120 122 131 133 142 144 153 155 164 166
175 177 186 188 197 199 210 221 230 232 241 243
252 254 263 265 274 276 285 287 296 298 320 331
340 342 351 353 362 364 373 375 384 386 395 397
430 441 450 452 461 463 472 474 483 485 494 496
540 551 560 562 571 573 582 584 593 595 650 661
670 672 681 683 692 694 760 771 780 782 791 793
870 881 890 892 980 991
*/
// suppose num = 351
// 5 = 5
// Even Position ↑
// 3 5 1
// Odd Position ↓ ↓
// 3 + 1 = 4
// ----------------------------------
// 5 - 4 = 1 Difference is 1
task.combination(3);
}
main();
Output
Given n : 2
10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
Given n : 3
100 111 120 122 131 133 142 144 153 155 164 166 175 177 186 188 197 199 210 221 230 232 241 243 252 254 263 265 274 276 285 287 296 298 320 331 340 342 351 353 362 364 373 375 384 386 395 397 430 441 450 452 461 463 472 474 483 485 494 496 540 551 560 562 571 573 582 584 593 595 650 661 670 672 681 683 692 694 760 771 780 782 791 793 870 881 890 892 980 991
# Python 3 Program
# Print all n-digit numbers with sum of even and odd position
# digits whose absolute difference are one.
class Combinations :
# Display result
def printSequence(self, result, k) :
i = 0
while (i < k) :
print(result[i], end = "")
i += 1
print(" ", end = "")
def absValue(self, x) :
if (x < 0) :
return -x
return x
def findCombination(self, result, index, even, odd, n) :
if (index == n and self.absValue(even - odd) == 1) :
# Display calculated result
self.printSequence(result, index)
if (index >= n) :
return
i = 0
while (i <= 9) :
result[index] = i
if (not(index == 0 and i == 0)) :
if (index % 2 == 0) :
# When positions are Even
self.findCombination(result, index + 1, even + i, odd, n)
else :
# When positions are Odd
self.findCombination(result, index + 1, even, odd + i, n)
i += 1
# Handles the request of find combination of given n
def combination(self, n) :
if (n <= 0) :
return
# N is length
print("\n Given n : ", n)
# Collect result
result = [0] * (n)
# Test
self.findCombination(result, 0, 0, 0, n)
def main() :
task = Combinations()
# n = 2
# 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
# Here
# Value Digit Difference
# 10 [1,0] = 1
# 12 [1,2] = 1
# etc
task.combination(2)
# n = 3
# 100 111 120 122 131 133 142 144 153 155 164 166
# 175 177 186 188 197 199 210 221 230 232 241 243
# 252 254 263 265 274 276 285 287 296 298 320 331
# 340 342 351 353 362 364 373 375 384 386 395 397
# 430 441 450 452 461 463 472 474 483 485 494 496
# 540 551 560 562 571 573 582 584 593 595 650 661
# 670 672 681 683 692 694 760 771 780 782 791 793
# 870 881 890 892 980 991
# suppose num = 351
# 5 = 5
# Even Position ↑
# 3 5 1
# Odd Position ↓ ↓
# 3 + 1 = 4
# ----------------------------------
# 5 - 4 = 1 Difference is 1
task.combination(3)
if __name__ == "__main__": main()
Output
Given n : 2
10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
Given n : 3
100 111 120 122 131 133 142 144 153 155 164 166 175 177 186 188 197 199 210 221 230 232 241 243 252 254 263 265 274 276 285 287 296 298 320 331 340 342 351 353 362 364 373 375 384 386 395 397 430 441 450 452 461 463 472 474 483 485 494 496 540 551 560 562 571 573 582 584 593 595 650 661 670 672 681 683 692 694 760 771 780 782 791 793 870 881 890 892 980 991
# Ruby Program
# Print all n-digit numbers with sum of even and odd position
# digits whose absolute difference are one.
class Combinations
# Display result
def printSequence(result, k)
i = 0
while (i < k)
print(result[i])
i += 1
end
print(" ")
end
def absValue(x)
if (x < 0)
return -x
end
return x
end
def findCombination(result, index, even, odd, n)
if (index == n && self.absValue(even - odd) == 1)
# Display calculated result
self.printSequence(result, index)
end
if (index >= n)
return
end
i = 0
while (i <= 9)
result[index] = i
if (!(index == 0 && i == 0))
if (index % 2 == 0)
# When positions are Even
self.findCombination(result,
index + 1,
even + i, odd, n)
else
# When positions are Odd
self.findCombination(result,
index + 1,
even, odd + i, n)
end
end
i += 1
end
end
# Handles the request of find combination of given n
def combination(n)
if (n <= 0)
return
end
# N is length
print("\n Given n : ", n, "\n")
# Collect result
result = Array.new(n) {0}
# Test
self.findCombination(result, 0, 0, 0, n)
end
end
def main()
task = Combinations.new()
# n = 2
# 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
# Here
# Value Digit Difference
# 10 [1,0] = 1
# 12 [1,2] = 1
# etc
task.combination(2)
# n = 3
# 100 111 120 122 131 133 142 144 153 155 164 166
# 175 177 186 188 197 199 210 221 230 232 241 243
# 252 254 263 265 274 276 285 287 296 298 320 331
# 340 342 351 353 362 364 373 375 384 386 395 397
# 430 441 450 452 461 463 472 474 483 485 494 496
# 540 551 560 562 571 573 582 584 593 595 650 661
# 670 672 681 683 692 694 760 771 780 782 791 793
# 870 881 890 892 980 991
# suppose num = 351
# 5 = 5
# Even Position ↑
# 3 5 1
# Odd Position ↓ ↓
# 3 + 1 = 4
# ----------------------------------
# 5 - 4 = 1 Difference is 1
task.combination(3)
end
main()
Output
Given n : 2
10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
Given n : 3
100 111 120 122 131 133 142 144 153 155 164 166 175 177 186 188 197 199 210 221 230 232 241 243 252 254 263 265 274 276 285 287 296 298 320 331 340 342 351 353 362 364 373 375 384 386 395 397 430 441 450 452 461 463 472 474 483 485 494 496 540 551 560 562 571 573 582 584 593 595 650 661 670 672 681 683 692 694 760 771 780 782 791 793 870 881 890 892 980 991
/*
Scala Program
Print all n-digit numbers with sum of even and odd position
digits whose absolute difference are one.
*/
class Combinations()
{
// Display result
def printSequence(result: Array[Int], k: Int): Unit = {
var i: Int = 0;
while (i < k)
{
print(result(i));
i += 1;
}
print(" ");
}
def absValue(x: Int): Int = {
if (x < 0)
{
return -x;
}
return x;
}
def findCombination(result: Array[Int],
index: Int, even: Int, odd: Int, n: Int): Unit = {
if (index == n && absValue(even - odd) == 1)
{
// Display calculated result
printSequence(result, index);
}
if (index >= n)
{
return;
}
var i: Int = 0;
while (i <= 9)
{
result(index) = i;
if (!(index == 0 && i == 0))
{
if (index % 2 == 0)
{
// When positions are Even
findCombination(result,
index + 1,
even + i,
odd, n);
}
else
{
// When positions are Odd
findCombination(result,
index + 1, even,
odd + i, n);
}
}
i += 1;
}
}
// Handles the request of find combination of given n
def combination(n: Int): Unit = {
if (n <= 0)
{
return;
}
// N is length
println("\n Given n : " + n);
// Collect result
var result: Array[Int] = Array.fill[Int](n)(0);
// Test
findCombination(result, 0, 0, 0, n);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Combinations = new Combinations();
// n = 2
// 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
// Here
// Value Digit Difference
// 10 [1,0] = 1
// 12 [1,2] = 1
// etc
task.combination(2);
/*
n = 3
100 111 120 122 131 133 142 144 153 155 164 166
175 177 186 188 197 199 210 221 230 232 241 243
252 254 263 265 274 276 285 287 296 298 320 331
340 342 351 353 362 364 373 375 384 386 395 397
430 441 450 452 461 463 472 474 483 485 494 496
540 551 560 562 571 573 582 584 593 595 650 661
670 672 681 683 692 694 760 771 780 782 791 793
870 881 890 892 980 991
*/
// suppose num = 351
// 5 = 5
// Even Position ↑
// 3 5 1
// Odd Position ↓ ↓
// 3 + 1 = 4
// ----------------------------------
// 5 - 4 = 1 Difference is 1
task.combination(3);
}
}
Output
Given n : 2
10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
Given n : 3
100 111 120 122 131 133 142 144 153 155 164 166 175 177 186 188 197 199 210 221 230 232 241 243 252 254 263 265 274 276 285 287 296 298 320 331 340 342 351 353 362 364 373 375 384 386 395 397 430 441 450 452 461 463 472 474 483 485 494 496 540 551 560 562 571 573 582 584 593 595 650 661 670 672 681 683 692 694 760 771 780 782 791 793 870 881 890 892 980 991
/*
Swift 4 Program
Print all n-digit numbers with sum of even and odd position
digits whose absolute difference are one.
*/
class Combinations
{
// Display result
func printSequence(_ result: [Int], _ k: Int)
{
var i: Int = 0;
while (i < k)
{
print(result[i], terminator: "");
i += 1;
}
print(" ", terminator: "");
}
func absValue(_ x: Int) -> Int
{
if (x < 0)
{
return -x;
}
return x;
}
func findCombination(_ result: inout[Int],
_ index: Int,
_ even: Int,
_ odd: Int,
_ n: Int)
{
if (index == n && self.absValue(even - odd) == 1)
{
// Display calculated result
self.printSequence(result, index);
}
if (index >= n)
{
return;
}
var i: Int = 0;
while (i <= 9)
{
result[index] = i;
if (!(index == 0 && i == 0))
{
if (index % 2 == 0)
{
// When positions are Even
self.findCombination(&result, index + 1, even + i, odd, n);
}
else
{
// When positions are Odd
self.findCombination(&result, index + 1, even, odd + i, n);
}
}
i += 1;
}
}
// Handles the request of find combination of given n
func combination(_ n: Int)
{
if (n <= 0)
{
return;
}
// N is length
print("\n Given n : ", n);
// Collect result
var result: [Int] = Array(repeating: 0, count: n);
// Test
self.findCombination(&result, 0, 0, 0, n);
}
}
func main()
{
let task: Combinations = Combinations();
// n = 2
// 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
// Here
// Value Digit Difference
// 10 [1,0] = 1
// 12 [1,2] = 1
// etc
task.combination(2);
/*
n = 3
100 111 120 122 131 133 142 144 153 155 164 166
175 177 186 188 197 199 210 221 230 232 241 243
252 254 263 265 274 276 285 287 296 298 320 331
340 342 351 353 362 364 373 375 384 386 395 397
430 441 450 452 461 463 472 474 483 485 494 496
540 551 560 562 571 573 582 584 593 595 650 661
670 672 681 683 692 694 760 771 780 782 791 793
870 881 890 892 980 991
*/
// suppose num = 351
// 5 = 5
// Even Position ↑
// 3 5 1
// Odd Position ↓ ↓
// 3 + 1 = 4
// ----------------------------------
// 5 - 4 = 1 Difference is 1
task.combination(3);
}
main();
Output
Given n : 2
10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
Given n : 3
100 111 120 122 131 133 142 144 153 155 164 166 175 177 186 188 197 199 210 221 230 232 241 243 252 254 263 265 274 276 285 287 296 298 320 331 340 342 351 353 362 364 373 375 384 386 395 397 430 441 450 452 461 463 472 474 483 485 494 496 540 551 560 562 571 573 582 584 593 595 650 661 670 672 681 683 692 694 760 771 780 782 791 793 870 881 890 892 980 991
/*
Kotlin Program
Print all n-digit numbers with sum of even and odd position
digits whose absolute difference are one.
*/
class Combinations
{
// Display result
fun printSequence(result: Array < Int > , k: Int): Unit
{
var i: Int = 0;
while (i < k)
{
print(result[i]);
i += 1;
}
print(" ");
}
fun absValue(x: Int): Int
{
if (x < 0)
{
return -x;
}
return x;
}
fun findCombination(result: Array < Int > ,
index: Int, even: Int,
odd: Int, n: Int): Unit
{
if (index == n && this.absValue(even - odd) == 1)
{
// Display calculated result
this.printSequence(result, index);
}
if (index >= n)
{
return;
}
var i: Int = 0;
while (i <= 9)
{
result[index] = i;
if (!(index == 0 && i == 0))
{
if (index % 2 == 0)
{
// When positions are Even
this.findCombination(result,
index + 1,
even + i,
odd, n);
}
else
{
// When positions are Odd
this.findCombination(result,
index + 1,
even,
odd + i, n);
}
}
i += 1;
}
}
// Handles the request of find combination of given n
fun combination(n: Int): Unit
{
if (n <= 0)
{
return;
}
// N is length
println("\n Given n : " + n);
// Collect result
val result: Array < Int > = Array(n)
{
0
};
// Test
this.findCombination(result, 0, 0, 0, n);
}
}
fun main(args: Array < String > ): Unit
{
val task: Combinations = Combinations();
// n = 2
// 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
// Here
// Value Digit Difference
// 10 [1,0] = 1
// 12 [1,2] = 1
// etc
task.combination(2);
/*
n = 3
100 111 120 122 131 133 142 144 153 155 164 166
175 177 186 188 197 199 210 221 230 232 241 243
252 254 263 265 274 276 285 287 296 298 320 331
340 342 351 353 362 364 373 375 384 386 395 397
430 441 450 452 461 463 472 474 483 485 494 496
540 551 560 562 571 573 582 584 593 595 650 661
670 672 681 683 692 694 760 771 780 782 791 793
870 881 890 892 980 991
*/
// suppose num = 351
// 5 = 5
// Even Position ↑
// 3 5 1
// Odd Position ↓ ↓
// 3 + 1 = 4
// ----------------------------------
// 5 - 4 = 1 Difference is 1
task.combination(3);
}
Output
Given n : 2
10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98
Given n : 3
100 111 120 122 131 133 142 144 153 155 164 166 175 177 186 188 197 199 210 221 230 232 241 243 252 254 263 265 274 276 285 287 296 298 320 331 340 342 351 353 362 364 373 375 384 386 395 397 430 441 450 452 461 463 472 474 483 485 494 496 540 551 560 562 571 573 582 584 593 595 650 661 670 672 681 683 692 694 760 771 780 782 791 793 870 881 890 892 980 991
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