#include <bits/stdc++.h> using namespace std; class Solution { public: vector<int> subarraySum(int arr[], int n, long long s) { long long sum=0; for(long long int i=0;i<n;i++){ sum=arr[i]; if(sum==s) return {i+1,i+1}; for(long long int j=i+1;j<n;j++){ sum+=arr[j]; if(sum==s) return {i+1 , j+1}; else if(sum>s) break; } } return {-1}; } }; int main() { int t; cin>>t; while(t--) { int n; long long s; cin>>n>>s; int arr[n]; const int mx = 1e9; for(int i=0;i<n;i++) { cin>>arr[i]; } Solution ob; vector<int>res; res = ob.subarraySum(arr, n, s); for(int i = 0;i<res.size();i++) cout<<res[i]<<" "; cout<<endl; } return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter