We are given a list of N numbers A_1,A_2, \dots A_n. We have to go from A_i to A_{i+1} in the i^{th} step. What is the total number of integers we skip in the process?
EXPLANATION:
The solution for this is just essentially counting in constant time the number of integers we skip in each iteration:
S_i = |A_{i+1} - A_i | - 1
notice that we have to take modulus here, since we can go to a smaller integer as well.
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--){
int n;
cin >> n;
long long arr[n];
for(int i=0;i<n;i++)cin >> arr[i];
long long ans = 0;
for(int i=1;i<n;i++)ans+=(abs(arr[i]-arr[i-1])-1);
cout << ans << '\n';
}
}
# cook your dish here.
for _ in range(int(input())):
x = int(input())
arr = list(map(int,input().split()))
l = []
for i in range(len(arr)-1):
if arr[i+1] > arr[i]:
l.append(arr[i+1] - arr[i])
elif arr[i+1]<arr[i]:
l.append(arr[i]-arr[i+1])
m = len(l)
print ((sum(l)) - m)
Can someone please tell why was i getting partially correct during contest
int n;
cin>>n;
while(n--){
long long T;
cin>>T;
long long a[T];
for(long long i=0;i<T;i++){
cin>>a[i];
}
int sum=0;
int diff=0;
for(long long j=0;j<T-1;j++){
diff=abs(a[j+1]-a[j])-1;
sum=sum+diff;
}
cout<<sum<<endl;
}
difference = 0
sum1=0
for i in range(int(input())):
f = int(input())
a = list(map(int,input().split()))
length = len(a)
for k in range(length-1):
difference = abs(a[k]-a[k+1])-1
sum1+=difference
print(sum1)
sum1=0
#Here's what i've done in Python 3