C Exercises: Find a subarray with given sum from the given array
47. Subarray with Given Sum
Write a program in C to find a subarray with a given sum from the given array.
The program searches for subarrays within a given array whose elements sum to a specified value. It iterates through the array, calculating the sum of subarrays starting from each index and prints those subarrays that match the desired sum.
Sample Solution:
C Code:
Sample Output:
The given array is : 3 4 -7 1 3 3 1 -4 [0..1] -- { 3 4 } [0..5] -- { 3 4 -7 1 3 3 } [3..5] -- { 1 3 3 } [4..6] -- { 3 3 1 }
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C program to find all subarrays with a given sum using nested loops.
- Write a C program to identify a subarray with a given sum using the sliding window technique.
- Write a C program to find a subarray with a specified sum using recursion.
- Write a C program to detect and print all subarrays that add up to a given sum.
C Programming Code Editor:
Previous:Write a program in C to find the smallest positive number missing from an unsorted array.
Next: Write a program in C to find if a given integer x appears more than n/2 times in a sorted array of n integers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.