Posted on by Kalkicode
Code Mathematics

Find combination of k with consecutive absolute difference

Here given code implementation process.

// C Program
// Find combination of k with consecutive absolute difference
#include <stdio.h>

int absoluteValue(int x)
{
	if (x < 0)
	{
		return -x;
	}
	return x;
}
void combinations(int k)
{
	int n = absoluteValue(k);
	if (n <= 1)
	{
		return;
	}
	printf("\n Given k : %d\n", k);
	int result[n];
	// Auxiliary variable
	int min = 1;
	int max = k;
	int flag = 0;
	if (k < 0)
	{
		min = k;
		max = -1;
	}
	// Counter variable
	int i = min;
	int j = max;
	int count = 0;
	while (i - 1 <= j && count < n)
	{
		if (count % 2 == 0)
		{
			result[count] = j;
			j--;
		}
		else
		{
			result[count] = i;
			i++;
		}
		count++;
	}
	// Display calculated result
	for (int i = 0; i < n; ++i)
	{
		printf("  %d", result[i]);
	}
}
int main()
{
	/*
	    k = 9
	    -------------------------
	    (9 - 1) : 8
	    (1 - 8) : -7
	    (8 - 2) : 6
	    (2 - 7) : -5
	    (7 - 3) : 4
	    (3 - 6) : -3
	    (6 - 4) : 2
	    (4 - 5) : -1
	    -------------------------
	    9  1  8  2  7  3  6  4  5


	    Alternate decreasing sequence
	                ↑
	    ╔┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╗
	    │     │     │     │     │ 
	    │     │     │     │     │
	    9  1  8  2  7  3  6  4  5
	       │     │     │     │  
	       │     │     │     │
	       ╰┄┄┄┄┄╩┄┄┄┄┄╩┄┄┄┄┄┘
	                ↆ
	    Alternate increasing order
	*/
	combinations(9);
	/*
	    Given k : -9
	    ---------------------------------
	    (-1 - -9) : 8
	    (-9 - -2) : -7
	    (-2 - -8) : 6
	    (-8 - -3) : -5
	    (-3 - -7) : 4
	    (-7 - -4) : -3
	    (-4 - -6) : 2
	    (-6 - -5) : -1
	    ------------------------------------
	    -1  -9  -2  -8  -3  -7  -4  -6  -5
	*/
	combinations(-9);
	/*
	    Given k : 5
	    -------------------
	    (5 - 1) : 4
	    (1 - 4) : -3
	    (4 - 2) : 2
	    (2 - 3) : -1
	    -------------------
	    5  1  4  2  3
	*/
	combinations(5);
	return 0;
}

Output

 Given k : 9
  9  1  8  2  7  3  6  4  5
 Given k : -9
  -1  -9  -2  -8  -3  -7  -4  -6  -5
 Given k : 5
  5  1  4  2  3
// Java program for
// Find combination of k with consecutive absolute difference
public class Combinations
{
	public int absoluteValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	public void combinations(int k)
	{
		int n = absoluteValue(k);
		if (n <= 1)
		{
			return;
		}
		System.out.print("\n Given k : " + k + "\n");
		int[] result = new int[n];
		// Auxiliary variable
		int min = 1;
		int max = k;
		int flag = 0;
		if (k < 0)
		{
			min = k;
			max = -1;
		}
		// Counter variable
		int i = min;
		int j = max;
		int count = 0;
		while (i - 1 <= j && count < n)
		{
			if (count % 2 == 0)
			{
				result[count] = j;
				j--;
			}
			else
			{
				result[count] = i;
				i++;
			}
			count++;
		}
		// Display calculated result
		for (i = 0; i < n; ++i)
		{
			System.out.print(" " + result[i]);
		}
	}
	public static void main(String[] args)
	{
		Combinations task = new Combinations();
		/*
		    k = 9
		    -------------------------
		    (9 - 1) : 8
		    (1 - 8) : -7
		    (8 - 2) : 6
		    (2 - 7) : -5
		    (7 - 3) : 4
		    (3 - 6) : -3
		    (6 - 4) : 2
		    (4 - 5) : -1
		    -------------------------
		    9  1  8  2  7  3  6  4  5


		    Alternate decreasing sequence
		                ↑
		    ╔┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╗
		    │     │     │     │     │ 
		    │     │     │     │     │
		    9  1  8  2  7  3  6  4  5
		       │     │     │     │  
		       │     │     │     │
		       ╰┄┄┄┄┄╩┄┄┄┄┄╩┄┄┄┄┄┘
		                ↆ
		    Alternate increasing order
		*/
		task.combinations(9);
		/*
		    Given k : -9
		    ---------------------------------
		    (-1 - -9) : 8
		    (-9 - -2) : -7
		    (-2 - -8) : 6
		    (-8 - -3) : -5
		    (-3 - -7) : 4
		    (-7 - -4) : -3
		    (-4 - -6) : 2
		    (-6 - -5) : -1
		    ------------------------------------
		    -1  -9  -2  -8  -3  -7  -4  -6  -5
		*/
		task.combinations(-9);
		/*
		    Given k : 5
		    -------------------
		    (5 - 1) : 4
		    (1 - 4) : -3
		    (4 - 2) : 2
		    (2 - 3) : -1
		    -------------------
		    5  1  4  2  3
		*/
		task.combinations(5);
	}
}

