Pregunta de entrevista de PhonePe

1st round: 2nd coding question: describes a new programming language that only contains one binary operator #. This operator takes two integers as input and returns their sum. However, the operation modifies the values of the input integers in a specific way: the value of the first operand (a) is updated to be the sum of a and b, while the value of the second operand (b) remains unchanged. The problem is to find the minimum number of operations required to make either L or M strictly greater than N using this # operator. The input consists of three integers: L, M, and N. The output should be the minimum number of operations needed to achieve the desired condition.

Respuesta de la entrevista

Anónimo

15 oct 2024

#include int min_operations(int L, int M, int N) { // If L or M is already greater than N, no operations are needed if (L > N || M > N) { return 0; } int operations = 0; // Continue applying the # operation until either L or M is greater than N while (L <= N && M <= N) { // Always update the smaller of the two to maximize the increase if (L < M) { L += M; // Apply the # operation: L becomes L + M } else { M += L; // Apply the # operation: M becomes M + L } operations++; } return operations; } int main() { int L, M, N; // Input values for L, M, and N printf("Enter L: "); scanf("%d", &L); printf("Enter M: "); scanf("%d", &M); printf("Enter N: "); scanf("%d", &N); // Calculate and output the result int result = min_operations(L, M, N); printf("Minimum number of operations: %d\n", result); return 0; }