THIS IS THE LINK FOR QUESTION:https://practice.geeksforgeeks.org/problems/subarray-with-given-sum/0

#include <iostream>
using namespace std;

int main() {
            long long int s,n,a[10000000000],e,d,l,flag=0,t;
            cin>>t;
            while(t--)
            {   cin>>n>>s;
                e=0;
                flag=0;
                for(long long int i=0;i<n;i++)
                {   cin>>a[i];
                }
                for(long long int j=0;j<n;j++)
                {   e=a[j];
                    for(long long int h=j+1;h<n;h++)
                    {   e+=a[h];
                        if(e==s)
                        {   l=j;
                            d=h;
                            flag=1;
                            break;
                        }
                        else if(e>s)
                        {   flag=2;
                            break;
                        }     

                    }
                    if(flag==1 || flag==2)
                    break;
                }
                if(flag==1)
                cout<<l+1<<" "<<d+1;
                else
                cout<<-1;

                cout<<endl;
            }     
    return 0;
}
cored0mp commented: One other thing +0

Dani AI

Generated

The crash in the posted program has two separate problems that need to be fixed together: the massive fixed-size array and the algorithm/logic. correctly flagged the allocation problem, and clarified the problem constraints. In addition, the nested loops in the original code never test the single-element case (the code sets e = a[j] then only tests sums that include a[j+1]), and an O(N²) approach will be infeasible for large N.

A practical fix: allocate after reading N and use an O(N) sliding-window (two-pointer) solution when the array values are non‑negative. The sliding-window both avoids huge stack allocations and runs in linear time. Example implementation:

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    if (!(cin >> T)) return 0;
    while (T--) {
        long long n, target;
        cin >> n >> target;
        vector<long long> a(n);
        for (long long i = 0; i < n; ++i) cin >> a[i];

        long long sum = 0, left = 0;
        bool found = false;
        for (long long right = 0; right < n; ++right) {
            sum += a[right];
            while (sum > target && left <= right) sum -= a[left++];
            if (sum == target) {
                cout << left + 1 << ' ' << right + 1 << '\n';
                found = true;
                break;
            }
        }
        if (!found) cout << -1 << '\n';
    }
    return 0;
}

Notes and troubleshooting tips: sliding-window requires non‑negative elements; if negatives are allowed, use prefix sums + hashmap. Check judge memory limits (vector<long long> of size 1e7 ≈ 80 MB) before assuming full allocation is safe. For segfault hunting, compile with debug/sanitizer flags (-g -fsanitize=address) or use gdb/valgrind to see the exact failing line. Finally, avoid very large stack arrays — always allocate based on the input size (dynamic allocation or STL containers).

Recommended Answers

All 4 Replies

Do you have enough address space for 10 biilion long long ints (at least 80GB)?

I think perhaps you need to look at the limits of this challenge a little closer. The size of the array(N) is limited to 10⁷. It's the numbers in the array(Aᵢ) that are limited to 10¹⁰.

1 <= T <= 100
1 <= N <= 10⁷
1 <= Aᵢ <= 10¹⁰

A segmentation fault in your program typically occurs due to issues like accessing invalid memory, dereferencing NULL pointers, buffer overflows, uninitialized variables, memory leaks, or stack overflows. To fix it, carefully review your code, use debugging tools, check memory allocation, avoid buffer overflows, and handle NULL pointers properly. Without your specific code, it's hard to provide a precise solution.
To debug and fix a segmentation fault, you can follow these steps:

1.Check your code: Carefully review your code and look for the common issues mentioned above.

2.Use debugging tools: Use debugging tools like gdb (GNU Debugger) to analyze the segmentation fault. It can help pinpoint the line of code that's causing the issue.

3.Print statements: Insert print statements in your code to track the flow and values of variables before the segmentation fault occurs.

4.Check memory allocation and deallocation: Make sure you're properly allocating and deallocating memory, especially when using dynamic memory allocation functions like malloc and free.

5.Avoid buffer overflows: Double-check array or buffer accesses to ensure they're within their bounds.

6.Handle NULL pointers: Make sure you're handling pointers properly, checking for NULL values before dereferencing them.

According to my knowledge Segmentation fault usually occurs due to accessing memory that your program doesn't have permission to access, like null pointers or out-of-bounds arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.