Can be coded in C++, Java, or Python
Given three integers, j j and k, a sequence sumto be the value of j + (i + 1) + (i+ 2) + (i+ 3) +... +j + (j= 1) + (j = 2) + (j = 3) +... + k (increment from i until it equals j then decrement from
J until it equals k). Given values j j, and k, calculate the sequence sum as described.
Sum all the values from ito jand back to k: 5+6+7+8+9+8+ 7 +6 = 56.
Function Description
Complete the function getSequenceSum in the editor below.
getSequenceSum has the following parameter(s):
inti, intj, int k: three integers
Return
long: the value of the sequence sum
Constraints
-10^8 ≤ i,j,k≤10^8
i,k≤j
v Input Format For Custom Testing
The first line contains an integer, i.
The next line contains an integer, .
The last line contains an integer, k.
v Sample Case 0
Sample Input 0
STDIN Function
Sample Output 0
24
Explanation 0
i=0
j=5
k=-1
0+1+2+3+4+5+4+3+2+1+0+-1=24
v Sample Case 1
STDIN: Function:
-5 -> i=-5
-1 -> j=-1
-3 -> k=-3
Sample Output 1:
-20
Explanation 1:
i=-5, j=-1, k=-3
-5+-4+-3+-2+-1+-2+-3=-20
1 answer
```python
def getSequenceSum(i, j, k):
if i <= j:
return sum(range(i, j+1)) + sum(range(j-1, k-1, -1))
else:
return sum(range(j, i+1)) + sum(range(i-1, k-1, -1))
```
And here is a Java implementation:
```java
public static long getSequenceSum(int i, int j, int k) {
if (i <= j) {
int sum1 = 0;
int sum2 = 0;
for (int num = i; num <= j; num++) {
sum1 += num;
}
for (int num = j-1; num >= k; num--) {
sum2 += num;
}
return sum1 + sum2;
} else {
int sum1 = 0;
int sum2 = 0;
for (int num = j; num <= i; num++) {
sum1 += num;
}
for (int num = i-1; num >= k; num--) {
sum2 += num;
}
return sum1 + sum2;
}
}
```
You can use either of these functions to calculate the sequence sum based on the given inputs.