Output

 Given k : 9
 9 1 8 2 7 3 6 4 5
 Given k : -9
 -1 -9 -2 -8 -3 -7 -4 -6 -5
 Given k : 5
 5 1 4 2 3
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Find combination of k with consecutive absolute difference
class Combinations
{
	public: int absoluteValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	void combinations(int k)
	{
		int n = this->absoluteValue(k);
		if (n <= 1)
		{
			return;
		}
		cout << "\n Given k : " << k << "\n";
		int result[n];
		// Auxiliary variable
		int min = 1;
		int max = k;
		int flag = 0;
		if (k < 0)
		{
			min = k;
			max = -1;
		}
		// Counter variable
		int i = min;
		int j = max;
		int count = 0;
		while (i - 1 <= j && count < n)
		{
			if (count % 2 == 0)
			{
				result[count] = j;
				j--;
			}
			else
			{
				result[count] = i;
				i++;
			}
			count++;
		}
		// Display calculated result
		for (i = 0; i < n; ++i)
		{
			cout << " " << result[i];
		}
	}
};
int main()
{
	Combinations *task = new Combinations();
	/*
	    k = 9
	    -------------------------
	    (9 - 1) : 8
	    (1 - 8) : -7
	    (8 - 2) : 6
	    (2 - 7) : -5
	    (7 - 3) : 4
	    (3 - 6) : -3
	    (6 - 4) : 2
	    (4 - 5) : -1
	    -------------------------
	    9  1  8  2  7  3  6  4  5
	    Alternate decreasing sequence
	                ↑
	    ╔┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╗
	    │     │     │     │     │ 
	    │     │     │     │     │
	    9  1  8  2  7  3  6  4  5
	       │     │     │     │  
	       │     │     │     │
	       ╰┄┄┄┄┄╩┄┄┄┄┄╩┄┄┄┄┄┘
	                ↆ
	    Alternate increasing order
	*/
	task->combinations(9);
	/*
	    Given k : -9
	    ---------------------------------
	    (-1 - -9) : 8
	    (-9 - -2) : -7
	    (-2 - -8) : 6
	    (-8 - -3) : -5
	    (-3 - -7) : 4
	    (-7 - -4) : -3
	    (-4 - -6) : 2
	    (-6 - -5) : -1
	    ------------------------------------
	    -1  -9  -2  -8  -3  -7  -4  -6  -5
	*/
	task->combinations(-9);
	/*
	    Given k : 5
	    -------------------
	    (5 - 1) : 4
	    (1 - 4) : -3
	    (4 - 2) : 2
	    (2 - 3) : -1
	    -------------------
	    5  1  4  2  3
	*/
	task->combinations(5);
	return 0;
}

Output

 Given k : 9
 9 1 8 2 7 3 6 4 5
 Given k : -9
 -1 -9 -2 -8 -3 -7 -4 -6 -5
 Given k : 5
 5 1 4 2 3
// Include namespace system
using System;
// Csharp program for
// Find combination of k with consecutive absolute difference
public class Combinations
{
	public int absoluteValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	public void combinations(int k)
	{
		int n = this.absoluteValue(k);
		if (n <= 1)
		{
			return;
		}
		Console.Write("\n Given k : " + k + "\n");
		int[] result = new int[n];
		// Auxiliary variable
		int min = 1;
		int max = k;
		if (k < 0)
		{
			min = k;
			max = -1;
		}
		// Counter variable
		int i = min;
		int j = max;
		int count = 0;
		while (i - 1 <= j && count < n)
		{
			if (count % 2 == 0)
			{
				result[count] = j;
				j--;
			}
			else
			{
				result[count] = i;
				i++;
			}
			count++;
		}
		// Display calculated result
		for (i = 0; i < n; ++i)
		{
			Console.Write(" " + result[i]);
		}
	}
	public static void Main(String[] args)
	{
		Combinations task = new Combinations();
		/*
		    k = 9
		    -------------------------
		    (9 - 1) : 8
		    (1 - 8) : -7
		    (8 - 2) : 6
		    (2 - 7) : -5
		    (7 - 3) : 4
		    (3 - 6) : -3
		    (6 - 4) : 2
		    (4 - 5) : -1
		    -------------------------
		    9  1  8  2  7  3  6  4  5
		    Alternate decreasing sequence
		                ↑
		    ╔┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╗
		    │     │     │     │     │ 
		    │     │     │     │     │
		    9  1  8  2  7  3  6  4  5
		       │     │     │     │  
		       │     │     │     │
		       ╰┄┄┄┄┄╩┄┄┄┄┄╩┄┄┄┄┄┘
		                ↆ
		    Alternate increasing order
		*/
		task.combinations(9);
		/*
		    Given k : -9
		    ---------------------------------
		    (-1 - -9) : 8
		    (-9 - -2) : -7
		    (-2 - -8) : 6
		    (-8 - -3) : -5
		    (-3 - -7) : 4
		    (-7 - -4) : -3
		    (-4 - -6) : 2
		    (-6 - -5) : -1
		    ------------------------------------
		    -1  -9  -2  -8  -3  -7  -4  -6  -5
		*/
		task.combinations(-9);
		/*
		    Given k : 5
		    -------------------
		    (5 - 1) : 4
		    (1 - 4) : -3
		    (4 - 2) : 2
		    (2 - 3) : -1
		    -------------------
		    5  1  4  2  3
		*/
		task.combinations(5);
	}
}

Output

 Given k : 9
 9 1 8 2 7 3 6 4 5
 Given k : -9
 -1 -9 -2 -8 -3 -7 -4 -6 -5
 Given k : 5
 5 1 4 2 3
