 GFG App
Open App Browser
Continue

# Count ways to form Triplet of consecutive integers having the given numbers

Given an integer N (N ≥ 3), two distinct numbers X and Y, the task is to find the maximum number of possible ways by which a third drawn number (the third number lies in range [1, N]) can make a triplet of consecutive with given two numbers. Also, print the triplets.

Examples:

Input: N = 3, X = 2,  Y = 3
Output:1
1 2 3
Explanation: There is only 1 way to make consecutive series of X and Y with
3rd drawn number which is 1.

Input: N = 5, X = 2, Y = 5
Output: 0
Explanation: There are no possible ways to make form a triplet
of consecutive numbers having 2 and 5 in the triplet.

Approach: To solve the problem follow the below idea:

The problem can be solved by observing absolute difference between X and Y. There will be three cases on the basis of difference between X and Y, Which are explained below.

Observation of Possible cases:

Consider N = 5 for all inputs and X and Y are defined for each case separately.

Case 1: When the absolute difference between X and Y is 1:

There will be three types of cases for the difference equal to 1. In the first case, If Min(X, Y) is equal to 1 and in other cases, When
Max(X, Y) = N.

• When Min(X, Y) = 1 Then there will be only 1 possible case, Let X = 2, Y = 1.So, the consecutive array will be = {1, 2, 3}
• When Max(X, Y) = N Then there will be only 1 possible case, Let X = 5, Y = 4.So,  the consecutive array will be = {3, 4, 5}
• For the rest of the cases: The rest of the cases excluding the above two discussed sub-cases will have two possible ways of making a consecutive array. Let X =2 and Y = 3.Then, there are two consecutive arrays are possible {1, 2, 3} and (2, 3, 4).

Case 2: When the absolute difference between X and Y is 2:

There will be only one possible case. Let X  = 5 and Y = 3, Then only the occurrence of 4 can make a consecutive array = {3, 4, 5} in which the third drawn number will be mean of X and Y.

Case 3: When the absolute difference between X and Y is greater than 2: There will be no possible case by which you can make a consecutive array with X and Y.

Follow the steps to solve the problem:

• Obtain the absolute difference between X and Y.
• If the absolute difference is equal to 2 then the third number will be the mean of X and Y as discussed in Case 2.
• If the absolute difference is equal to 1, Then only for corner cases there will be one possible consecutive array as discussed in Case 1, Rest of the cases will have two consecutive arrays.
• If the absolute difference is greater than 2. Then, there will no possible consecutive array for such cases.

Below is the implementation of the above approach:

## C++

```// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;

void findTriplet(int X, int Y, int N)
{
// Array initialized to hold
// consecutive array numbers
int arr = { 0 };

// Condition when absolute
// difference between X and Y is 2
if (abs(X - Y) == 2) {

// Printing possible number
// of cases
cout << "1\n";
arr = X;
arr = Y;
arr = (X + Y) / 2;

// Printing consecutive
// array elements
cout << arr << "  " << arr << "  " << arr
<< "\n";
}

// Condition when absolute
// difference between X and Y is 1
else if (abs(X - Y) == 1) {

// If one from X and Y is
// equal to 1
if (min(X, Y) == 1) {

// Printing possible
// number of cases
cout << "1\n";
arr = 1;
arr = max(X, Y);
arr = max(X, Y) + 1;

// Printing consecutive
// array elements
cout << arr << "  " << arr << "  "
<< arr << "\n";
}
else if (max(X, Y) == N) {

// Printing possible
// number of cases
cout << "1\n";
arr = min(X, Y) - 1;
arr = min(X, Y);
arr = max(X, Y);

// Printing consecutive
// array elements
cout << arr << " " << arr << " " << arr
<< "\n";
}

// For cases when neither X
// and nor Y is not equal
// to 1 or N.
else {

// Printing possible cases
cout << "2\n";

// Values of consecutive
// numbers for first case
arr = min(X, Y) - 1;
arr = min(X, Y);
arr = max(X, Y);

// Printing consecutive
// array numbers
cout << arr << " " << arr << " " << arr
<< "\n";

// Values of consecutive
// numbers for second case
arr = min(X, Y);
arr = max(X, Y);
arr = max(X, Y) + 1;

// Printing consecutive
// array numbers
cout << arr << " " << arr << " " << arr
<< "\n";
}
}

// When difference between X and Y
// is greater than 2
else {
cout << "0\n";
}
}

// Driver Code
int main()
{
// Input value of N
int N = 3;

// input value of X and Y
int X = 2, Y = 1;
findTriplet(X, Y, N);
return 0;
}

// This code is contributed by Rohit Pradhan```

