Posted on by Kalkicode
Code Backtracking

# Print all intervals of given number

In this article, we will discuss a problem that involves printing all intervals of a given number. We will provide a detailed explanation of the problem statement, present a suitable example, and describe an algorithm to solve it. Furthermore, we will analyze the time complexity of the algorithm. Finally, we will explain the resultant output and provide a step-by-step explanation of the code.

## Problem Statement

The problem is to print all intervals of a given number. An interval is a sequence of numbers where the given number appears twice, with a specific distance between the two occurrences. For example, if the given number is 4, the intervals could be [4 1 3 1], [2 4 3 2], etc. The goal is to find and print all possible intervals for a given number.

## Example

Let's take the example of finding intervals for the number 4. The expected output for this example would be:

```Intervals size 4
4 1 3 1 2 4 3 2
2 3 4 2 1 3 1 4
```

## Pseudocode Algorithm

To solve this problem, we can use a recursive approach. Here is the pseudocode algorithm for finding and printing all intervals of a given number:

``````function display(auxiliary[], size):
for i = 0 to size:
print auxiliary[i]
end for
print a newline
end function

function findIntervals(auxiliary[], element, size, n):
if element > n:
display(auxiliary, size)
return
end if
for i = 0 to size:
if auxiliary[i] == 0 and i + element + 1 < size and auxiliary[i + element + 1] == 0:
auxiliary[i] = element
auxiliary[i + element + 1] = element
findIntervals(auxiliary, element + 1, size, n)
auxiliary[i] = 0
auxiliary[i + element + 1] = 0
end if
end for
end function

function printIntervals(intervals):
if intervals <= 1:
return
end if
print "Intervals size " + intervals
auxiliary[intervals * 2]

for i = 0 to intervals * 2:
auxiliary[i] = 0
end for

findIntervals(auxiliary, 1, intervals * 2, intervals)
print a newline
end function

main():
printIntervals(4)
printIntervals(7)
``````

## Code Solution

``````// C Program
// Print all intervals of given number
#include <stdio.h>

//  Display calculated intervals
void display(int auxiliary[], int size)
{
for (int i = 0; i < size; ++i)
{
printf(" %d", auxiliary[i]);
}
printf("\n");
}
// Find the elements from 1 to n in specific intervals
void findIntervals(int auxiliary[], int element, int size, int n)
{
if (element > n)
{
// Display calculated result
display(auxiliary, size);
return;
}
for (int i = 0; i < size; ++i)
{
if (auxiliary[i] == 0 && i + element + 1 < size && auxiliary[i + element + 1] == 0)
{
// Insert element on particular distance
auxiliary[i] = element;
auxiliary[i + element + 1] = element;
findIntervals(auxiliary, element + 1, size, n);
// Reset value
auxiliary[i] = 0;
auxiliary[i + element + 1] = 0;
}
}
}
// Handles the request of printing number intervals
void printIntervals(int intervals)
{
if (intervals <= 1)
{
return;
}
printf("Intervals size %d \n", intervals);
// Used to store results
int auxiliary[intervals *2];
for (int i = 0; i < intervals *2; ++i)
{
auxiliary[i] = 0;
}
//  print all intervals
findIntervals(auxiliary, 1, intervals *2, intervals);
printf("\n");
}
int main(int argc, char
const *argv[])
{
// Test case
printIntervals(4);
printIntervals(7);
return 0;
}``````

#### Output

``````Intervals size 4
4 1 3 1 2 4 3 2
2 3 4 2 1 3 1 4

