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++
// 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[3] = { 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[0] = X;
arr[2] = Y;
arr[1] = (X + Y) / 2;
// Printing consecutive
// array elements
cout << arr[0] << " " << arr[1] << " " << arr[2]
<< "\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[0] = 1;
arr[1] = max(X, Y);
arr[2] = max(X, Y) + 1;
// Printing consecutive
// array elements
cout << arr[0] << " " << arr[1] << " "
<< arr[2] << "\n";
}
else if (max(X, Y) == N) {
// Printing possible
// number of cases
cout << "1\n";
arr[0] = min(X, Y) - 1;
arr[1] = min(X, Y);
arr[2] = max(X, Y);
// Printing consecutive
// array elements
cout << arr[0] << " " << arr[1] << " " << arr[2]
<< "\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[0] = min(X, Y) - 1;
arr[1] = min(X, Y);
arr[2] = max(X, Y);
// Printing consecutive
// array numbers
cout << arr[0] << " " << arr[1] << " " << arr[2]
<< "\n";
// Values of consecutive
// numbers for second case
arr[0] = min(X, Y);
arr[1] = max(X, Y);
arr[2] = max(X, Y) + 1;
// Printing consecutive
// array numbers
cout << arr[0] << " " << arr[1] << " " << arr[2]
<< "\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[3];
// 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[0] = X;
arr[2] = Y;
arr[1] = (X + Y) / 2;
// Printing consecutive
// array elements
System.out.println(arr[0] + " " + arr[1] + " "
+ arr[2]);
}
// 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[0] = 1;
arr[1] = Math.max(X, Y);
arr[2] = Math.max(X, Y) + 1;
// Printing consecutive
// array elements
System.out.println(arr[0] + " " + arr[1]
+ " " + arr[2]);
}
else if (Math.max(X, Y) == N) {
// Printing possible
// number of cases
System.out.println(1);
arr[0] = Math.min(X, Y) - 1;
arr[1] = Math.min(X, Y);
arr[2] = Math.max(X, Y);
// Printing consecutive
// array elements
System.out.println(arr[0] + " " + arr[1]
+ " " + arr[2]);
}
// 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[0] = Math.min(X, Y) - 1;
arr[1] = Math.min(X, Y);
arr[2] = Math.max(X, Y);
// Printing consecutive
// array numbers
System.out.println(arr[0] + " " + arr[1]
+ " " + arr[2]);
// Values of consecutive
// numbers for second case
arr[0] = Math.min(X, Y);
arr[1] = Math.max(X, Y);
arr[2] = Math.max(X, Y) + 1;
// Printing consecutive
// array numbers
System.out.println(arr[0] + " " + arr[1]
+ " " + arr[2]);
}
}
// 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[0] = X
arr[2] = Y
arr[1] = (X + Y)//2
# Printing consecutive array elements
print(arr[0],end=' ')
print(arr[1],end=' ')
print(arr[2])
# 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[0] = 1
if(X > Y):
arr[1] = X
else:
arr[1] = Y
arr[2] = arr[1] + 1
# Printing consecutive array elements
print(arr[0],end=' ')
print(arr[1],end=' ')
print(arr[2])
elif(X == N or Y == N):
# Printing possible number of cases
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]
# Printing consecutive array elements
print(arr[0],end=' ')
print(arr[1],end=' ')
print(arr[2])
# 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[0] = Y - 1
else:
arr[0] = X - 1
arr[1] = arr[0] + 1
arr[2] = X + Y - arr[1]
# Printing consecutive array numbers
print(arr[0],end = ' ')
print(arr[1],end = ' ')
print(arr[2])
# Values of consecutive numbers for second case
if(X > Y):
arr[0] = Y
arr[1] = X
arr[2] = X + 1
else:
arr[0] = X
arr[1] = Y
arr[2] = Y + 1
# Printing consecutive array numbers
print(arr[0],end=' ')
print(arr[1],end=' ')
print(arr[2])
# 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[3];
// 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[0] = X;
arr[2] = Y;
arr[1] = (X + Y) / 2;
// Printing consecutive
// array elements
Console.WriteLine(arr[0] + " " + arr[1] + " "
+ arr[2]);
}
// 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[0] = 1;
arr[1] = Math.Max(X, Y);
arr[2] = Math.Max(X, Y) + 1;
// Printing consecutive
// array elements
Console.WriteLine(arr[0] + " " + arr[1]
+ " " + arr[2]);
}
else if (Math.Max(X, Y) == N) {
// Printing possible
// number of cases
Console.WriteLine(1);
arr[0] = Math.Min(X, Y) - 1;
arr[1] = Math.Min(X, Y);
arr[2] = Math.Max(X, Y);
// Printing consecutive
// array elements
Console.WriteLine(arr[0] + " " + arr[1]
+ " " + arr[2]);
}
// 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[0] = Math.Min(X, Y) - 1;
arr[1] = Math.Min(X, Y);
arr[2] = Math.Max(X, Y);
// Printing consecutive
// array numbers
Console.WriteLine(arr[0] + " " + arr[1]
+ " " + arr[2]);
// Values of consecutive
// numbers for second case
arr[0] = Math.Min(X, Y);
arr[1] = Math.Max(X, Y);
arr[2] = Math.Max(X, Y) + 1;
// Printing consecutive
// array numbers
Console.WriteLine(arr[0] + " " + arr[1]
+ " " + arr[2]);
}
}
// 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[0] = X;
arr[2] = Y;
arr[1] = parseInt((X + Y) / 2);
// Printing consecutive
// array elements
document.write(`${arr[0]} ${arr[1]} ${arr[2]}<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[0] = 1;
arr[1] = Math.max(X, Y);
arr[2] = Math.max(X, Y) + 1;
// Printing consecutive
// array elements
document.write(`${arr[0]} ${arr[1]} ${arr[2]}<br/>`);
}
else if (Math.max(X, Y) == N) {
// Printing possible
// number of cases
document.write("1<br/>");
arr[0] = Math.min(X, Y) - 1;
arr[1] = Math.min(X, Y);
arr[2] = Math.max(X, Y);
// Printing consecutive
// array elements
document.write(`${arr[0]} ${arr[1]} ${arr[2]}<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[0] = Math.min(X, Y) - 1;
arr[1] = Math.min(X, Y);
arr[2] = Math.max(X, Y);
// Printing consecutive
// array numbers
document.write(`${arr[0]} ${arr[1]} ${arr[2]}<br/>`);
// Values of consecutive
// numbers for second case
arr[0] = Math.min(X, Y);
arr[1] = Math.max(X, Y);
arr[2] = Math.max(X, Y) + 1;
// Printing consecutive
// array numbers
document.write(`${arr[0]} ${arr[1]} ${arr[2]}<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>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Count triplets from a given range having sum of two numbers of a triplet equal to the third number Given two integers L and R, the task is to find the number of unique triplets whose values lie in the range [L, R], such that the sum of any two numbers is equal to the third number. Examples: Input: L = 1, R = 3Output: 3Explanation: Three such triplets satisfying the necessary conditions are (1, 1,
9 min read
Count numbers in range [L, R] having K consecutive set bits Given three positive integers l, r, and k, the task is to find the count of numbers in the range [l, r] having k consecutive set bits in their binary representation.Examples:Input: l = 4, r = 15, k = 3 Output: 3 Explanation: Numbers whose binary representation contains k=3 consecutive set bits are:
13 min read
Count of ways to split N into Triplets forming a Triangle Given an integer N, the task is to find the number of ways to split N into ordered triplets which can together form a triangle. Examples: Input: N = 15 Output: Total number of triangles possible are 28 Input: N = 9 Output: Total number of triangles possible is 10 Approach: The following observation
4 min read
Count the number of ordered sets not containing consecutive numbers Given a positive integer N. Count the total number of ordered sets of any size such that consecutive numbers are not present in the set and all numbers are <= N. Ordered Set: Arrays in which all numbers are distinct, and order of elements is taken into consideration, For e.g. {1, 2, 3} is differe
14 min read
Count number of triplets with product equal to given number | Set 2 Given an array of distinct integers(considering only positive numbers) and a number âmâ, find the number of triplets with the product equal to âmâ. Examples: Input: arr[] = { 1, 4, 6, 2, 3, 8} m = 24 Output: 3 Input: arr[] = { 0, 4, 6, 2, 3, 8} m = 18 Output: 0 An approach with O(n) extra space has
7 min read
Ways to form an array having integers in given range such that total sum is divisible by 2 Given three positive integers N, L and R. The task is to find the number of ways to form an array of size N where each element lies in the range [L, R] such that the total sum of all the elements of the array is divisible by 2.Examples: Input: N = 2, L = 1, R = 3 Output: 5 Explanation: Possible arra
15+ min read