Indexes of Subarray Sum GFG Problem

 Given an array arr[] containing only non-negative integers, your task is to find a continuous subarray (a contiguous sequence of elements) whose sum equals a specified value target. You need to return the 1-based indices of the leftmost and rightmost elements of this subarray. You need to find the first subarray whose sum is equal to the target.


Note: If no such array is possible then, return [-1].


Examples:


Input: arr[] = [1, 2, 3, 7, 5], target = 12

Output: [2, 4]

Explanation: The sum of elements from 2nd to 4th position is 12.

Input: arr[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], target = 15

Output: [1, 5]

Explanation: The sum of elements from 1st to 5th position is 15.

Input: arr[] = [5, 3, 4], target = 2

Output: [-1]

Explanation: There is no subarray with sum 2.

Constraints:

1 <= arr.size()<= 106

0 <= arr[i] <= 103

0 <= target <= 109

'




//{ Driver Code Starts

import java.io.*;

import java.util.*;


class Main {

    public static void main(String args[]) throws IOException {

        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

        int t = Integer.parseInt(read.readLine().trim());


        while (t-- > 0) {

            String line = read.readLine().trim();

            String[] numsStr = line.split(" ");

            int[] nums = new int[numsStr.length];

            for (int i = 0; i < numsStr.length; i++) {

                nums[i] = Integer.parseInt(numsStr[i]);

            }


            int d = Integer.parseInt(read.readLine().trim());


            Solution ob = new Solution();

            ArrayList<Integer> result = ob.subarraySum(nums, d);

            // Print all elements in the result list

            for (int i : result) {

                System.out.print(i + " ");

            }

            System.out.println(); // Print a new line after the result

            System.out.println("~");

        }

    }

}


// } Driver Code Ends




class Solution {

    static ArrayList<Integer> subarraySum(int[] arr, int target) {

        // code here

                int n=arr.length;

        ArrayList<Integer> temp = new ArrayList<>();

        

        for(int i=0;i<n;i++)

        {

            int sum=0;

            for(int j=i;j<n;j++)

            {

                sum+=arr[j];

                if(sum==target)

                {

                    temp.add(i+1);//1-based indices 

                    temp.add(j+1);//1-based indices 

                    return temp;

                }

            }

            

        }

        temp.add(-1);

        return temp;

    }

}


Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence