0

I am trying to solve a problem which is a Basic level Array problem. I implemented my logic and passed the sample test cases given on that page and also tried some custom inputs and it did worked as intended. But when I submit my code it gives a Segmentation Fault, but unfortunately it didn't tell on which particular test case it happens.

So, here is my logic:-

  1. Check if K divides N exactly or not because if it does we can simply use reverse function in a loop

  2. If it doesn't we first reverse the elements up to e(which are under the given group)

  3. Then we reverse the remaining elements separately

Here is my code:-

#include <bits/stdc++.h>
using namespace std;
int main(){
    int T; cin >> T;
    while(T--) {                   
        int N, K;
        cin >> N >> K;
        int A[N];
        for (int i = 0; i < N; i++)
              cin >> A[i];
        if (N / K != 0) {
            int G = (int)(N / K);   // G -> groups
            int e = G * K;          // e -> no. of elements which fall under given group
        for (int i = 0; i < e; i += K)
          reverse(A + i, A + i + K);

        reverse(A + e , A + N);    // reversing remaining elements
        } 
        else {
        for (int i = 0; i < N; i += K) 
            reverse(A + i, A + i + K);
        }
        for (int i = 0; i < N; i++)
              cout << A[i] << " ";
        cout << "\n";      
    }
    return 0;
}

Now everything is working as expected but why it is giving SIGSEGV on submitting the code? and how can I find which particular line is causing this?

PS: I know that I shouldn't use using namespace std and bits/stdc++.h and I know it's implications

daya
  • 160
  • 2
  • 14
  • `if K divides N exactly` and `if (N / K != 0) {` aren't the same. Did you mean `if (N % K != 0) {` – Jerry Jeremiah May 10 '20 at 08:30
  • @JerryJeremiah Right! typo from my side. Corrected – daya May 10 '20 at 08:31
  • `using namespace std;` in a source file isnt that evil, its just not nice. But `#include ` is, because strictly speaking it is not C++. If you know you should not do it, why do you still do it? Also `int A[N];` is not standard C++, see here: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – 463035818_is_not_an_ai May 10 '20 at 08:39
  • @idclev463035818 Thanks, I didn't know about `int A[N]` before but As I said in my question body that using or not using those two things is not the concern for this particular question. So I hope you understand what I mean :) – daya May 10 '20 at 08:46
  • it should be a concern, because not everybody can compile code that has `#include `. – 463035818_is_not_an_ai May 10 '20 at 08:47
  • @idclev463035818 Agreed, but the platform I am using (geeksforgeeks) is able to compile the code and I submitted many solutions with no problems at all – daya May 10 '20 at 08:50
  • because your code in non-portable. It works with one compiler, but not with others. Also "compiles without errors" doesn't mean a thing – 463035818_is_not_an_ai May 10 '20 at 08:53
  • `A[i]` can be up to 10^18. Too large for an `int` – Damien May 10 '20 at 10:12
  • @Damien After changing array to a vector. Now I am getting SIGABRT. – daya May 10 '20 at 10:38
  • Did you try using `long long` instead of `int`, to handle such large values ? – Damien May 10 '20 at 10:42
  • @Damien Yes, but it still gives SIGABRT – daya May 10 '20 at 11:00
  • You have an out of bound access when calling `reverse` function – Damien May 10 '20 at 11:07
  • @Damien Can you please explain how? because I rechecked the code and can't understand how it has an out of bound access. – daya May 10 '20 at 12:05
  • `i < N` implies that `A + i + K` is out of bound. Maybe `i < N -K` – Damien May 10 '20 at 12:09
  • Not really, Let's say N = 6 and K = 2, then G = 3, now we will execute `else` block, In 1st iteration i will be 0. In 2nd, i will be 2. In 3rd iteration will be 4 and now our array is completely reversed and in 4th iteration i will be 6 and at _this_ point condition in `for` loop becomes false and loop will simply end and `reverse()` will never execute therefore no out of bound exception – daya May 10 '20 at 12:38

1 Answers1

0

So the fault here is reverse(A + i, A + i + K); because if K > N then A + i + K in reverse() will access out of bound memory therefore it causes SIGSEGV. The solution is to use reverse(A + i, min(A + N, A + i + K)); which will handle both the general case and special case when K > N

daya
  • 160
  • 2
  • 14