Intervals size 7
1 7 1 2 5 6 2 3 4 7 5 3 6 4
1 7 1 2 6 4 2 5 3 7 4 6 3 5
1 6 1 7 2 4 5 2 6 3 4 7 5 3
1 5 1 6 7 2 4 5 2 3 6 4 7 3
1 4 1 5 6 7 4 2 3 5 2 6 3 7
1 4 1 6 7 3 4 5 2 3 6 2 7 5
1 6 1 3 5 7 4 3 6 2 5 4 2 7
1 5 1 7 3 4 6 5 3 2 4 7 2 6
1 5 1 6 3 7 4 5 3 2 6 4 2 7
1 5 1 4 6 7 3 5 4 2 3 6 2 7
5 1 7 1 6 2 5 4 2 3 7 6 4 3
4 1 7 1 6 4 2 5 3 2 7 6 3 5
4 1 6 1 7 4 3 5 2 6 3 2 7 5
7 1 3 1 6 4 3 5 7 2 4 6 2 5
7 1 4 1 6 3 5 4 7 3 2 6 5 2
6 1 5 1 7 3 4 6 5 3 2 4 7 2
4 6 1 7 1 4 5 2 6 3 2 7 5 3
7 3 1 6 1 3 4 5 7 2 6 4 2 5
4 6 1 7 1 4 3 5 6 2 3 7 2 5
5 6 1 7 1 3 5 4 6 3 2 7 4 2
7 4 1 5 1 6 4 3 7 5 2 3 6 2
5 7 1 4 1 6 5 3 4 7 2 3 6 2
3 6 7 1 3 1 4 5 6 2 7 4 2 5
5 7 4 1 6 1 5 4 3 7 2 6 3 2
2 6 7 2 1 5 1 4 6 3 7 5 4 3
4 5 6 7 1 4 1 5 3 6 2 7 3 2
2 3 7 2 6 3 5 1 4 1 7 6 5 4
3 4 5 7 3 6 4 1 5 1 2 7 6 2
2 3 6 2 7 3 4 5 1 6 1 4 7 5
5 2 4 7 2 6 5 4 1 3 1 7 6 3
2 6 3 2 7 4 3 5 6 1 4 1 7 5
2 6 3 2 5 7 3 4 6 1 5 1 4 7
2 4 7 2 3 6 4 5 3 1 7 1 6 5
5 2 7 3 2 6 5 3 4 1 7 1 6 4
5 2 4 6 2 7 5 4 3 1 6 1 3 7
3 5 7 2 3 6 2 5 4 1 7 1 6 4
2 7 4 2 3 5 6 4 3 7 1 5 1 6
2 5 6 2 3 7 4 5 3 6 1 4 1 7
5 2 6 4 2 7 5 3 4 6 1 3 1 7
5 7 2 3 6 2 5 3 4 7 1 6 1 4
5 3 6 7 2 3 5 2 4 6 1 7 1 4
3 4 6 7 3 2 4 5 2 6 1 7 1 5
7 2 6 3 2 4 5 3 7 6 4 1 5 1
7 2 4 6 2 3 5 4 7 3 6 1 5 1
6 2 7 4 2 3 5 6 4 3 7 1 5 1
7 2 4 5 2 6 3 4 7 5 3 1 6 1
5 7 2 6 3 2 5 4 3 7 6 1 4 1
7 3 6 2 5 3 2 4 7 6 5 1 4 1
3 7 4 6 3 2 5 4 2 7 6 1 5 1
3 5 7 4 3 6 2 5 4 2 7 1 6 1
5 3 6 4 7 3 5 2 4 6 2 1 7 1
4 6 3 5 7 4 3 2 6 5 2 1 7 1
``````
``````/*
Java Program
Print all intervals of given number
*/
class Interval
{
//  Display calculated intervals
public void display(int[] auxiliary, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" " + auxiliary[i]);
}
System.out.print("\n");
}
// Find the elements from 1 to n in specific intervals
public void findIntervals(int[] auxiliary, int element, int size, int n)
{
if (element > n)
{
// Display calculated result
display(auxiliary, size);
return;
}
for (int i = 0; i < size; ++i)
{
if (auxiliary[i] == 0 && i + element + 1 < size && auxiliary[i + element + 1] == 0)
{
// Insert element on particular distance
auxiliary[i] = element;
auxiliary[i + element + 1] = element;
findIntervals(auxiliary, element + 1, size, n);
// Reset value
auxiliary[i] = 0;
auxiliary[i + element + 1] = 0;
}
}
}
// Handles the request of printing number intervals
public void printIntervals(int intervals)
{
if (intervals <= 1)
{
return;
}
System.out.print(" Intervals size " + intervals + " \n");
// Used to store results
int[] auxiliary = new int[intervals * 2];
for (int i = 0; i < intervals * 2; ++i)
{
auxiliary[i] = 0;
}
//  print all intervals
findIntervals(auxiliary, 1, intervals * 2, intervals);
System.out.print("\n");
}
public static void main(String[] args)
{
// Test case
}
}``````

#### Output

`````` Intervals size 4
4 1 3 1 2 4 3 2
2 3 4 2 1 3 1 4

Intervals size 7
1 7 1 2 5 6 2 3 4 7 5 3 6 4
1 7 1 2 6 4 2 5 3 7 4 6 3 5
1 6 1 7 2 4 5 2 6 3 4 7 5 3
1 5 1 6 7 2 4 5 2 3 6 4 7 3
1 4 1 5 6 7 4 2 3 5 2 6 3 7
1 4 1 6 7 3 4 5 2 3 6 2 7 5
1 6 1 3 5 7 4 3 6 2 5 4 2 7
1 5 1 7 3 4 6 5 3 2 4 7 2 6
1 5 1 6 3 7 4 5 3 2 6 4 2 7
1 5 1 4 6 7 3 5 4 2 3 6 2 7
5 1 7 1 6 2 5 4 2 3 7 6 4 3
4 1 7 1 6 4 2 5 3 2 7 6 3 5
4 1 6 1 7 4 3 5 2 6 3 2 7 5
7 1 3 1 6 4 3 5 7 2 4 6 2 5
7 1 4 1 6 3 5 4 7 3 2 6 5 2
6 1 5 1 7 3 4 6 5 3 2 4 7 2
4 6 1 7 1 4 5 2 6 3 2 7 5 3
7 3 1 6 1 3 4 5 7 2 6 4 2 5
4 6 1 7 1 4 3 5 6 2 3 7 2 5
5 6 1 7 1 3 5 4 6 3 2 7 4 2
7 4 1 5 1 6 4 3 7 5 2 3 6 2
5 7 1 4 1 6 5 3 4 7 2 3 6 2
3 6 7 1 3 1 4 5 6 2 7 4 2 5
5 7 4 1 6 1 5 4 3 7 2 6 3 2
2 6 7 2 1 5 1 4 6 3 7 5 4 3
4 5 6 7 1 4 1 5 3 6 2 7 3 2
2 3 7 2 6 3 5 1 4 1 7 6 5 4
3 4 5 7 3 6 4 1 5 1 2 7 6 2
2 3 6 2 7 3 4 5 1 6 1 4 7 5
5 2 4 7 2 6 5 4 1 3 1 7 6 3
2 6 3 2 7 4 3 5 6 1 4 1 7 5
2 6 3 2 5 7 3 4 6 1 5 1 4 7
2 4 7 2 3 6 4 5 3 1 7 1 6 5
5 2 7 3 2 6 5 3 4 1 7 1 6 4
5 2 4 6 2 7 5 4 3 1 6 1 3 7
3 5 7 2 3 6 2 5 4 1 7 1 6 4
2 7 4 2 3 5 6 4 3 7 1 5 1 6
2 5 6 2 3 7 4 5 3 6 1 4 1 7
5 2 6 4 2 7 5 3 4 6 1 3 1 7
5 7 2 3 6 2 5 3 4 7 1 6 1 4
5 3 6 7 2 3 5 2 4 6 1 7 1 4
3 4 6 7 3 2 4 5 2 6 1 7 1 5
7 2 6 3 2 4 5 3 7 6 4 1 5 1
7 2 4 6 2 3 5 4 7 3 6 1 5 1
6 2 7 4 2 3 5 6 4 3 7 1 5 1
7 2 4 5 2 6 3 4 7 5 3 1 6 1
5 7 2 6 3 2 5 4 3 7 6 1 4 1
7 3 6 2 5 3 2 4 7 6 5 1 4 1
3 7 4 6 3 2 5 4 2 7 6 1 5 1
3 5 7 4 3 6 2 5 4 2 7 1 6 1
5 3 6 4 7 3 5 2 4 6 2 1 7 1
4 6 3 5 7 4 3 2 6 5 2 1 7 1
``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program
Print all intervals of given number
*/
class Interval
{
public:
//  Display calculated intervals
void display(int auxiliary[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << " " << auxiliary[i];
}
cout << "\n";
}
// Find the elements from 1 to n in specific intervals
void findIntervals(int auxiliary[], int element, int size, int n)
{
if (element > n)
{
// Display calculated result
this->display(auxiliary, size);
return;
}
for (int i = 0; i < size; ++i)
{
if (auxiliary[i] == 0 && i + element + 1 < size && auxiliary[i + element + 1] == 0)
{
// Insert element on particular distance
auxiliary[i] = element;
auxiliary[i + element + 1] = element;
this->findIntervals(auxiliary, element + 1, size, n);
// Reset value
auxiliary[i] = 0;
auxiliary[i + element + 1] = 0;
}
}
}
// Handles the request of printing number intervals
void printIntervals(int intervals)
{
if (intervals <= 1)
{
return;
}
cout << " Intervals size " << intervals << " \n";
// Used to store results
int auxiliary[intervals *2];
for (int i = 0; i < intervals *2; ++i)
{
auxiliary[i] = 0;
}
//  print all intervals
this->findIntervals(auxiliary, 1, intervals *2, intervals);
cout << "\n";
}
};
int main()
{
// Test case
return 0;
}``````

#### Output

`````` Intervals size 4
4 1 3 1 2 4 3 2
2 3 4 2 1 3 1 4

Intervals size 7
1 7 1 2 5 6 2 3 4 7 5 3 6 4
1 7 1 2 6 4 2 5 3 7 4 6 3 5
1 6 1 7 2 4 5 2 6 3 4 7 5 3
1 5 1 6 7 2 4 5 2 3 6 4 7 3
1 4 1 5 6 7 4 2 3 5 2 6 3 7
1 4 1 6 7 3 4 5 2 3 6 2 7 5
1 6 1 3 5 7 4 3 6 2 5 4 2 7
1 5 1 7 3 4 6 5 3 2 4 7 2 6
1 5 1 6 3 7 4 5 3 2 6 4 2 7
1 5 1 4 6 7 3 5 4 2 3 6 2 7
5 1 7 1 6 2 5 4 2 3 7 6 4 3
4 1 7 1 6 4 2 5 3 2 7 6 3 5
4 1 6 1 7 4 3 5 2 6 3 2 7 5
7 1 3 1 6 4 3 5 7 2 4 6 2 5
7 1 4 1 6 3 5 4 7 3 2 6 5 2
6 1 5 1 7 3 4 6 5 3 2 4 7 2
4 6 1 7 1 4 5 2 6 3 2 7 5 3
7 3 1 6 1 3 4 5 7 2 6 4 2 5
4 6 1 7 1 4 3 5 6 2 3 7 2 5
5 6 1 7 1 3 5 4 6 3 2 7 4 2
7 4 1 5 1 6 4 3 7 5 2 3 6 2
5 7 1 4 1 6 5 3 4 7 2 3 6 2
3 6 7 1 3 1 4 5 6 2 7 4 2 5
5 7 4 1 6 1 5 4 3 7 2 6 3 2
2 6 7 2 1 5 1 4 6 3 7 5 4 3
4 5 6 7 1 4 1 5 3 6 2 7 3 2
2 3 7 2 6 3 5 1 4 1 7 6 5 4
3 4 5 7 3 6 4 1 5 1 2 7 6 2
2 3 6 2 7 3 4 5 1 6 1 4 7 5
5 2 4 7 2 6 5 4 1 3 1 7 6 3
2 6 3 2 7 4 3 5 6 1 4 1 7 5
2 6 3 2 5 7 3 4 6 1 5 1 4 7
2 4 7 2 3 6 4 5 3 1 7 1 6 5
5 2 7 3 2 6 5 3 4 1 7 1 6 4
5 2 4 6 2 7 5 4 3 1 6 1 3 7
3 5 7 2 3 6 2 5 4 1 7 1 6 4
2 7 4 2 3 5 6 4 3 7 1 5 1 6
2 5 6 2 3 7 4 5 3 6 1 4 1 7
5 2 6 4 2 7 5 3 4 6 1 3 1 7
5 7 2 3 6 2 5 3 4 7 1 6 1 4
5 3 6 7 2 3 5 2 4 6 1 7 1 4
3 4 6 7 3 2 4 5 2 6 1 7 1 5
7 2 6 3 2 4 5 3 7 6 4 1 5 1
7 2 4 6 2 3 5 4 7 3 6 1 5 1
6 2 7 4 2 3 5 6 4 3 7 1 5 1
7 2 4 5 2 6 3 4 7 5 3 1 6 1
5 7 2 6 3 2 5 4 3 7 6 1 4 1
7 3 6 2 5 3 2 4 7 6 5 1 4 1
3 7 4 6 3 2 5 4 2 7 6 1 5 1
3 5 7 4 3 6 2 5 4 2 7 1 6 1
5 3 6 4 7 3 5 2 4 6 2 1 7 1
4 6 3 5 7 4 3 2 6 5 2 1 7 1
``````
``````// Include namespace system
using System;
/*
C# Program
Print all intervals of given number
*/
public class Interval
{
//  Display calculated intervals
public void display(int[] auxiliary, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write(" " + auxiliary[i]);
}
Console.Write("\n");
}
// Find the elements from 1 to n in specific intervals
public void findIntervals(int[] auxiliary, int element, int size, int n)
{
if (element > n)
{
// Display calculated result
display(auxiliary, size);
return;
}
for (int i = 0; i < size; ++i)
{
if (auxiliary[i] == 0 && i + element + 1 < size && auxiliary[i + element + 1] == 0)
{
// Insert element on particular distance
auxiliary[i] = element;
auxiliary[i + element + 1] = element;
findIntervals(auxiliary, element + 1, size, n);
// Reset value
auxiliary[i] = 0;
auxiliary[i + element + 1] = 0;
}
}
}
// Handles the request of printing number intervals
public void printIntervals(int intervals)
{
if (intervals <= 1)
{
return;
}
Console.Write(" Intervals size " + intervals + " \n");
// Used to store results
int[] auxiliary = new int[intervals * 2];
for (int i = 0; i < intervals * 2; ++i)
{
auxiliary[i] = 0;
}
//  print all intervals
findIntervals(auxiliary, 1, intervals * 2, intervals);
Console.Write("\n");
}
public static void Main(String[] args)
{
// Test case
}
}``````

#### Output

`````` Intervals size 4
4 1 3 1 2 4 3 2
2 3 4 2 1 3 1 4

Intervals size 7
1 7 1 2 5 6 2 3 4 7 5 3 6 4
1 7 1 2 6 4 2 5 3 7 4 6 3 5
1 6 1 7 2 4 5 2 6 3 4 7 5 3
1 5 1 6 7 2 4 5 2 3 6 4 7 3
1 4 1 5 6 7 4 2 3 5 2 6 3 7
1 4 1 6 7 3 4 5 2 3 6 2 7 5
1 6 1 3 5 7 4 3 6 2 5 4 2 7
1 5 1 7 3 4 6 5 3 2 4 7 2 6
1 5 1 6 3 7 4 5 3 2 6 4 2 7
1 5 1 4 6 7 3 5 4 2 3 6 2 7
5 1 7 1 6 2 5 4 2 3 7 6 4 3
4 1 7 1 6 4 2 5 3 2 7 6 3 5
4 1 6 1 7 4 3 5 2 6 3 2 7 5
7 1 3 1 6 4 3 5 7 2 4 6 2 5
7 1 4 1 6 3 5 4 7 3 2 6 5 2
6 1 5 1 7 3 4 6 5 3 2 4 7 2
4 6 1 7 1 4 5 2 6 3 2 7 5 3
7 3 1 6 1 3 4 5 7 2 6 4 2 5
4 6 1 7 1 4 3 5 6 2 3 7 2 5
5 6 1 7 1 3 5 4 6 3 2 7 4 2
7 4 1 5 1 6 4 3 7 5 2 3 6 2
5 7 1 4 1 6 5 3 4 7 2 3 6 2
3 6 7 1 3 1 4 5 6 2 7 4 2 5
5 7 4 1 6 1 5 4 3 7 2 6 3 2
2 6 7 2 1 5 1 4 6 3 7 5 4 3
4 5 6 7 1 4 1 5 3 6 2 7 3 2
2 3 7 2 6 3 5 1 4 1 7 6 5 4
3 4 5 7 3 6 4 1 5 1 2 7 6 2
2 3 6 2 7 3 4 5 1 6 1 4 7 5
5 2 4 7 2 6 5 4 1 3 1 7 6 3
2 6 3 2 7 4 3 5 6 1 4 1 7 5
2 6 3 2 5 7 3 4 6 1 5 1 4 7
2 4 7 2 3 6 4 5 3 1 7 1 6 5
5 2 7 3 2 6 5 3 4 1 7 1 6 4
5 2 4 6 2 7 5 4 3 1 6 1 3 7
3 5 7 2 3 6 2 5 4 1 7 1 6 4
2 7 4 2 3 5 6 4 3 7 1 5 1 6
2 5 6 2 3 7 4 5 3 6 1 4 1 7
5 2 6 4 2 7 5 3 4 6 1 3 1 7
5 7 2 3 6 2 5 3 4 7 1 6 1 4
5 3 6 7 2 3 5 2 4 6 1 7 1 4
3 4 6 7 3 2 4 5 2 6 1 7 1 5
7 2 6 3 2 4 5 3 7 6 4 1 5 1
7 2 4 6 2 3 5 4 7 3 6 1 5 1
6 2 7 4 2 3 5 6 4 3 7 1 5 1
7 2 4 5 2 6 3 4 7 5 3 1 6 1
5 7 2 6 3 2 5 4 3 7 6 1 4 1
7 3 6 2 5 3 2 4 7 6 5 1 4 1
3 7 4 6 3 2 5 4 2 7 6 1 5 1
3 5 7 4 3 6 2 5 4 2 7 1 6 1
5 3 6 4 7 3 5 2 4 6 2 1 7 1
4 6 3 5 7 4 3 2 6 5 2 1 7 1
``````
``````<?php
/*
Php Program
Print all intervals of given number
*/
class Interval
{
//  Display calculated intervals
public	function display( & \$auxiliary, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo " ". \$auxiliary[\$i];
}
echo "\n";
}
// Find the elements from 1 to n in specific intervals
public	function findIntervals( & \$auxiliary, \$element, \$size, \$n)
{
if (\$element > \$n)
{
// Display calculated result
\$this->display(\$auxiliary, \$size);
return;
}
for (\$i = 0; \$i < \$size; ++\$i)
{
if (\$auxiliary[\$i] == 0 && \$i + \$element + 1 < \$size && \$auxiliary[\$i + \$element + 1] == 0)
{
// Insert element on particular distance
\$auxiliary[\$i] = \$element;
\$auxiliary[\$i + \$element + 1] = \$element;
\$this->findIntervals(\$auxiliary, \$element + 1, \$size, \$n);
// Reset value
\$auxiliary[\$i] = 0;
\$auxiliary[\$i + \$element + 1] = 0;
}
}
}
// Handles the request of printing number intervals
public	function printIntervals(\$intervals)
{
if (\$intervals <= 1)
{
return;
}
echo " Intervals size ". \$intervals ." \n";
// Used to store results
\$auxiliary = array_fill(0, \$intervals * 2, 0);
for (\$i = 0; \$i < \$intervals * 2; ++\$i)
{
\$auxiliary[\$i] = 0;
}
//  print all intervals
\$this->findIntervals(\$auxiliary, 1, \$intervals * 2, \$intervals);
echo "\n";
}
}