package main
import "fmt"
// Go program for
// Find combination of k with consecutive absolute difference
type Combinations struct {}
func getCombinations() * Combinations {
	var me *Combinations = &Combinations {}
	return me
}
func(this Combinations) absoluteValue(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func(this Combinations) combinations(k int) {
	var n int = this.absoluteValue(k)
	if n <= 1 {
		return
	}
	fmt.Print("\n Given k : ", k, "\n")
	var result = make([] int, n)
	// Auxiliary variable
	var min int = 1
	var max int = k
	if k < 0 {
		min = k
		max = -1
	}
	// Counter variable
	var i int = min
	var j int = max
	var count int = 0
	for (i - 1 <= j && count < n) {
		if count % 2 == 0 {
			result[count] = j
			j--
		} else {
			result[count] = i
			i++
		}
		count++
	}
	// Display calculated result
	for i = 0 ; i < n ; i++ {
		fmt.Print(" ", result[i])
	}
}
func main() {
	var task * Combinations = getCombinations()
	/*
	    k = 9
	    -------------------------
	    (9 - 1) : 8
	    (1 - 8) : -7
	    (8 - 2) : 6
	    (2 - 7) : -5
	    (7 - 3) : 4
	    (3 - 6) : -3
	    (6 - 4) : 2
	    (4 - 5) : -1
	    -------------------------
	    9  1  8  2  7  3  6  4  5
	    Alternate decreasing sequence
	                ↑
	    ╔┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╗
	    │     │     │     │     │ 
	    │     │     │     │     │
	    9  1  8  2  7  3  6  4  5
	       │     │     │     │  
	       │     │     │     │
	       ╰┄┄┄┄┄╩┄┄┄┄┄╩┄┄┄┄┄┘
	                ↆ
	    Alternate increasing order
	*/
	task.combinations(9)
	/*
	    Given k : -9
	    ---------------------------------
	    (-1 - -9) : 8
	    (-9 - -2) : -7
	    (-2 - -8) : 6
	    (-8 - -3) : -5
	    (-3 - -7) : 4
	    (-7 - -4) : -3
	    (-4 - -6) : 2
	    (-6 - -5) : -1
	    ------------------------------------
	    -1  -9  -2  -8  -3  -7  -4  -6  -5
	*/
	task.combinations(-9)
	/*
	    Given k : 5
	    -------------------
	    (5 - 1) : 4
	    (1 - 4) : -3
	    (4 - 2) : 2
	    (2 - 3) : -1
	    -------------------
	    5  1  4  2  3
	*/
	task.combinations(5)
}

Output

 Given k : 9
 9 1 8 2 7 3 6 4 5
 Given k : -9
 -1 -9 -2 -8 -3 -7 -4 -6 -5
 Given k : 5
 5 1 4 2 3
<?php
// Php program for
// Find combination of k with consecutive absolute difference
class Combinations
{
	public	function absoluteValue($x)
	{
		if ($x < 0)
		{
			return -$x;
		}
		return $x;
	}
	public	function combination($k)
	{
		$n = $this->absoluteValue($k);
		if ($n <= 1)
		{
			return;
		}
		echo("\n Given k : ".$k."\n");
		$result = array_fill(0, $n, 0);
		// Auxiliary variable
		$min = 1;
		$max = $k;
		if ($k < 0)
		{
			$min = $k;
			$max = -1;
		}
		// Counter variable
		$i = $min;
		$j = $max;
		$count = 0;
		while ($i - 1 <= $j && $count < $n)
		{
			if ($count % 2 == 0)
			{
				$result[$count] = $j;
				$j--;
			}
			else
			{
				$result[$count] = $i;
				$i++;
			}
			$count++;
		}
		// Display calculated result
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$result[$i]);
		}
	}
}

function main()
{
	$task = new Combinations();
	/*
	    k = 9
	    -------------------------
	    (9 - 1) : 8
	    (1 - 8) : -7
	    (8 - 2) : 6
	    (2 - 7) : -5
	    (7 - 3) : 4
	    (3 - 6) : -3
	    (6 - 4) : 2
	    (4 - 5) : -1
	    -------------------------
	    9  1  8  2  7  3  6  4  5
	    Alternate decreasing sequence
	                ↑
	    ╔┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╗
	    │     │     │     │     │ 
	    │     │     │     │     │
	    9  1  8  2  7  3  6  4  5
	       │     │     │     │  
	       │     │     │     │
	       ╰┄┄┄┄┄╩┄┄┄┄┄╩┄┄┄┄┄┘
	                ↆ
	    Alternate increasing order
	*/
	$task->combination(9);
	/*
	    Given k : -9
	    ---------------------------------
	    (-1 - -9) : 8
	    (-9 - -2) : -7
	    (-2 - -8) : 6
	    (-8 - -3) : -5
	    (-3 - -7) : 4
	    (-7 - -4) : -3
	    (-4 - -6) : 2
	    (-6 - -5) : -1
	    ------------------------------------
	    -1  -9  -2  -8  -3  -7  -4  -6  -5
	*/
	$task->combination(-9);
	/*
	    Given k : 5
	    -------------------
	    (5 - 1) : 4
	    (1 - 4) : -3
	    (4 - 2) : 2
	    (2 - 3) : -1
	    -------------------
	    5  1  4  2  3
	*/
	$task->combination(5);
}
main();

Output

 Given k : 9
 9 1 8 2 7 3 6 4 5
 Given k : -9
 -1 -9 -2 -8 -3 -7 -4 -6 -5
 Given k : 5
 5 1 4 2 3
