Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Ashwin Ramesh
Solution with different order of sub arrays not accepted

Ashwin Ramesh

Jun 23, 2023

My solution lists the sub arrays in a different order but it is not getting accepted. Also I don't understand how best case is of cubic complexity. Am I missing something in my solution? Feel like this is getting done in O(n*n) complexity?

public static List<List<Integer>> findSubarrays(int[] arr, int target) { List<List<Integer>> result = new ArrayList<>(); for(int i=0;i<arr.length;i++) { if(arr[i]<target) { result.add(Arrays.asList(arr[i])); } else { continue; } int product=arr[i]; List<Integer> subArray = new ArrayList<>(); subArray.add(arr[i]); for(int j=i+1;j<arr.length;j++) { product*=arr[j]; if(product<target) { subArray.add(arr[j]); result.add(new ArrayList<>(subArray)); } } } return result; }

2

0

Comments
Comments
edisonfreire14
edisonfreire14 2 years ago

its because it asks for CONTIGUOUS subarrays