function main()
{
// Test case
}
main();``````

#### Output

`````` Intervals size 4
4 1 3 1 2 4 3 2
2 3 4 2 1 3 1 4

Intervals size 7
1 7 1 2 5 6 2 3 4 7 5 3 6 4
1 7 1 2 6 4 2 5 3 7 4 6 3 5
1 6 1 7 2 4 5 2 6 3 4 7 5 3
1 5 1 6 7 2 4 5 2 3 6 4 7 3
1 4 1 5 6 7 4 2 3 5 2 6 3 7
1 4 1 6 7 3 4 5 2 3 6 2 7 5
1 6 1 3 5 7 4 3 6 2 5 4 2 7
1 5 1 7 3 4 6 5 3 2 4 7 2 6
1 5 1 6 3 7 4 5 3 2 6 4 2 7
1 5 1 4 6 7 3 5 4 2 3 6 2 7
5 1 7 1 6 2 5 4 2 3 7 6 4 3
4 1 7 1 6 4 2 5 3 2 7 6 3 5
4 1 6 1 7 4 3 5 2 6 3 2 7 5
7 1 3 1 6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 1
5 3 6 4 7 3 5 2 4 6 2 1 7 1
4 6 3 5 7 4 3 2 6 5 2 1 7 1
``````
``````/*
Node Js Program
Print all intervals of given number
*/
class Interval
{
//  Display calculated intervals
display(auxiliary, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + auxiliary[i]);
}
process.stdout.write("\n");
}
// Find the elements from 1 to n in specific intervals
findIntervals(auxiliary, element, size, n)
{
if (element > n)
{
// Display calculated result
this.display(auxiliary, size);
return;
}
for (var i = 0; i < size; ++i)
{
if (auxiliary[i] == 0 && i + element + 1 < size && auxiliary[i + element + 1] == 0)
{
// Insert element on particular distance
auxiliary[i] = element;
auxiliary[i + element + 1] = element;
this.findIntervals(auxiliary, element + 1, size, n);
// Reset value
auxiliary[i] = 0;
auxiliary[i + element + 1] = 0;
}
}
}
// Handles the request of printing number intervals
printIntervals(intervals)
{
if (intervals <= 1)
{
return;
}
process.stdout.write(" Intervals size " + intervals + " \n");
// Used to store results
var auxiliary = Array(intervals * 2).fill(0);
for (var i = 0; i < intervals * 2; ++i)
{
auxiliary[i] = 0;
}
//  print all intervals
this.findIntervals(auxiliary, 1, intervals * 2, intervals);
process.stdout.write("\n");
}
}

function main()
{
// Test case
}
main();``````

#### Output

`````` Intervals size 4
4 1 3 1 2 4 3 2
2 3 4 2 1 3 1 4

Intervals size 7
1 7 1 2 5 6 2 3 4 7 5 3 6 4
1 7 1 2 6 4 2 5 3 7 4 6 3 5
1 6 1 7 2 4 5 2 6 3 4 7 5 3
1 5 1 6 7 2 4 5 2 3 6 4 7 3
1 4 1 5 6 7 4 2 3 5 2 6 3 7
1 4 1 6 7 3 4 5 2 3 6 2 7 5
1 6 1 3 5 7 4 3 6 2 5 4 2 7
1 5 1 7 3 4 6 5 3 2 4 7 2 6
1 5 1 6 3 7 4 5 3 2 6 4 2 7
1 5 1 4 6 7 3 5 4 2 3 6 2 7
5 1 7 1 6 2 5 4 2 3 7 6 4 3
4 1 7 1 6 4 2 5 3 2 7 6 3 5
4 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7 6 1 5 1
3 5 7 4 3 6 2 5 4 2 7 1 6 1
5 3 6 4 7 3 5 2 4 6 2 1 7 1
4 6 3 5 7 4 3 2 6 5 2 1 7 1
``````
``````#    Python 3 Program
#    Print all intervals of given number

class Interval :
#   Display calculated intervals
def display(self, auxiliary, size) :
i = 0
while (i < size) :
print(" ", auxiliary[i], end = "")
i += 1

print(end = "\n")

#  Find the elements from 1 to n in specific intervals
def findIntervals(self, auxiliary, element, size, n) :
if (element > n) :
#  Display calculated result
self.display(auxiliary, size)
return

i = 0
while (i < size) :
if (auxiliary[i] == 0 and i + element + 1 < size and auxiliary[i + element + 1] == 0) :
#  Insert element on particular distance
auxiliary[i] = element
auxiliary[i + element + 1] = element
self.findIntervals(auxiliary, element + 1, size, n)
#  Reset value
auxiliary[i] = 0
auxiliary[i + element + 1] = 0

i += 1

#  Handles the request of printing number intervals
def printIntervals(self, intervals) :
if (intervals <= 1) :
return

print(" Intervals size ", intervals ," ")
#  Used to store results
auxiliary = [0] * (intervals * 2)
#   print all intervals
self.findIntervals(auxiliary, 1, intervals * 2, intervals)
print(end = "\n")

def main() :
#  Test case

if __name__ == "__main__": main()``````

#### Output

`````` Intervals size  4
4  1  3  1  2  4  3  2
2  3  4  2  1  3  1  4

Intervals size  7
1  7  1  2  5  6  2  3  4  7  5  3  6  4
1  7  1  2  6  4  2  5  3  7  4  6  3  5
1  6  1  7  2  4  5  2  6  3  4  7  5  3
1  5  1  6  7  2  4  5  2  3  6  4  7  3
1  4  1  5  6  7  4  2  3  5  2  6  3  7
1  4  1  6  7  3  4  5  2  3  6  2  7  5
1  6  1  3  5  7  4  3  6  2  5  4  2  7
1  5  1  7  3  4  6  5  3  2  4  7  2  6
1  5  1  6  3  7  4  5  3  2  6  4  2  7
1  5  1  4  6  7  3  5  4  2  3  6  2  7
5  1  7  1  6  2  5  4  2  3  7  6  4  3
4  1  7  1  6  4  2  5  3  2  7  6  3  5
4  1  6  1  7  4  3  5  2  6  3  2  7  5
7  1  3  1  6  4  3  5  7  2  4  6  2  5
7  1  4  1  6  3  5  4  7  3  2  6  5  2
6  1  5  1  7  3  4  6  5  3  2  4  7  2
4  6  1  7  1  4  5  2  6  3  2  7  5  3
7  3  1  6  1  3  4  5  7  2  6  4  2  5
4  6  1  7  1  4  3  5  6  2  3  7  2  5
5  6  1  7  1  3  5  4  6  3  2  7  4  2
7  4  1  5  1  6  4  3  7  5  2  3  6  2
5  7  1  4  1  6  5  3  4  7  2  3  6  2
3  6  7  1  3  1  4  5  6  2  7  4  2  5
5  7  4  1  6  1  5  4  3  7  2  6  3  2
2  6  7  2  1  5  1  4  6  3  7  5  4  3
4  5  6  7  1  4  1  5  3  6  2  7  3  2
2  3  7  2  6  3  5  1  4  1  7  6  5  4
3  4  5  7  3  6  4  1  5  1  2  7  6  2
2  3  6  2  7  3  4  5  1  6  1  4  7  5
5  2  4  7  2  6  5  4  1  3  1  7  6  3
2  6  3  2  7  4  3  5  6  1  4  1  7  5
2  6  3  2  5  7  3  4  6  1  5  1  4  7
2  4  7  2  3  6  4  5  3  1  7  1  6  5
5  2  7  3  2  6  5  3  4  1  7  1  6  4
5  2  4  6  2  7  5  4  3  1  6  1  3  7
3  5  7  2  3  6  2  5  4  1  7  1  6  4
2  7  4  2  3  5  6  4  3  7  1  5  1  6
2  5  6  2  3  7  4  5  3  6  1  4  1  7
5  2  6  4  2  7  5  3  4  6  1  3  1  7
5  7  2  3  6  2  5  3  4  7  1  6  1  4
5  3  6  7  2  3  5  2  4  6  1  7  1  4
3  4  6  7  3  2  4  5  2  6  1  7  1  5
7  2  6  3  2  4  5  3  7  6  4  1  5  1
7  2  4  6  2  3  5  4  7  3  6  1  5  1
6  2  7  4  2  3  5  6  4  3  7  1  5  1
7  2  4  5  2  6  3  4  7  5  3  1  6  1
5  7  2  6  3  2  5  4  3  7  6  1  4  1
7  3  6  2  5  3  2  4  7  6  5  1  4  1
3  7  4  6  3  2  5  4  2  7  6  1  5  1
3  5  7  4  3  6  2  5  4  2  7  1  6  1
5  3  6  4  7  3  5  2  4  6  2  1  7  1
4  6  3  5  7  4  3  2  6  5  2  1  7  1
``````
``````#    Ruby Program
#    Print all intervals of given number

class Interval
#   Display calculated intervals
def display(auxiliary, size)
i = 0
while (i < size)
print(" ", auxiliary[i])
i += 1
end

print("\n")
end

#  Find the elements from 1 to n in specific intervals
def findIntervals(auxiliary, element, size, n)
if (element > n)
#  Display calculated result
self.display(auxiliary, size)
return
end

i = 0
while (i < size)
if (auxiliary[i] == 0 && i + element + 1 < size && auxiliary[i + element + 1] == 0)
#  Insert element on particular distance
auxiliary[i] = element
auxiliary[i + element + 1] = element
self.findIntervals(auxiliary, element + 1, size, n)
#  Reset value
auxiliary[i] = 0
auxiliary[i + element + 1] = 0
end

i += 1
end

end

#  Handles the request of printing number intervals
def printIntervals(intervals)
if (intervals <= 1)
return
end

print(" Intervals size ", intervals ," \n")
#  Used to store results
auxiliary = Array.new(intervals * 2) {0}
#   print all intervals
self.findIntervals(auxiliary, 1, intervals * 2, intervals)
print("\n")
end

end

def main()
#  Test case
end

main()``````

#### Output

`````` Intervals size 4
4 1 3 1 2 4 3 2
2 3 4 2 1 3 1 4

Intervals size 7
1 7 1 2 5 6 2 3 4 7 5 3 6 4
1 7 1 2 6 4 2 5 3 7 4 6 3 5
1 6 1 7 2 4 5 2 6 3 4 7 5 3
1 5 1 6 7 2 4 5 2 3 6 4 7 3
1 4 1 5 6 7 4 2 3 5 2 6 3 7
1 4 1 6 7 3 4 5 2 3 6 2 7 5
1 6 1 3 5 7 4 3 6 2 5 4 2 7
1 5 1 7 3 4 6 5 3 2 4 7 2 6
1 5 1 6 3 7 4 5 3 2 6 4 2 7
1 5 1 4 6 7 3 5 4 2 3 6 2 7
5 1 7 1 6 2 5 4 2 3 7 6 4 3
4 1 7 1 6 4 2 5 3 2 7 6 3 5
4 1 6 1 7 4 3 5 2 6 3 2 7 5
7 1 3 1 6 4 3 5 7 2 4 6 2 5
7 1 4 1 6 3 5 4 7 3 2 6 5 2
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6 5 2 1 7 1

``````
``````/*
Scala Program
Print all intervals of given number
*/
class Interval
{
//  Display calculated intervals
def display(auxiliary: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" " + auxiliary(i));
i += 1;
}
print("\n");
}
// Find the elements from 1 to n in specific intervals
def findIntervals(auxiliary: Array[Int], element: Int, size: Int, n: Int): Unit = {
if (element > n)
{
// Display calculated result
this.display(auxiliary, size);
return;
}
var i: Int = 0;
while (i < size)
{
if (auxiliary(i) == 0 && i + element + 1 < size && auxiliary(i + element + 1) == 0)
{
// Insert element on particular distance
auxiliary(i) = element;
auxiliary(i + element + 1) = element;
this.findIntervals(auxiliary, element + 1, size, n);
// Reset value
auxiliary(i) = 0;
auxiliary(i + element + 1) = 0;
}
i += 1;
}
}
// Handles the request of printing number intervals
def printIntervals(intervals: Int): Unit = {
if (intervals <= 1)
{
return;
}
print(" Intervals size " + intervals + " \n");
// Used to store results
var auxiliary: Array[Int] = Array.fill[Int](intervals * 2)(0);
//  print all intervals
this.findIntervals(auxiliary, 1, intervals * 2, intervals);
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Interval = new Interval();
// Test case
}
}``````

#### Output

`````` Intervals size 4
4 1 3 1 2 4 3 2
2 3 4 2 1 3 1 4