// Node JS program for
// Find combination of k with consecutive absolute difference
class Combinations
{
	absoluteValue(x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	combinations(k)
	{
		var n = this.absoluteValue(k);
		if (n <= 1)
		{
			return;
		}
		process.stdout.write("\n Given k : " + k + "\n");
		var result = Array(n).fill(0);
		// Auxiliary variable
		var min = 1;
		var max = k;
		if (k < 0)
		{
			min = k;
			max = -1;
		}
		// Counter variable
		var i = min;
		var j = max;
		var count = 0;
		while (i - 1 <= j && count < n)
		{
			if (count % 2 == 0)
			{
				result[count] = j;
				j--;
			}
			else
			{
				result[count] = i;
				i++;
			}
			count++;
		}
		// Display calculated result
		for (i = 0; i < n; ++i)
		{
			process.stdout.write(" " + result[i]);
		}
	}
}

function main()
{
	var task = new Combinations();
	/*
	    k = 9
	    -------------------------
	    (9 - 1) : 8
	    (1 - 8) : -7
	    (8 - 2) : 6
	    (2 - 7) : -5
	    (7 - 3) : 4
	    (3 - 6) : -3
	    (6 - 4) : 2
	    (4 - 5) : -1
	    -------------------------
	    9  1  8  2  7  3  6  4  5
	    Alternate decreasing sequence
	                ↑
	    ╔┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╗
	    │     │     │     │     │ 
	    │     │     │     │     │
	    9  1  8  2  7  3  6  4  5
	       │     │     │     │  
	       │     │     │     │
	       ╰┄┄┄┄┄╩┄┄┄┄┄╩┄┄┄┄┄┘
	                ↆ
	    Alternate increasing order
	*/
	task.combinations(9);
	/*
	    Given k : -9
	    ---------------------------------
	    (-1 - -9) : 8
	    (-9 - -2) : -7
	    (-2 - -8) : 6
	    (-8 - -3) : -5
	    (-3 - -7) : 4
	    (-7 - -4) : -3
	    (-4 - -6) : 2
	    (-6 - -5) : -1
	    ------------------------------------
	    -1  -9  -2  -8  -3  -7  -4  -6  -5
	*/
	task.combinations(-9);
	/*
	    Given k : 5
	    -------------------
	    (5 - 1) : 4
	    (1 - 4) : -3
	    (4 - 2) : 2
	    (2 - 3) : -1
	    -------------------
	    5  1  4  2  3
	*/
	task.combinations(5);
}
main();

Output

 Given k : 9
 9 1 8 2 7 3 6 4 5
 Given k : -9
 -1 -9 -2 -8 -3 -7 -4 -6 -5
 Given k : 5
 5 1 4 2 3
#  Python 3 program for
#  Find combination of k with consecutive absolute difference
class Combinations :
	def absoluteValue(self, x) :
		if (x < 0) :
			return -x
		
		return x
	
	def combinations(self, k) :
		n = self.absoluteValue(k)
		if (n <= 1) :
			return
		
		print("\n Given k : ", k )
		result = [0] * (n)
		#  Auxiliary variable
		min = 1
		max = k
		if (k < 0) :
			min = k
			max = -1
		
		#  Counter variable
		i = min
		j = max
		count = 0
		while (i - 1 <= j and count < n) :
			if (count % 2 == 0) :
				result[count] = j
				j -= 1
			else :
				result[count] = i
				i += 1
			
			count += 1
		
