|
| 1 | +--- |
| 2 | +id: arithmetic-number |
| 3 | +title: Arithmetic Number |
| 4 | +sidebar_label: Arithmetic-Number |
| 5 | +tags: |
| 6 | + - Intermediate |
| 7 | + - Array |
| 8 | + - Mathematical |
| 9 | + - GeeksforGeeks |
| 10 | + - CPP |
| 11 | + - Python |
| 12 | + - DSA |
| 13 | +description: "This tutorial covers the solution to the Find the Arithmetic Number problem from the GeeksforGeeks." |
| 14 | +--- |
| 15 | +## Problem Description |
| 16 | + |
| 17 | +Given three integers `'A'` denoting the first term of an arithmetic sequence , `'C'` denoting the common difference of an arithmetic sequence and an integer `'B'`. you need to tell whether `'B'` exists in the arithmetic sequence or not. Return `1` if `B` is present in the sequence. Otherwise, returns `0`. |
| 18 | + |
| 19 | +## Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | +``` |
| 24 | +Input: A = 1, B = 3, C = 2 |
| 25 | +Output: 1 |
| 26 | +Explaination: 3 is the second term of the |
| 27 | +sequence starting with 1 and having a common |
| 28 | +difference 2. |
| 29 | +``` |
| 30 | + |
| 31 | +**Example 2:** |
| 32 | + |
| 33 | +``` |
| 34 | +Input: A = 1, B = 2, C = 3 |
| 35 | +Output: 0 |
| 36 | +Explaination: 2 is not present in the sequence. |
| 37 | +``` |
| 38 | + |
| 39 | +## Your Task |
| 40 | + |
| 41 | +You do not need to read input or print anything. Your task is to complete the function inSequence() which takes A, B and C and returns 1 if B is present in the sequence. Otherwise, returns 0. |
| 42 | + |
| 43 | +Expected Time Complexity: $O(1)$ |
| 44 | + |
| 45 | +Expected Auxiliary Space: $O(1)$ |
| 46 | + |
| 47 | +## Constraints |
| 48 | + |
| 49 | +* `-10^9 ≤ A, B, C ≤ 10^9` |
| 50 | + |
| 51 | +## Problem Explanation |
| 52 | +Given three integers 'A' denoting the first term of an arithmetic sequence , 'C' denoting the common difference of an arithmetic sequence and an integer 'B'. you need to tell whether 'B' exists in the arithmetic sequence or not. Return 1 if B is present in the sequence. Otherwise, returns 0. |
| 53 | + |
| 54 | + |
| 55 | +## Code Implementation |
| 56 | + |
| 57 | +<Tabs> |
| 58 | + <TabItem value="Python" label="Python" default> |
| 59 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 60 | + |
| 61 | + ```py |
| 62 | + def is_present(A, C, B): |
| 63 | + if (B - A) % C == 0 and (B - A) / C >= 0: |
| 64 | + return 1 |
| 65 | + return 0 |
| 66 | + |
| 67 | + |
| 68 | + ``` |
| 69 | + |
| 70 | + </TabItem> |
| 71 | + <TabItem value="C++" label="C++"> |
| 72 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 73 | + |
| 74 | + ```cpp |
| 75 | + int isPresent(int A, int C, int B) { |
| 76 | + if ((B - A) % C == 0 && (B - A) / C >= 0) { |
| 77 | + return 1; |
| 78 | + } |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | + ``` |
| 83 | +
|
| 84 | + </TabItem> |
| 85 | +
|
| 86 | + <TabItem value="Javascript" label="Javascript" default> |
| 87 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 88 | +
|
| 89 | + ```javascript |
| 90 | + function isPresent(A, C, B) { |
| 91 | + if ((B - A) % C === 0 && (B - A) / C >= 0) { |
| 92 | + return 1; |
| 93 | + } |
| 94 | + return 0; |
| 95 | +} |
| 96 | +
|
| 97 | +
|
| 98 | + ``` |
| 99 | + |
| 100 | + </TabItem> |
| 101 | + |
| 102 | + <TabItem value="Typescript" label="Typescript" default> |
| 103 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 104 | + |
| 105 | + ```typescript |
| 106 | +function isPresent(A, C, B) { |
| 107 | + if ((B - A) % C === 0 && (B - A) / C >= 0) { |
| 108 | + return 1; |
| 109 | + } |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
| 113 | + ``` |
| 114 | + |
| 115 | + </TabItem> |
| 116 | + |
| 117 | + <TabItem value="Java" label="Java" default> |
| 118 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 119 | + |
| 120 | + ```java |
| 121 | + public int isPresent(int A, int C, int B) { |
| 122 | + if ((B - A) % C == 0 && (B - A) / C >= 0) { |
| 123 | + return 1; |
| 124 | + } |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | + ``` |
| 130 | + |
| 131 | + </TabItem> |
| 132 | +</Tabs> |
| 133 | + |
| 134 | + |
| 135 | +## Solution Logic: |
| 136 | +The solution checks if the difference between B and A is a multiple of C (the common difference) and if the result is non-negative. If both conditions are true, it means that B is present in the arithmetic sequence. |
| 137 | + |
| 138 | + |
| 139 | +## Time Complexity |
| 140 | + |
| 141 | +* The time complexity is $O(1)$, because it only involves simple arithmetic operations. |
| 142 | + |
| 143 | + |
| 144 | +## Space Complexity |
| 145 | + |
| 146 | +* The auxiliary space complexity is $O(1)$ because we are not using any extra space proportional to the size of the input array. |
0 commit comments