-3

Reference Image

I have been trying to solve this simple problem in C++ but every time I submit, it says wrong answer. I am pretty sure I have got the logic right. Any help is appreciated.

Question: Find the sum of distances between the inputted numbers.

Ex. Input: 2 5 8 2 1
Distance=2+2+5+0 =9, (1 < n < 1000000)

PS: Input can't have the same number consecutively.
PSS: Subtask two is giving Wrong Answer

Code:

#include <iostream>
using namespace std;

int main() {
    // your code goes here

    int t,a[100000],n,sum=0;

    cin>>t;

    for(int i=0;i<t;i++)
    {
        cin>>n;
        
        for(int j=0;j<n;j++)
        {
            cin>>a[j];
        }
        for(int j=0;j<n-1;j++)
        {
            if(a[j]!=a[j+1])
            {
                sum = sum + abs(a[j]-a[j+1])-1;
            }
       }
        cout<<sum<<endl;
        sum=0;   
    }
}
GSerg
  • 76,472
  • 17
  • 159
  • 346
ASHWIN
  • 21
  • 3
  • 1
    Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/217638/discussion-on-question-by-ashwin-what-is-the-problem-with-the-logic-of-the-code). – Samuel Liew Jul 11 '20 at 08:16

1 Answers1

1

The problem with your code is that you are using int type for sum whose maximum value (1E11) can exceed the upper limit of int(if it's 32-bit or less). Use long long(atleast 64-bit) instead to store your sum.

Well, you can also optimize the code because you don't exactly need an array of 100000 integers and store the values in it. You can do so using only two variables.

Here is a modified implementation of your logic:

#include <iostream>

int main() {
    int t, n, first, second;
    long long sum; // or better use std::int_fast64_t sum;
    std::cin >> t;
    while (t--) {
        sum = 0;
        std::cin >> n >> first;
        for (int i = 0; i < n - 1; ++i) {
            std::cin >> second;
            sum += std::abs(first - second) - 1;
            first = second;
        }
        std::cout << sum << std::endl;
    }
}

PS: In competitive coding checking the provided constraints like if(a[j]!=a[j+1]) is useless. The problem statement simply guarantees it that it will never be false.

brc-dd
  • 10,788
  • 3
  • 47
  • 67
  • 1
    Use `std::int64_t` instead. The exact size of `int`, `long` and `long long` is not specified. – Thomas Sablik Jul 10 '20 at 11:17
  • @ThomasSablik added that in the code. BTW that exactly doesn't matter unless you want fixed width, which you don't in this particular question. The integer must be *atleast* 64-bit to hold the `sum` in this case, and the standard guarantees that `long long` will meet that requirement, i.e. it will be of atleast 64-bit. So, the code I'd posted earlier would have also worked fine. – brc-dd Jul 10 '20 at 11:45
  • If OP had used `std::int32_t` instead of `int` the question would have been answered one hour earlier. – Thomas Sablik Jul 10 '20 at 11:54
  • @ThomasSablik Actually I'd answered it 1 hr earlier (as soon as he had provided the question link in the comments)! :) – brc-dd Jul 10 '20 at 11:55
  • You commented 30 minutes after the question was asked. If OP had used `std::int32_t` the first comment would have answered the question after less than 5 minutes. And your comment was _"try changing..."_ because you still couldn't be sure. – Thomas Sablik Jul 10 '20 at 11:58
  • @ThomasSablik Okay! That's quite OP's fault. But surely I haven't seen the use of `std::int32_t` much, especially in competitive coding. Moreover I have seen a thing, which you'll find quite amusing. In their template these guys use a line `#define int long long`. :-D – brc-dd Jul 10 '20 at 12:02
  • @ThomasSablik There was a reason I wasn't sure: I had searched the environment details of Codechef, but that page returned me a 403 forbidden status code. – brc-dd Jul 10 '20 at 12:04
  • Especially in competitive programming where I don't know the platform I have to fix all variables. On my system I know the size of `int` and `long long`. In a cloud I don't know them. You can't even be sure that it will run on the same system every time. On some sites your code will pass all checks in the morning and in the evening you get Time Limit Exceeded with the same code. – Thomas Sablik Jul 10 '20 at 12:04