		i = 0
		#  Display calculated result
		while (i < n) :
			print(" ", result[i], end = "")
			i += 1
		
	

def main() :
	task = Combinations()
	#    k = 9
	#    -------------------------
	#    (9 - 1) : 8
	#    (1 - 8) : -7
	#    (8 - 2) : 6
	#    (2 - 7) : -5
	#    (7 - 3) : 4
	#    (3 - 6) : -3
	#    (6 - 4) : 2
	#    (4 - 5) : -1
	#    -------------------------
	#    9  1  8  2  7  3  6  4  5
	#    Alternate decreasing sequence
	#                ↑
	#    ╔┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╗
	#    │     │     │     │     │ 
	#    │     │     │     │     │
	#    9  1  8  2  7  3  6  4  5
	#       │     │     │     │  
	#       │     │     │     │
	#       ╰┄┄┄┄┄╩┄┄┄┄┄╩┄┄┄┄┄┘
	#                ↆ
	#    Alternate increasing order
	task.combinations(9)
	#    Given k : -9
	#    ---------------------------------
	#    (-1 - -9) : 8
	#    (-9 - -2) : -7
	#    (-2 - -8) : 6
	#    (-8 - -3) : -5
	#    (-3 - -7) : 4
	#    (-7 - -4) : -3
	#    (-4 - -6) : 2
	#    (-6 - -5) : -1
	#    ------------------------------------
	#    -1  -9  -2  -8  -3  -7  -4  -6  -5
	task.combinations(-9)
	#    Given k : 5
	#    -------------------
	#    (5 - 1) : 4
	#    (1 - 4) : -3
	#    (4 - 2) : 2
	#    (2 - 3) : -1
	#    -------------------
	#    5  1  4  2  3
	task.combinations(5)

if __name__ == "__main__": main()

Output

 Given k :  9
  9  1  8  2  7  3  6  4  5
 Given k :  -9
  -1  -9  -2  -8  -3  -7  -4  -6  -5
 Given k :  5
  5  1  4  2  3
#  Ruby program for
#  Find combination of k with consecutive absolute difference
class Combinations 
	def absoluteValue(x) 
		if (x < 0) 
			return -x
		end

		return x
	end

	def combinations(k) 
		n = self.absoluteValue(k)
		if (n <= 1) 
			return
		end

		print("\n Given k : ", k ,"\n")
		result = Array.new(n) {0}
		#  Auxiliary variable
		min = 1
		max = k
		if (k < 0) 
			min = k
			max = -1
		end

		#  Counter variable
		i = min
		j = max
		count = 0
		while (i - 1 <= j && count < n) 
			if (count % 2 == 0) 
				result[count] = j
				j -= 1
			else
 
				result[count] = i
				i += 1
			end

			count += 1
		end

		i = 0
		#  Display calculated result
		while (i < n) 
			print(" ", result[i])
			i += 1
		end

	end

end

def main() 
	task = Combinations.new()
	#    k = 9
	#    -------------------------
	#    (9 - 1) : 8
	#    (1 - 8) : -7
	#    (8 - 2) : 6
	#    (2 - 7) : -5
	#    (7 - 3) : 4
	#    (3 - 6) : -3
	#    (6 - 4) : 2
	#    (4 - 5) : -1
	#    -------------------------
	#    9  1  8  2  7  3  6  4  5
	#    Alternate decreasing sequence
	#                ↑
	#    ╔┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╗
	#    │     │     │     │     │ 
	#    │     │     │     │     │
	#    9  1  8  2  7  3  6  4  5
	#       │     │     │     │  
	#       │     │     │     │
	#       ╰┄┄┄┄┄╩┄┄┄┄┄╩┄┄┄┄┄┘
	#                ↆ
	#    Alternate increasing order
	task.combinations(9)
	#    Given k : -9
	#    ---------------------------------
	#    (-1 - -9) : 8
	#    (-9 - -2) : -7
	#    (-2 - -8) : 6
	#    (-8 - -3) : -5
	#    (-3 - -7) : 4
	#    (-7 - -4) : -3
	#    (-4 - -6) : 2
	#    (-6 - -5) : -1
	#    ------------------------------------
	#    -1  -9  -2  -8  -3  -7  -4  -6  -5
	task.combinations(-9)
	#    Given k : 5
	#    -------------------
	#    (5 - 1) : 4
	#    (1 - 4) : -3
	#    (4 - 2) : 2
	#    (2 - 3) : -1
	#    -------------------
	#    5  1  4  2  3
	task.combinations(5)
end

main()

Output

