Count ways to form Triplet of consecutive integers having the given numbers
Last Updated :
15 Nov, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void findTriplet( int X, int Y, int N)
{
int arr[3] = { 0 };
if ( abs (X - Y) == 2) {
cout << "1\n" ;
arr[0] = X;
arr[2] = Y;
arr[1] = (X + Y) / 2;
cout << arr[0] << " " << arr[1] << " " << arr[2]
<< "\n" ;
}
else if ( abs (X - Y) == 1) {
if (min(X, Y) == 1) {
cout << "1\n" ;
arr[0] = 1;
arr[1] = max(X, Y);
arr[2] = max(X, Y) + 1;
cout << arr[0] << " " << arr[1] << " "
<< arr[2] << "\n" ;
}
else if (max(X, Y) == N) {
cout << "1\n" ;
arr[0] = min(X, Y) - 1;
arr[1] = min(X, Y);
arr[2] = max(X, Y);
cout << arr[0] << " " << arr[1] << " " << arr[2]
<< "\n" ;
}
else {
cout << "2\n" ;
arr[0] = min(X, Y) - 1;
arr[1] = min(X, Y);
arr[2] = max(X, Y);
cout << arr[0] << " " << arr[1] << " " << arr[2]
<< "\n" ;
arr[0] = min(X, Y);
arr[1] = max(X, Y);
arr[2] = max(X, Y) + 1;
cout << arr[0] << " " << arr[1] << " " << arr[2]
<< "\n" ;
}
}
else {
cout << "0\n" ;
}
}
int main()
{
int N = 3;
int X = 2, Y = 1;
findTriplet(X, Y, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int N = 3 ;
int X = 2 , Y = 1 ;
findTriplet(X, Y, N);
}
static void findTriplet( int X, int Y, int N)
{
int [] arr = new int [ 3 ];
if (Math.abs(X - Y) == 2 ) {
System.out.println( 1 );
arr[ 0 ] = X;
arr[ 2 ] = Y;
arr[ 1 ] = (X + Y) / 2 ;
System.out.println(arr[ 0 ] + " " + arr[ 1 ] + " "
+ arr[ 2 ]);
}
else if (Math.abs(X - Y) == 1 ) {
if (Math.min(X, Y) == 1 ) {
System.out.println( 1 );
arr[ 0 ] = 1 ;
arr[ 1 ] = Math.max(X, Y);
arr[ 2 ] = Math.max(X, Y) + 1 ;
System.out.println(arr[ 0 ] + " " + arr[ 1 ]
+ " " + arr[ 2 ]);
}
else if (Math.max(X, Y) == N) {
System.out.println( 1 );
arr[ 0 ] = Math.min(X, Y) - 1 ;
arr[ 1 ] = Math.min(X, Y);
arr[ 2 ] = Math.max(X, Y);
System.out.println(arr[ 0 ] + " " + arr[ 1 ]
+ " " + arr[ 2 ]);
}
else {
System.out.println( 2 );
arr[ 0 ] = Math.min(X, Y) - 1 ;
arr[ 1 ] = Math.min(X, Y);
arr[ 2 ] = Math.max(X, Y);
System.out.println(arr[ 0 ] + " " + arr[ 1 ]
+ " " + arr[ 2 ]);
arr[ 0 ] = Math.min(X, Y);
arr[ 1 ] = Math.max(X, Y);
arr[ 2 ] = Math.max(X, Y) + 1 ;
System.out.println(arr[ 0 ] + " " + arr[ 1 ]
+ " " + arr[ 2 ]);
}
}
else {
System.out.println( 0 );
}
}
}
|
Python3
def findTriplet(X,Y,N):
arr = [ 0 , 0 , 0 ]
if ( abs (X - Y) = = 2 ):
print ( 1 )
arr[ 0 ] = X
arr[ 2 ] = Y
arr[ 1 ] = (X + Y) / / 2
print (arr[ 0 ],end = ' ' )
print (arr[ 1 ],end = ' ' )
print (arr[ 2 ])
elif ( abs (X - Y) = = 1 ):
if (X = = 1 or Y = = 1 ):
print ( 1 )
arr[ 0 ] = 1
if (X > Y):
arr[ 1 ] = X
else :
arr[ 1 ] = Y
arr[ 2 ] = arr[ 1 ] + 1
print (arr[ 0 ],end = ' ' )
print (arr[ 1 ],end = ' ' )
print (arr[ 2 ])
elif (X = = N or Y = = N):
print ( 1 )
if (X > Y):
arr[ 0 ] = Y - 1
else :
arr[ 0 ] = X - 1
arr[ 1 ] = arr[ 0 ] + 1
arr[ 2 ] = X + Y - arr[ 1 ]
print (arr[ 0 ],end = ' ' )
print (arr[ 1 ],end = ' ' )
print (arr[ 2 ])
else :
print ( 2 )
if (X > Y):
arr[ 0 ] = Y - 1
else :
arr[ 0 ] = X - 1
arr[ 1 ] = arr[ 0 ] + 1
arr[ 2 ] = X + Y - arr[ 1 ]
print (arr[ 0 ],end = ' ' )
print (arr[ 1 ],end = ' ' )
print (arr[ 2 ])
if (X > Y):
arr[ 0 ] = Y
arr[ 1 ] = X
arr[ 2 ] = X + 1
else :
arr[ 0 ] = X
arr[ 1 ] = Y
arr[ 2 ] = Y + 1
print (arr[ 0 ],end = ' ' )
print (arr[ 1 ],end = ' ' )
print (arr[ 2 ])
else :
print ( 0 )
if __name__ = = "__main__" :
N = 3
X = 2
Y = 1
findTriplet(X,Y,N)
|
C#
using System;
public class GFG
{
public static void Main( string [] args)
{
int N = 3;
int X = 2, Y = 1;
findTriplet(X, Y, N);
}
static void findTriplet( int X, int Y, int N)
{
int [] arr = new int [3];
if (Math.Abs(X - Y) == 2) {
Console.WriteLine(1);
arr[0] = X;
arr[2] = Y;
arr[1] = (X + Y) / 2;
Console.WriteLine(arr[0] + " " + arr[1] + " "
+ arr[2]);
}
else if (Math.Abs(X - Y) == 1) {
if (Math.Min(X, Y) == 1) {
Console.WriteLine(1);
arr[0] = 1;
arr[1] = Math.Max(X, Y);
arr[2] = Math.Max(X, Y) + 1;
Console.WriteLine(arr[0] + " " + arr[1]
+ " " + arr[2]);
}
else if (Math.Max(X, Y) == N) {
Console.WriteLine(1);
arr[0] = Math.Min(X, Y) - 1;
arr[1] = Math.Min(X, Y);
arr[2] = Math.Max(X, Y);
Console.WriteLine(arr[0] + " " + arr[1]
+ " " + arr[2]);
}
else {
Console.WriteLine(2);
arr[0] = Math.Min(X, Y) - 1;
arr[1] = Math.Min(X, Y);
arr[2] = Math.Max(X, Y);
Console.WriteLine(arr[0] + " " + arr[1]
+ " " + arr[2]);
arr[0] = Math.Min(X, Y);
arr[1] = Math.Max(X, Y);
arr[2] = Math.Max(X, Y) + 1;
Console.WriteLine(arr[0] + " " + arr[1]
+ " " + arr[2]);
}
}
else {
Console.WriteLine(0);
}
}
}
|
Javascript
<script>
const findTriplet = (X, Y, N) => {
let arr = new Array(3).fill(0);
if (Math.abs(X - Y) == 2) {
document.write( "1<br/>" );
arr[0] = X;
arr[2] = Y;
arr[1] = parseInt((X + Y) / 2);
document.write(`${arr[0]} ${arr[1]} ${arr[2]}<br/>`);
}
else if (Math.abs(X - Y) == 1) {
if (Math.min(X, Y) == 1) {
document.write( "1<br/>" );
arr[0] = 1;
arr[1] = Math.max(X, Y);
arr[2] = Math.max(X, Y) + 1;
document.write(`${arr[0]} ${arr[1]} ${arr[2]}<br/>`);
}
else if (Math.max(X, Y) == N) {
document.write( "1<br/>" );
arr[0] = Math.min(X, Y) - 1;
arr[1] = Math.min(X, Y);
arr[2] = Math.max(X, Y);
document.write(`${arr[0]} ${arr[1]} ${arr[2]}<br/>`);
}
else {
document.write( "2<br/>" );
arr[0] = Math.min(X, Y) - 1;
arr[1] = Math.min(X, Y);
arr[2] = Math.max(X, Y);
document.write(`${arr[0]} ${arr[1]} ${arr[2]}<br/>`);
arr[0] = Math.min(X, Y);
arr[1] = Math.max(X, Y);
arr[2] = Math.max(X, Y) + 1;
document.write(`${arr[0]} ${arr[1]} ${arr[2]}<br/>`);
}
}
else {
document.write( "0<br/>" );
}
}
let N = 3;
let X = 2, Y = 1;
findTriplet(X, Y, N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...