AB plus C solution

Problem(AB plus C)

Chef has an integer � and wants to find out any triplet (�,�,�) such that:

  • 1≤�,�,�≤106;
  • �⋅�+�=�

If no such triplet exists, print −1 instead.

Input Format

  • The first line of input will contain a single integer �, denoting the number of test cases.
  • Each test case consists of a single integer �, as mentioned in the statement.

Output Format

For each test case, output the values of �,�,� such that 1≤�,�,�≤106 and �⋅�+�=�.

If multiple such triplets exist, you may print any.
If no such triplet exists, print −1 instead.

Constraints

  • 1≤�≤105
  • 1≤�≤1012

Sample 1:

Input
Output
4
1
15
2
100
-1
2 7 1
1 1 1
9 9 19

Explanation:

Test case 1: It can be proven that no such triplet exists.

Test case 2: Consider �=2,�=7, and �=1, where:

  • 1≤�,�,�≤106
  • �⋅�+�=2⋅7+1=15=�

Test case 3: Consider �=1,�=1, and �=1, where:

  • 1≤�,�,�≤106
  • �⋅�+�=1⋅1+1=2=�

Solution:-


#include <stdio.h> // Include the stdio library for input and output

int main() {
int t; // Number of test cases
scanf("%d", &t); // Read the number of test cases from standard input
// Loop over each test case
while (t--) {
long long int n; // Declare variable 'n' to store the input number for each test case
long long num = 1000000; // Set 'num' to 1,000,000, which is used for calculations later
// Read the input number for the current test case
scanf("%lld", &n);
// Case when 'n' is 1: output -1
if (n == 1) {
printf("-1\n");
}
// Case when 'n' is less than or equal to 'num': output 1, 1, and (n-1)
else if (n <= num) {
printf("1 1 %lld\n", n - 1);
}
// Case when 'n' is a multiple ofnum': output (n/num - 1), 'num', and 'num'
else if (n % num == 0) {
printf("%lld %lld %lld\n", (n / num) - 1, num, num);
}
// Case when 'n' is not a multiple of 'num': output (n/num), 'num', and (n%num)
else {
printf("%lld %lld %lld\n", n / num, num, n % num);
}
}
// Return 0 to indicate successful completion
return 0;
}
#include <iostream> // Include the iostream library for input and output
using namespace std; // Use the standard namespace to avoid typing 'std::' before cin and cout

int main() {
int t; // Number of test cases
cin >> t; // Read the number of test cases from standard input
// Loop over each test case
while (t--) {
long long int n; // Declare variable 'n' to store the input number for each test case
long long num = 1000000; // Set 'num' to 1,000,000, which is used for calculations later
// Read the input number for the current test case
cin >> n;
// Case when 'n' is 1: output -1
if (n == 1) {
cout << -1 << endl;
}
// Case when 'n' is less than or equal to 'num': output 1, 1, and (n-1)
else if (n <= num) {
cout << 1 << " " << 1 << " " << n - 1 << endl;
}
// Case when 'n' is a multiple of 'num': output (n/num - 1), 'num', and 'num'
else if (n % num == 0) {
cout << (n / num) - 1 << " " << num << " " << num << endl;
}
// Case when 'n' is not a multiple of 'num': output (n/num), 'num', and (n%num)
else {
cout << (n / num) << " " << num << " " << n % num << endl;
}
}
// Return 0 to indicate successful completion
return 0;
}
# Loop over each test case
for _ in range(t):
# Read the input number for the current test case
n = int(input())
num = 1000000 # Set 'num' to 1,000,000, which is used for calculations later
# Case when 'n' is 1: output -1
if n == 1:
print(-1)
# Case when 'n' is less than or equal to 'num': output 1, 1, and (n-1)
elif n <= num:
print(1, 1, n - 1)
# Case when 'n' is a multiple of 'num': output (n//num - 1), 'num', and 'num'
elif n % num == 0:
print((n // num) - 1, num, num)
# Case when 'n' is not a multiple of 'num': output (n//num), 'num', and (n%num)
else:
print(n // num, num, n % num)
Text Copied!


easycodingzone

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-