## Java

```// Java code to implement the approach
import java.io.*;
import java.lang.*;
import java.util.*;

public class GFG {

// Driver function
public static void main(String[] args)
{

// Input value of N
int N = 3;

// input value of X and Y
int X = 2, Y = 1;
findTriplet(X, Y, N);
}

static void findTriplet(int X, int Y, int N)
{
// Array initialized to hold
// consecutive array numbers
int[] arr = new int;

// Condition when absolute
// difference between X and Y is 2
if (Math.abs(X - Y) == 2) {

// Printing possible number
// of cases
System.out.println(1);
arr = X;
arr = Y;
arr = (X + Y) / 2;

// Printing consecutive
// array elements
System.out.println(arr + "  " + arr + "  "
+ arr);
}

// Condition when absolute
// difference between X and Y is 1
else if (Math.abs(X - Y) == 1) {

// If one from X and Y is
// equal to 1
if (Math.min(X, Y) == 1) {

// Printing possible
// number of cases
System.out.println(1);
arr = 1;
arr = Math.max(X, Y);
arr = Math.max(X, Y) + 1;

// Printing consecutive
// array elements
System.out.println(arr + "  " + arr
+ "  " + arr);
}
else if (Math.max(X, Y) == N) {

// Printing possible
// number of cases
System.out.println(1);
arr = Math.min(X, Y) - 1;
arr = Math.min(X, Y);
arr = Math.max(X, Y);

// Printing consecutive
// array elements
System.out.println(arr + " " + arr
+ " " + arr);
}

// For cases when neither X
// and nor Y is not equal
// to 1 or N.
else {

// Printing possible cases
System.out.println(2);

// Values of consecutive
// numbers for first case
arr = Math.min(X, Y) - 1;
arr = Math.min(X, Y);
arr = Math.max(X, Y);

// Printing consecutive
// array numbers
System.out.println(arr + " " + arr
+ " " + arr);

// Values of consecutive
// numbers for second case
arr = Math.min(X, Y);
arr = Math.max(X, Y);
arr = Math.max(X, Y) + 1;

// Printing consecutive
// array numbers
System.out.println(arr + " " + arr
+ " " + arr);
}
}

// When difference between X and Y
// is greater than 2
else {
System.out.println(0);
}
}
}```

## Python3

```# Python code to implement the approach
def findTriplet(X,Y,N):

# Array initialized to hold consecutive array numbers
arr = [0,0,0]

# Condition when absolute difference between X and Y is 2
if(abs(X-Y) == 2):
print(1)

# Printing possible number of cases
arr = X
arr = Y
arr = (X + Y)//2

# Printing consecutive array elements
print(arr,end=' ')
print(arr,end=' ')
print(arr)

# Condition when absolute difference between X and Y is 1
elif(abs(X-Y) == 1):

# If one from X and Y is equal to 1
if(X == 1 or Y == 1):

# Printing possible number of cases
print(1)
arr = 1
if(X > Y):
arr = X
else:
arr = Y
arr = arr + 1

# Printing consecutive array elements
print(arr,end=' ')
print(arr,end=' ')
print(arr)
elif(X == N or Y == N):

# Printing possible number of cases
print(1)
if(X > Y):
arr = Y - 1
else:
arr = X - 1
arr = arr + 1
arr = X + Y - arr

# Printing consecutive array elements
print(arr,end=' ')
print(arr,end=' ')
print(arr)

# For cases when neither X and nor Y is not equal to 1 or N.
else:

# Printing possible cases
print(2)

# Values of consecutive numbers for first case
if(X > Y):
arr = Y - 1
else:
arr = X - 1
arr = arr + 1
arr = X + Y - arr

# Printing consecutive array numbers
print(arr,end = ' ')
print(arr,end = ' ')
print(arr)

# Values of consecutive numbers for second case
if(X > Y):
arr = Y
arr = X
arr = X + 1
else:
arr = X
arr = Y
arr = Y + 1

# Printing consecutive array numbers
print(arr,end=' ')
print(arr,end=' ')
print(arr)

# When difference between X and Y is greater than 2
else:
print(0)

# Driver Code
if __name__ == "__main__":

# Input value of N
N = 3

# input value of X and Y
X = 2
Y = 1
findTriplet(X,Y,N)

# This code is contributed by ajaymakvana.```

## C#