Intervals size 7
1 7 1 2 5 6 2 3 4 7 5 3 6 4
1 7 1 2 6 4 2 5 3 7 4 6 3 5
1 6 1 7 2 4 5 2 6 3 4 7 5 3
1 5 1 6 7 2 4 5 2 3 6 4 7 3
1 4 1 5 6 7 4 2 3 5 2 6 3 7
1 4 1 6 7 3 4 5 2 3 6 2 7 5
1 6 1 3 5 7 4 3 6 2 5 4 2 7
1 5 1 7 3 4 6 5 3 2 4 7 2 6
1 5 1 6 3 7 4 5 3 2 6 4 2 7
1 5 1 4 6 7 3 5 4 2 3 6 2 7
5 1 7 1 6 2 5 4 2 3 7 6 4 3
4 1 7 1 6 4 2 5 3 2 7 6 3 5
4 1 6 1 7 4 3 5 2 6 3 2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 2 5 4 2 7 1 6 1
5 3 6 4 7 3 5 2 4 6 2 1 7 1
4 6 3 5 7 4 3 2 6 5 2 1 7 1
``````
``````/*
Swift 4 Program
Print all intervals of given number
*/
class Interval
{
//  Display calculated intervals
func display(_ auxiliary: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", auxiliary[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// Find the elements from 1 to n in specific intervals
func findIntervals(_ auxiliary: inout[Int], _ element: Int, _ size: Int, _ n: Int)
{
if (element > n)
{
// Display calculated result
self.display(auxiliary, size);
return;
}
var i: Int = 0;
while (i < size)
{
if (auxiliary[i] == 0 && i + element + 1 < size && auxiliary[i + element + 1] == 0)
{
// Insert element on particular distance
auxiliary[i] = element;
auxiliary[i + element + 1] = element;
self.findIntervals(&auxiliary, element + 1, size, n);
// Reset value
auxiliary[i] = 0;
auxiliary[i + element + 1] = 0;
}
i += 1;
}
}
// Handles the request of printing number intervals
func printIntervals(_ intervals: Int)
{
if (intervals <= 1)
{
return;
}
print(" Intervals size ", intervals ," ");
// Used to store results
var auxiliary: [Int] = Array(repeating: 0, count: intervals * 2);
//  print all intervals
self.findIntervals(&auxiliary, 1, intervals * 2, intervals);
print(terminator: "\n");
}
}
func main()
{
// Test case
}
main();``````

#### Output

`````` Intervals size  4
4  1  3  1  2  4  3  2
2  3  4  2  1  3  1  4

Intervals size  7
1  7  1  2  5  6  2  3  4  7  5  3  6  4
1  7  1  2  6  4  2  5  3  7  4  6  3  5
1  6  1  7  2  4  5  2  6  3  4  7  5  3
1  5  1  6  7  2  4  5  2  3  6  4  7  3
1  4  1  5  6  7  4  2  3  5  2  6  3  7
1  4  1  6  7  3  4  5  2  3  6  2  7  5
1  6  1  3  5  7  4  3  6  2  5  4  2  7
1  5  1  7  3  4  6  5  3  2  4  7  2  6
1  5  1  6  3  7  4  5  3  2  6  4  2  7
1  5  1  4  6  7  3  5  4  2  3  6  2  7
5  1  7  1  6  2  5  4  2  3  7  6  4  3
4  1  7  1  6  4  2  5  3  2  7  6  3  5
4  1  6  1  7  4  3  5  2  6  3  2  7  5
7  1  3  1  6  4  3  5  7  2  4  6  2  5
7  1  4  1  6  3  5  4  7  3  2  6  5  2
6  1  5  1  7  3  4  6  5  3  2  4  7  2
4  6  1  7  1  4  5  2  6  3  2  7  5  3
7  3  1  6  1  3  4  5  7  2  6  4  2  5
4  6  1  7  1  4  3  5  6  2  3  7  2  5
5  6  1  7  1  3  5  4  6  3  2  7  4  2
7  4  1  5  1  6  4  3  7  5  2  3  6  2
5  7  1  4  1  6  5  3  4  7  2  3  6  2
3  6  7  1  3  1  4  5  6  2  7  4  2  5
5  7  4  1  6  1  5  4  3  7  2  6  3  2
2  6  7  2  1  5  1  4  6  3  7  5  4  3
4  5  6  7  1  4  1  5  3  6  2  7  3  2
2  3  7  2  6  3  5  1  4  1  7  6  5  4
3  4  5  7  3  6  4  1  5  1  2  7  6  2
2  3  6  2  7  3  4  5  1  6  1  4  7  5
5  2  4  7  2  6  5  4  1  3  1  7  6  3
2  6  3  2  7  4  3  5  6  1  4  1  7  5
2  6  3  2  5  7  3  4  6  1  5  1  4  7
2  4  7  2  3  6  4  5  3  1  7  1  6  5
5  2  7  3  2  6  5  3  4  1  7  1  6  4
5  2  4  6  2  7  5  4  3  1  6  1  3  7
3  5  7  2  3  6  2  5  4  1  7  1  6  4
2  7  4  2  3  5  6  4  3  7  1  5  1  6
2  5  6  2  3  7  4  5  3  6  1  4  1  7
5  2  6  4  2  7  5  3  4  6  1  3  1  7
5  7  2  3  6  2  5  3  4  7  1  6  1  4
5  3  6  7  2  3  5  2  4  6  1  7  1  4
3  4  6  7  3  2  4  5  2  6  1  7  1  5
7  2  6  3  2  4  5  3  7  6  4  1  5  1
7  2  4  6  2  3  5  4  7  3  6  1  5  1
6  2  7  4  2  3  5  6  4  3  7  1  5  1
7  2  4  5  2  6  3  4  7  5  3  1  6  1
5  7  2  6  3  2  5  4  3  7  6  1  4  1
7  3  6  2  5  3  2  4  7  6  5  1  4  1
3  7  4  6  3  2  5  4  2  7  6  1  5  1
3  5  7  4  3  6  2  5  4  2  7  1  6  1
5  3  6  4  7  3  5  2  4  6  2  1  7  1
4  6  3  5  7  4  3  2  6  5  2  1  7  1
``````
``````/*
Kotlin Program
Print all intervals of given number
*/
class Interval
{
//  Display calculated intervals
fun display(auxiliary: Array<Int> , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print(" " + auxiliary[i]);
i += 1;
}
print("\n");
}
// Find the elements from 1 to n in specific intervals
fun findIntervals(auxiliary: Array<Int> , element: Int, size: Int, n: Int): Unit
{
if (element > n)
{
// Display calculated result
this.display(auxiliary, size);
return;
}
var i: Int = 0;
while (i < size)
{
if (auxiliary[i] == 0 && i + element + 1 < size && auxiliary[i + element + 1] == 0)
{
// Insert element on particular distance
auxiliary[i] = element;
auxiliary[i + element + 1] = element;
this.findIntervals(auxiliary, element + 1, size, n);
// Reset value
auxiliary[i] = 0;
auxiliary[i + element + 1] = 0;
}
i += 1;
}
}
// Handles the request of printing number intervals
fun printIntervals(intervals: Int): Unit
{
if (intervals <= 1)
{
return;
}
print(" Intervals size " + intervals + " \n");
// Used to store results
var auxiliary: Array < Int > = Array(intervals * 2)
{
0
};
//  print all intervals
this.findIntervals(auxiliary, 1, intervals * 2, intervals);
print("\n");
}
}
fun main(args: Array < String > ): Unit
{
// Test case
}``````

#### Output

`````` Intervals size 4
4 1 3 1 2 4 3 2
2 3 4 2 1 3 1 4

Intervals size 7
1 7 1 2 5 6 2 3 4 7 5 3 6 4
1 7 1 2 6 4 2 5 3 7 4 6 3 5
1 6 1 7 2 4 5 2 6 3 4 7 5 3
1 5 1 6 7 2 4 5 2 3 6 4 7 3
1 4 1 5 6 7 4 2 3 5 2 6 3 7
1 4 1 6 7 3 4 5 2 3 6 2 7 5
1 6 1 3 5 7 4 3 6 2 5 4 2 7
1 5 1 7 3 4 6 5 3 2 4 7 2 6
1 5 1 6 3 7 4 5 3 2 6 4 2 7
1 5 1 4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 6 1
5 7 2 6 3 2 5 4 3 7 6 1 4 1
7 3 6 2 5 3 2 4 7 6 5 1 4 1
3 7 4 6 3 2 5 4 2 7 6 1 5 1
3 5 7 4 3 6 2 5 4 2 7 1 6 1
5 3 6 4 7 3 5 2 4 6 2 1 7 1
4 6 3 5 7 4 3 2 6 5 2 1 7 1
``````
h2>Resultant Output Explanation

The program will generate all possible intervals for the given numbers. The output is displayed in the following format: each line represents an interval, and the numbers are separated by spaces. For example, the output "4 1 3 1 2 4 3 2" represents one interval for the number 4.

## Time Complexity

The time complexity of this algorithm depends on the size of the intervals, denoted as 'n'. Since the algorithm uses recursion, the time complexity can be expressed as O(2^n). This is because, in the worst case, the algorithm explores all possible combinations of intervals.

## 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.

Categories
Relative Post