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

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.