```// C# code to implement the above approach
using System;
public class GFG
{

// Driver function
public static void Main(string[] args)
{

// Input value of N
int N = 3;

// input value of X and Y
int X = 2, Y = 1;
findTriplet(X, Y, N);
}

static void findTriplet(int X, int Y, int N)
{
// Array initialized to hold
// consecutive array numbers
int[] arr = new int;

// Condition when absolute
// difference between X and Y is 2
if (Math.Abs(X - Y) == 2) {

// Printing possible number
// of cases
Console.WriteLine(1);
arr = X;
arr = Y;
arr = (X + Y) / 2;

// Printing consecutive
// array elements
Console.WriteLine(arr + "  " + arr + "  "
+ arr);
}

// Condition when absolute
// difference between X and Y is 1
else if (Math.Abs(X - Y) == 1) {

// If one from X and Y is
// equal to 1
if (Math.Min(X, Y) == 1) {

// Printing possible
// number of cases
Console.WriteLine(1);
arr = 1;
arr = Math.Max(X, Y);
arr = Math.Max(X, Y) + 1;

// Printing consecutive
// array elements
Console.WriteLine(arr + "  " + arr
+ "  " + arr);
}
else if (Math.Max(X, Y) == N) {

// Printing possible
// number of cases
Console.WriteLine(1);
arr = Math.Min(X, Y) - 1;
arr = Math.Min(X, Y);
arr = Math.Max(X, Y);

// Printing consecutive
// array elements
Console.WriteLine(arr + " " + arr
+ " " + arr);
}

// For cases when neither X
// and nor Y is not equal
// to 1 or N.
else {

// Printing possible cases
Console.WriteLine(2);

// Values of consecutive
// numbers for first case
arr = Math.Min(X, Y) - 1;
arr = Math.Min(X, Y);
arr = Math.Max(X, Y);

// Printing consecutive
// array numbers
Console.WriteLine(arr + " " + arr
+ " " + arr);

// Values of consecutive
// numbers for second case
arr = Math.Min(X, Y);
arr = Math.Max(X, Y);
arr = Math.Max(X, Y) + 1;

// Printing consecutive
// array numbers
Console.WriteLine(arr + " " + arr
+ " " + arr);
}
}

// When difference between X and Y
// is greater than 2
else {
Console.WriteLine(0);
}
}

}

// This code is contributed by sanjoy_62.```

## Javascript

```    <script>
// JavaScript code to implement the approach

const findTriplet = (X, Y, N) => {
// Array initialized to hold
// consecutive array numbers
let arr = new Array(3).fill(0);

// Condition when absolute
// difference between X and Y is 2
if (Math.abs(X - Y) == 2) {

// Printing possible number
// of cases
document.write("1<br/>");
arr = X;
arr = Y;
arr = parseInt((X + Y) / 2);

// Printing consecutive
// array elements
document.write(`\${arr} \${arr} \${arr}<br/>`);
}

// Condition when absolute
// difference between X and Y is 1
else if (Math.abs(X - Y) == 1) {

// If one from X and Y is
// equal to 1
if (Math.min(X, Y) == 1) {

// Printing possible
// number of cases
document.write("1<br/>");
arr = 1;
arr = Math.max(X, Y);
arr = Math.max(X, Y) + 1;

// Printing consecutive
// array elements
document.write(`\${arr} \${arr} \${arr}<br/>`);
}
else if (Math.max(X, Y) == N) {

// Printing possible
// number of cases
document.write("1<br/>");
arr = Math.min(X, Y) - 1;
arr = Math.min(X, Y);
arr = Math.max(X, Y);

// Printing consecutive
// array elements
document.write(`\${arr} \${arr} \${arr}<br/>`);
}

// For cases when neither X
// and nor Y is not equal
// to 1 or N.
else {

// Printing possible cases
document.write("2<br/>");

// Values of consecutive
// numbers for first case
arr = Math.min(X, Y) - 1;
arr = Math.min(X, Y);
arr = Math.max(X, Y);

// Printing consecutive
// array numbers
document.write(`\${arr} \${arr} \${arr}<br/>`);

// Values of consecutive
// numbers for second case
arr = Math.min(X, Y);
arr = Math.max(X, Y);
arr = Math.max(X, Y) + 1;

// Printing consecutive
// array numbers
document.write(`\${arr} \${arr} \${arr}<br/>`);
}
}

// When difference between X and Y
// is greater than 2
else {
document.write("0<br/>");
}
}

// Driver function

// Input value of N
let N = 3;

// input value of X and Y
let X = 2, Y = 1;
findTriplet(X, Y, N);

// This code is contributed by rakeshsahni

</script>```
Output

```1
1  2  3```

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up