 Given k : 9
 9 1 8 2 7 3 6 4 5
 Given k : -9
 -1 -9 -2 -8 -3 -7 -4 -6 -5
 Given k : 5
 5 1 4 2 3
// Scala program for
// Find combination of k with consecutive absolute difference
class Combinations()
{
	def absoluteValue(x: Int): Int = {
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	def combinations(k: Int): Unit = {
		var n: Int = absoluteValue(k);
		if (n <= 1)
		{
			return;
		}
		print("\n Given k : " + k + "\n");
		var result: Array[Int] = Array.fill[Int](n)(0);
		// Auxiliary variable
		var min: Int = 1;
		var max: Int = k;
		if (k < 0)
		{
			min = k;
			max = -1;
		}
		// Counter variable
		var i: Int = min;
		var j: Int = max;
		var count: Int = 0;
		while (i - 1 <= j && count < n)
		{
			if (count % 2 == 0)
			{
				result(count) = j;
				j -= 1;
			}
			else
			{
				result(count) = i;
				i += 1;
			}
			count += 1;
		}
		i = 0;
		// Display calculated result
		while (i < n)
		{
			print(" " + result(i));
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combinations = new Combinations();
		/*
		    k = 9
		    -------------------------
		    (9 - 1) : 8
		    (1 - 8) : -7
		    (8 - 2) : 6
		    (2 - 7) : -5
		    (7 - 3) : 4
		    (3 - 6) : -3
		    (6 - 4) : 2
		    (4 - 5) : -1
		    -------------------------
		    9  1  8  2  7  3  6  4  5
		    Alternate decreasing sequence
		                ↑
		    ╔┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╗
		    │     │     │     │     │ 
		    │     │     │     │     │
		    9  1  8  2  7  3  6  4  5
		       │     │     │     │  
		       │     │     │     │
		       ╰┄┄┄┄┄╩┄┄┄┄┄╩┄┄┄┄┄┘
		                ↆ
		    Alternate increasing order
		*/
		task.combinations(9);
		/*
		    Given k : -9
		    ---------------------------------
		    (-1 - -9) : 8
		    (-9 - -2) : -7
		    (-2 - -8) : 6
		    (-8 - -3) : -5
		    (-3 - -7) : 4
		    (-7 - -4) : -3
		    (-4 - -6) : 2
		    (-6 - -5) : -1
		    ------------------------------------
		    -1  -9  -2  -8  -3  -7  -4  -6  -5
		*/
		task.combinations(-9);
		/*
		    Given k : 5
		    -------------------
		    (5 - 1) : 4
		    (1 - 4) : -3
		    (4 - 2) : 2
		    (2 - 3) : -1
		    -------------------
		    5  1  4  2  3
		*/
		task.combinations(5);
	}
}

Output

 Given k : 9
 9 1 8 2 7 3 6 4 5
 Given k : -9
 -1 -9 -2 -8 -3 -7 -4 -6 -5
 Given k : 5
 5 1 4 2 3
// Swift 4 program for
// Find combination of k with consecutive absolute difference
class Combinations
{
	func absoluteValue(_ x: Int) -> Int
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	func combinations(_ k: Int)
	{
		let n: Int = self.absoluteValue(k);
		if (n <= 1)
		{
			return;
		}
		print("\n Given k : ", k );
		var result: [Int] = Array(repeating: 0, count: n);
		// Auxiliary variable
		var min: Int = 1;
		var max: Int = k;
		if (k < 0)
		{
			min = k;
			max = -1;
		}
		// Counter variable
		var i: Int = min;
		var j: Int = max;
		var count: Int = 0;
		while (i - 1 <= j && count < n)
		{
			if (count % 2 == 0)
			{
				result[count] = j;
				j -= 1;
			}
			else
			{
				result[count] = i;
				i += 1;
			}
			count += 1;
		}
		i = 0;
		// Display calculated result
		while (i < n)
		{
			print(" ", result[i], terminator: "");
			i += 1;
		}
	}
}
func main()
{
	let task: Combinations = Combinations();
	/*
	    k = 9
	    -------------------------
	    (9 - 1) : 8
	    (1 - 8) : -7
	    (8 - 2) : 6
	    (2 - 7) : -5
	    (7 - 3) : 4
	    (3 - 6) : -3
	    (6 - 4) : 2
	    (4 - 5) : -1
	    -------------------------
	    9  1  8  2  7  3  6  4  5
	    Alternate decreasing sequence
	                ↑
	    ╔┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╗
	    │     │     │     │     │ 
	    │     │     │     │     │
	    9  1  8  2  7  3  6  4  5
	       │     │     │     │  
	       │     │     │     │
	       ╰┄┄┄┄┄╩┄┄┄┄┄╩┄┄┄┄┄┘
	                ↆ
	    Alternate increasing order
	*/
	task.combinations(9);
	/*
	    Given k : -9
	    ---------------------------------
	    (-1 - -9) : 8
	    (-9 - -2) : -7
	    (-2 - -8) : 6
	    (-8 - -3) : -5
	    (-3 - -7) : 4
	    (-7 - -4) : -3
	    (-4 - -6) : 2
	    (-6 - -5) : -1
	    ------------------------------------
	    -1  -9  -2  -8  -3  -7  -4  -6  -5
	*/
	task.combinations(-9);
	/*
	    Given k : 5
	    -------------------
	    (5 - 1) : 4
	    (1 - 4) : -3
	    (4 - 2) : 2
	    (2 - 3) : -1
	    -------------------
	    5  1  4  2  3
	*/
	task.combinations(5);
}
main();

Output

 Given k :  9
  9  1  8  2  7  3  6  4  5
 Given k :  -9
  -1  -9  -2  -8  -3  -7  -4  -6  -5
 Given k :  5
  5  1  4  2  3
// Kotlin program for
// Find combination of k with consecutive absolute difference
class Combinations
{
	fun absoluteValue(x: Int): Int
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	fun combinations(k: Int): Unit
	{
		val n: Int = this.absoluteValue(k);
		if (n <= 1)
		{
			return;
		}
		print("\n Given k : " + k + "\n");
		val result: Array < Int > = Array(n)
		{
			0
		};
		// Auxiliary variable
		var min: Int = 1;
		var max: Int = k;
		if (k < 0)
		{
			min = k;
			max = -1;
		}
		// Counter variable
		var i: Int = min;
		var j: Int = max;
		var count: Int = 0;
		while (i - 1 <= j && count < n)
		{
			if (count % 2 == 0)
			{
				result[count] = j;
				j -= 1;
			}
			else
			{
				result[count] = i;
				i += 1;
			}
			count += 1;
		}
		i = 0;
		// Display calculated result
		while (i < n)
		{
			print(" " + result[i]);
			i += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combinations = Combinations();
	/*
	    k = 9
	    -------------------------
	    (9 - 1) : 8
	    (1 - 8) : -7
	    (8 - 2) : 6
	    (2 - 7) : -5
	    (7 - 3) : 4
	    (3 - 6) : -3
	    (6 - 4) : 2
	    (4 - 5) : -1
	    -------------------------
	    9  1  8  2  7  3  6  4  5
	    Alternate decreasing sequence
	                ↑
	    ╔┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╦┄┄┄┄┄╗
	    │     │     │     │     │ 
	    │     │     │     │     │
	    9  1  8  2  7  3  6  4  5
	       │     │     │     │  
	       │     │     │     │
	       ╰┄┄┄┄┄╩┄┄┄┄┄╩┄┄┄┄┄┘
	                ↆ
	    Alternate increasing order
	*/
	task.combinations(9);
	/*
	    Given k : -9
	    ---------------------------------
	    (-1 - -9) : 8
	    (-9 - -2) : -7
	    (-2 - -8) : 6
	    (-8 - -3) : -5
	    (-3 - -7) : 4
	    (-7 - -4) : -3
	    (-4 - -6) : 2
	    (-6 - -5) : -1
	    ------------------------------------
	    -1  -9  -2  -8  -3  -7  -4  -6  -5
	*/
	task.combinations(-9);
	/*
	    Given k : 5
	    -------------------
	    (5 - 1) : 4
	    (1 - 4) : -3
	    (4 - 2) : 2
	    (2 - 3) : -1
	    -------------------
	    5  1  4  2  3
	*/
	task.combinations(5);
}

Output

 Given k : 9
 9 1 8 2 7 3 6 4 5
 Given k : -9
 -1 -9 -2 -8 -3 -7 -4 -6 -5
 Given k : 5
 5 1 4 2 3

Comment

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