原文:

给定正整数数组 arr[] 和两个整数 lr 定义范围【l,r】。任务是打印总和在 lr 范围内的子阵列。

示例:

输入: arr[] = {1,4,6},l = 3,r = 8 输出: {1,4},{4},{6}。 说明:所有可能的子阵如下 {1】和为 1。 {1,4}与总和 5。 {1,4,6}与 sum 11。 {4}与 sum 4。 {4,6}加和 10。 {6}与 sum 6。 因此,子阵列{1,4}、{4}、{6}的和在范围[3,8]内。

输入: arr[] = {2,3,5,8},l = 4,r = 13 输出: {2,3},{2,3,5},{3,5},{5},{5,8},{8}。

方法:这个问题可以通过使用两个循环进行蛮力和检查每个可能的子阵列来解决。下面是上述方法的实现。

c

// c   program for above approach
#include 
using namespace std;
// function to find subarrays in given range
void subarraysum(int arr[], int n,
                 int leftsum, int rightsum)
{
    int curr_sum, i, j, res = 0;
    // pick a starting point
    for (i = 0; i < n; i  ) {
        curr_sum = arr[i];
        // try all subarrays starting with 'i'
        for (j = i   1; j <= n; j  ) {
            if (curr_sum > leftsum
                && curr_sum < rightsum) {
                cout << "{ ";
                for (int k = i; k < j; k  )
                    cout << arr[k] << " ";
                cout << "}\n";
            }
            if (curr_sum > rightsum || j == n)
                break;
            curr_sum = curr_sum   arr[j];
        }
    }
}
// driver code
int main()
{
    int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int l = 10, r = 23;
    subarraysum(arr, n, l, r);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java code for the above approach
import java.io.*;
class gfg
{
    // function to find subarrays in given range
    static void subarraysum(int arr[], int n, int leftsum,
                            int rightsum)
    {
        int curr_sum, i, j, res = 0;
        // pick a starting point
        for (i = 0; i < n; i  ) {
            curr_sum = arr[i];
            // try all subarrays starting with 'i'
            for (j = i   1; j <= n; j  ) {
                if (curr_sum > leftsum
                    && curr_sum < rightsum) {
                    system.out.print("{ ");
                    for (int k = i; k < j; k  )
                        system.out.print(arr[k]   " ");
                    system.out.println("}");
                }
                if (curr_sum > rightsum || j == n)
                    break;
                curr_sum = curr_sum   arr[j];
            }
        }
    }
    // driver code
    public static void main(string[] args)
    {
        int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 };
        int n = arr.length;
        int l = 10, r = 23;
        subarraysum(arr, n, l, r);
    }
}
// this code is contributed by potta lokesh

python 3

# python program for above approach
# function to find subarrays in given range
def subarraysum (arr, n, leftsum, rightsum):
    res = 0
    # pick a starting point
    for i in range(n):
        curr_sum = arr[i]
        # try all subarrays starting with 'i'
        for j in range(i   1, n   1):
            if (curr_sum > leftsum
                and curr_sum < rightsum):
                print("{ ", end="")
                for k in range(i, j):
                    print(arr[k], end=" ")
                print("}")
            if (curr_sum > rightsum or j == n):
                break
            curr_sum = curr_sum   arr[j]
# driver code
arr = [15, 2, 4, 8, 9, 5, 10, 23]
n = len(arr)
l = 10
r = 23
subarraysum(arr, n, l, r)
# this code is contributed by saurabh jaiswal

c

// c# code for the above approach
using system;
class gfg
{
    // function to find subarrays in given range
    static void subarraysum(int []arr, int n, int leftsum,
                            int rightsum)
    {
        int curr_sum, i, j, res = 0;
        // pick a starting point
        for (i = 0; i < n; i  ) {
            curr_sum = arr[i];
            // try all subarrays starting with 'i'
            for (j = i   1; j <= n; j  ) {
                if (curr_sum > leftsum
                    && curr_sum < rightsum) {
                    console.write("{ ");
                    for (int k = i; k < j; k  )
                        console.write(arr[k]   " ");
                    console.writeline("}");
                }
                if (curr_sum > rightsum || j == n)
                    break;
                curr_sum = curr_sum   arr[j];
            }
        }
    }
    // driver code
    public static void main()
    {
        int []arr = { 15, 2, 4, 8, 9, 5, 10, 23 };
        int n = arr.length;
        int l = 10, r = 23;
        subarraysum(arr, n, l, r);
    }
}
// this code is contributed by samim hossain mondal.

java 描述语言


output

{ 15 }
{ 15 2 }
{ 15 2 4 }
{ 2 4 8 }
{ 4 8 }
{ 4 8 9 }
{ 8 9 }
{ 8 9 5 }
{ 9 5 }
{ 5 10 }

时间复杂度: o(n^3)

辅助空间: o(1)