Given an unsorted array of nonnegative integers, find a continous subarray which adds to a given number.
Examples:
Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33 Ouptut: Sum found between indexes 2 and 4 Input: arr[] = {1, 4, 0, 0, 3, 10, 5}, sum = 7 Ouptut: Sum found between indexes 1 and 4 Input: arr[] = {1, 4}, sum = 0 Output: No subarray found
There may be more than one subarrays with sum as the given sum. The following solutions print first such subarray.
public boolean subArraySumK(int[] A, int k) { int n = A.length; if(n == 0) return false; int start = 0, sum = 0; for(int i=0; i<n; i++) { sum += A[i]; while(sum > k && start < i) { sum -= A[start++]; } if(sum == k) { System.out.println("find subarray from "+start +" to " + i); return true; } } System.out.println("no subarray found"); return false; }
Time complexity of this method looks more than O(n), but if we take a closer look at the program, then we can figure out the time complexity is O(n). We can prove it by counting the number of operations performed on every element of arr[] in worst case. There are at most 2 operations performed on every element: (a) the element is added to the curr_sum (b) the element is subtracted from curr_sum. So the upper bound on number of operations is 2n which is O(n).
Reference:
相关推荐
c语言入门 c语言_leetcode题解之0560_subarray_sum_equals_k
javascript js_leetcode题解之152-maximum-product-subarray.js
js js_leetcode题解之53-maximum-subarray.js
c语言入门 C语言_leetcode题解之53-maximum-subarray.c
java java_leetcode题解之Maximum Subarray Sum with One Deletion.java
最大子数组总和问题给定一个整数数组,找到一个具有最大和的序列。 看一个例子: let array = [ 1 , - 1 , 5 , 3 , - 7 , 4 , 5 , 6 , - 100 , 4 ]function largestSubarraySum ( array ) { // code to write here}...
c++ C++_leetcode题解之1310_XOR_Queries_of_a_Subarray.cpp
python python_leetcode题解之152_Maximum_Product_Subarray.py
java java_leetcode题解之Continuous Subarray Sum.java
在MATLAB中,`subarray`是一个非常重要的概念,它涉及到数组操作的核心部分。当我们处理大型数据集时,经常需要从大数组中提取出一部分,也就是子数组,来进行特定的计算或分析。`subarray`操作允许我们有效地访问和...
最大子数组和问题是一个经典的计算机科学问题,它在算法设计和数据结构领域有着广泛的应用。在Java编程中,解决这个问题可以采用多种不同的方法,每种方法都有其特定的时间复杂度。下面将详细介绍三种不同时间复杂度...
word源码java 玩儿转算法面试 - 课程学习笔记 课程的学习笔记。 课程笔记目录 第一章:算法面试到底是什么鬼 第二章:面试中的复杂度分析 第三章:数组中的问题其实最常见 ...Subarray Sum 3-8 在滑动窗口中做记
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组...
子阵列划分的root-music algorithm
1.图案:推拉窗 大小为K的最大总和子数组(简单) 具有给定总和的最小子数组(简单) 最长的具有K个不同字符的子字符串(中) 水果入篮(中) 不重复子字符串(硬)* 替换后具有相同字母的最长子字符串(硬) ...
最大子数组总和 问题 给定一个整数数组,找到一个具有最大和的序列。 例如,看下面的例子。 let array = [ 1 , - 1 , 5 , 3 , - 7 , 4 , 5 , 6 , - 100 , 4 ] function largestSubarraySum ( array ) { ...
leetcode双人赛 1. Pattern: ...given sum (easy) Longest Substring with K Distinct Characters (medium) Fruits into Baskets (medium) No-repeat Substring (hard) Longest Substring with Same Le
给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的子数组。如果存在该子数组返回true,否则返回false。 #include #include using namespace std; int main() { std::cout <... subset[i
在这个特定的题目"Subarray SUM dynamicprogramming_"中,我们关注的是找出一个数组中和为S的所有子数组的数量,这个问题可以通过动态规划有效地解决。下面我们将深入探讨这个话题。 首先,我们需要了解什么是子...