Subarray Product Equals Target

This program finds subarray with product equal to target.

Problem Statement

Write a Java program to find subarray with product equal to 60.

Source Code

1 public class SubarrayProductEqualsTarget {
2 public static void main(String[] args) {
3 System.out.println("Eduinq Subarray Product Equals Target");
4 int[] arr = {2,3,4,5};
5 int target=60;
6 for(int i=0;i<arr.length;i++){
7 int prod=1;
8 for(int j=i;j<arr.length;j++){
9 prod*=arr[j];
10 if(prod==target){
11 System.out.println("Subarray found from index "+i+" to "+j);
12 return;
13 }
14 }
15 }
16 System.out.println("No subarray found");
17 }
18 }

Program Output

Eduinq Subarray Product Equals Target
Subarray found from index 1 to 3

Explanation

Nested loops compute the running product for every possible subarray, and when the product matches the target, the starting and ending indices are printed, demonstrating a subarray product search.

Quick Links to Explore