1.
Maximum Subarray Sum (Kadane’s Algorithm)
Problem: Find the contiguous subarray with the maximum sum.
C++ Solution:
cpp
Copy code
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
int maxSubArraySum(vector<int>& arr) {
int maxSum = INT_MIN, currSum = 0;
for (int num : arr) {
currSum = max(num, currSum + num);
maxSum = max(maxSum, currSum);
return maxSum;
int main() {
vector<int> arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
cout << "Max subarray sum: " << maxSubArraySum(arr) << endl;
return 0;
1. Maximum Subarray Sum (Kadane’s Algorithm)
Problem Statement:
Find the contiguous subarray with the maximum sum in a given array.
Input:
cpp
Copy code
arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4}
Output:
bash
Copy code
Max subarray sum: 6
Explanation:
Subarray [4, -1, 2, 1] has the maximum sum 4 + (-1) + 2 + 1 = 6.
2. Count Subarrays with Given Sum
Problem: Given an array and an integer k, count the number of subarrays whose sum
equals k.
C++ Solution (Optimized with HashMap):
cpp
Copy code
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int countSubarraysWithSumK(vector<int>& arr, int k) {
unordered_map<int, int> prefixSum;
int sum = 0, count = 0;
prefixSum[0] = 1;
for (int num : arr) {
sum += num;
if ([Link](sum - k) != [Link]()) {
count += prefixSum[sum - k];
prefixSum[sum]++;
return count;
int main() {
vector<int> arr = {1, 2, 3};
int k = 3;
cout << "Count of subarrays with sum " << k << ": " << countSubarraysWithSumK(arr, k) <<
endl;
return 0;
2. Count Subarrays with Given Sum
Problem Statement:
Given an array and an integer k, count the number of subarrays that sum to k.
Input:
cpp
Copy code
arr = {1, 2, 3}
k=3
Output:
python
Copy code
Count of subarrays with sum 3: 2
Explanation:
Subarrays [1, 2] and [3] both have a sum of 3.
3. Longest Subarray with Sum Zero
Problem: Find the length of the longest subarray with sum 0.
C++ Solution:
cpp
Copy code
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int longestZeroSumSubarray(vector<int>& arr) {
unordered_map<int, int> sumIndex;
int sum = 0, maxLen = 0;
for (int i = 0; i < [Link](); i++) {
sum += arr[i];
if (sum == 0) maxLen = i + 1;
else if ([Link](sum) != [Link]()) {
maxLen = max(maxLen, i - sumIndex[sum]);
} else {
sumIndex[sum] = i;
}
return maxLen;
int main() {
vector<int> arr = {15, -2, 2, -8, 1, 7, 10, 23};
cout << "Length of longest zero sum subarray: " << longestZeroSumSubarray(arr) << endl;
return 0;
Saved memory full
You said:
ChatGPT said:
The problem statement in the image can be summarized as:
Problem:
Given a string S of length N where each character is a digit from 0 to 9, find the number of
substrings where the sum of digits in the substring is not equal to the length of that
substring.
Input:
• Length of the string N = 201
• String S contains digits (0 to 9)
Output:
• A single integer: the number of such substrings where sum ≠ length.
Would you like help writing an optimized code for this problem or understanding the logic
behind it?
You said:
ChatGPT said:
The image contains a problem about Armstrong numbers. Here's a clean breakdown of the
problem and the expected solution:
Problem Statement:
Given an integer n, return True if it is an Armstrong number, otherwise return False.
Definition:
An Armstrong number is a number that is equal to the sum of its digits each raised to the
power of the number of digits.
For example:
• 153=13+53+33=153153=13+53+33=153
• 371=33+73+13=371371=33+73+13=371
• 108=13+03+83=513≠108108=13+03+83=513 =108
Input:
A list of integers, e.g. [153, 371, 108]
Output:
The Armstrong numbers in the list: 153 371
Sample Python Code:
python
Copy code
def is_armstrong(n): digits = str(n) power = len(digits) total = sum(int(digit) ** power for
digit in digits) return total == n def find_armstrong_numbers(numbers): return [num for num
in numbers if is_armstrong(num)] # Example usage: nums = [153, 371, 108] result =
find_armstrong_numbers(nums) print(*result) # Output: 153 371
Would you like the same in Java, C++, or any other language?
You said:
in cpp
ChatGPT said:
Here is the C++ version of the Armstrong number checker and solution for a list of numbers:
cpp
Copy code
#include <iostream> #include <vector> #include <cmath> using namespace std; // Function
to check if a number is an Armstrong number bool isArmstrong(int n) { int original = n; int
sum = 0; int digits = 0; int temp = n; // Count the number of digits while (temp > 0) { digits++;
temp /= 10; } temp = n; // Compute the sum of each digit raised to the power of number of
digits while (temp > 0) { int digit = temp % 10; sum += pow(digit, digits); temp /= 10; } return
sum == original; } // Function to return all Armstrong numbers from a vector vector<int>
findArmstrongNumbers(const vector<int>& nums) { vector<int> result; for (int num : nums) {
if (isArmstrong(num)) { result.push_back(num); } } return result; } int main() { vector<int>
nums = {153, 371, 108}; vector<int> armstrongs = findArmstrongNumbers(nums); for (int
num : armstrongs) { cout << num << " "; } return 0; }
Output:
Copy code
153 371
Let me know if you want to take input dynamically from the user or via command-line
arguments.
You said:
ChatGPT said:
The problem described in the image is:
Problem Statement:
You are given candidate scores, and you need to print the candidates in descending order of
their scores.
Input Format:
• First input: An integer N representing the number of entries.
• Then 2 * N values follow: Each candidate name followed by their score.
Example input:
Copy code
3 90 John 85 Bob 92 Alice 110 Aditya 100 Alice
But based on the highlighted part, the corrected input seems like:
Copy code
3 90 John 85 Bob 92 Alice 110 Aditya 100 Alice
This can be interpreted as:
Name Score
John 90
Bob 85
Alice 92
Aditya 110
Alice 100
But in the image, Alice only has 100, so perhaps repeated names overwrite previous scores
instead of accumulating them.
So, interpreting it with overwrite behavior:
Name Latest Score
John 90
Bob 85
Alice 100
Aditya 110
Expected Output:
Print the names and scores sorted by descending score:
makefile
Copy code
Aditya: 110
Alice: 100
John: 90
C++ Code:
cpp
Copy code
#include <iostream> #include <map> #include <vector> #include <algorithm> using
namespace std; int main() { int n; cin >> n; map<string, int> scores; for (int i = 0; i < n; i++) {
int score; string name; cin >> score >> name; scores[name] = score; // Overwrite if name
already exists } // Move map data to vector for sorting vector<pair<string, int>>
result([Link](), [Link]()); // Sort by score descending sort([Link](),
[Link](), [](auto &a, auto &b) { return [Link] > [Link]; }); // Output for (auto &entry
: result) { cout << [Link] << ": " << [Link] << endl; } return 0; }
Let me know if you'd prefer score accumulation (like summing multiple entries for the same
person), or this overwrite approach is what you want.
You said:
ChatGPT said:
The problem in the image is:
Problem Statement:
Rearrange the array such that:
• The first half is in increasing order
• The second half is in decreasing order
Examples:
Example 1
Input: 8 7 1 6 5 9
Output: 1 5 6 9 8 7
Example 2
Input: 4 2 8 6 15 5 9 20
Output: 2 4 5 6 20 15 9 8
C++ Code:
cpp
Copy code
#include <iostream> #include <vector> #include <algorithm> using namespace std; void
rearrangeArray(vector<int>& arr) { sort([Link](), [Link]()); // Step 1: Sort entire array int
n = [Link](); int mid = n / 2; vector<int> result; // First half in increasing order for (int i = 0; i
< mid; i++) { result.push_back(arr[i]); } // Second half in decreasing order for (int i = n - 1; i >=
mid; i--) { result.push_back(arr[i]); } // Output for (int val : result) { cout << val << " "; } cout
<< endl; } int main() { vector<int> arr = {4, 2, 8, 6, 15, 5, 9, 20}; rearrangeArray(arr); return 0;
}
Let me know if you want to take input from user, or implement this using arrays instead of
vectors.
You said:
give all the subarray question which is asked in tcs nqt along with solution in cpp
ChatGPT said:
Here’s a list of commonly asked subarray-related questions in TCS NQT, along with C++
solutions for each. These questions cover a wide range of subarray concepts (sum, max/min,
count, etc.) and are great for interview prep.
1. Maximum Subarray Sum (Kadane’s Algorithm)
Problem: Find the contiguous subarray with the maximum sum.
C++ Solution:
cpp
Copy code
#include <iostream> #include <vector> #include <climits> using namespace std; int
maxSubArraySum(vector<int>& arr) { int maxSum = INT_MIN, currSum = 0; for (int num : arr)
{ currSum = max(num, currSum + num); maxSum = max(maxSum, currSum); } return
maxSum; } int main() { vector<int> arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; cout << "Max subarray
sum: " << maxSubArraySum(arr) << endl; return 0; }
2. Count Subarrays with Given Sum
Problem: Given an array and an integer k, count the number of subarrays whose sum
equals k.
C++ Solution (Optimized with HashMap):
cpp
Copy code
#include <iostream> #include <unordered_map> #include <vector> using namespace std; int
countSubarraysWithSumK(vector<int>& arr, int k) { unordered_map<int, int> prefixSum; int
sum = 0, count = 0; prefixSum[0] = 1; for (int num : arr) { sum += num; if ([Link](sum
- k) != [Link]()) { count += prefixSum[sum - k]; } prefixSum[sum]++; } return count; }
int main() { vector<int> arr = {1, 2, 3}; int k = 3; cout << "Count of subarrays with sum " << k
<< ": " << countSubarraysWithSumK(arr, k) << endl; return 0; }
3. Longest Subarray with Sum Zero
Problem: Find the length of the longest subarray with sum 0.
C++ Solution:
cpp
Copy code
#include <iostream> #include <unordered_map> #include <vector> using namespace std; int
longestZeroSumSubarray(vector<int>& arr) { unordered_map<int, int> sumIndex; int sum =
0, maxLen = 0; for (int i = 0; i < [Link](); i++) { sum += arr[i]; if (sum == 0) maxLen = i + 1; else
if ([Link](sum) != [Link]()) { maxLen = max(maxLen, i - sumIndex[sum]); }
else { sumIndex[sum] = i; } } return maxLen; } int main() { vector<int> arr = {15, -2, 2, -8, 1, 7,
10, 23}; cout << "Length of longest zero sum subarray: " << longestZeroSumSubarray(arr) <<
endl; return 0; }
4. Maximum Product Subarray
Problem: Find the maximum product of a contiguous subarray.
C++ Solution:
cpp
Copy code
#include <iostream> #include <vector> #include <algorithm> using namespace std; int
maxProductSubarray(vector<int>& nums) { int maxProd = nums[0], minProd = nums[0],
result = nums[0]; for (int i = 1; i < [Link](); i++) { if (nums[i] < 0) swap(maxProd, minProd);
maxProd = max(nums[i], maxProd * nums[i]); minProd = min(nums[i], minProd * nums[i]);
result = max(result, maxProd); } return result; } int main() { vector<int> nums = {2, 3, -2, 4};
cout << "Max product subarray: " << maxProductSubarray(nums) << endl; return 0; }
5. Number of Subarrays with Equal 0s and 1s
Problem: Given a binary array, count subarrays with equal number of 0s and 1s.
C++ Solution (Convert 0 to -1 and use prefix sum logic):
cpp
Copy code
#include <iostream> #include <vector> #include <unordered_map> using namespace std; int
countSubarraysEqual0and1(vector<int>& nums) { unordered_map<int, int> mp; int sum = 0,
count = 0; mp[0] = 1; for (int num : nums) { sum += (num == 0) ? -1 : 1; if ([Link](sum) !=
[Link]()) { count += mp[sum]; } mp[sum]++; } return count; } int main() { vector<int> nums
= {0, 0, 1, 0, 1, 1}; cout << "Count of subarrays with equal 0s and 1s: " <<
countSubarraysEqual0and1(nums) << endl; return 0; }
6. Print All Subarrays
Problem: Print all subarrays of a given array.
C++ Solution:
cpp
Copy code
#include <iostream> #include <vector> using namespace std; void
printAllSubarrays(vector<int>& arr) { int n = [Link](); for (int start = 0; start < n; start++) { for
(int end = start; end < n; end++) { for (int i = start; i <= end; i++) { cout << arr[i] << " "; } cout
<< endl; } } } int main() { vector<int> arr = {1, 2, 3}; printAllSubarrays(arr); return 0; }
Would you like this list in PDF or DOCX format for download? Or do you want me to include
any sliding window-based subarray problems too?
2/2
You said:
give more question which is asked on subaaray also give problem statemet input and output
of each question
ChatGPT said:
Absolutely! Here are additional subarray questions frequently asked in TCS NQT along with:
• Problem Statement
• Input Format
• Expected Output
• C++ Solution
These are based on commonly tested concepts like sliding window, prefix sums, two-
pointers, and brute force.
7. Longest Subarray with Sum K (Positive integers only)
Problem Statement:
Given an array of positive integers and an integer k, find the length of the longest
subarray whose sum is equal to k.
Input:
ini
Copy code
n=7
arr = [1, 2, 1, 0, 1, 1, 0]
k=4
Output:
Copy code
C++ Solution (Two Pointer):
cpp
Copy code
#include <iostream> #include <vector> using namespace std; int
longestSubarrayWithSumK(vector<int>& arr, int k) { int left = 0, sum = 0, maxLen = 0; for (int
right = 0; right < [Link](); right++) { sum += arr[right]; while (sum > k) { sum -= arr[left++]; } if
(sum == k) { maxLen = max(maxLen, right - left + 1); } } return maxLen; } int main() {
vector<int> arr = {1, 2, 1, 0, 1, 1, 0}; int k = 4; cout << longestSubarrayWithSumK(arr, k) <<
endl; return 0; }
8. Smallest Subarray with Sum ≥ K
Problem Statement:
Given an array and a value k, find the length of the smallest subarray whose sum is greater
than or equal to k.
Input:
ini
Copy code
arr = [2, 3, 1, 2, 4, 3], k = 7
Output:
Copy code
C++ Solution:
cpp
Copy code
#include <iostream> #include <vector> #include <climits> using namespace std; int
minSubArrayLen(int k, vector<int>& nums) { int left = 0, sum = 0, minLen = INT_MAX; for (int
right = 0; right < [Link](); right++) { sum += nums[right]; while (sum >= k) { minLen =
min(minLen, right - left + 1); sum -= nums[left++]; } } return minLen == INT_MAX ? 0 :
minLen; } int main() { vector<int> nums = {2, 3, 1, 2, 4, 3}; int k = 7; cout <<
minSubArrayLen(k, nums) << endl; return 0; }
9. Subarray with 0 Sum Exists
Problem Statement:
Check if a subarray with 0 sum exists in the array.
Input:
ini
Copy code
arr = [4, 2, -3, 1, 6]
Output:
yaml
Copy code
Yes
C++ Solution:
cpp
Copy code
#include <iostream> #include <vector> #include <unordered_set> using namespace std; bool
isZeroSumSubarray(vector<int>& arr) { unordered_set<int> prefixSums; int sum = 0; for (int
num : arr) { sum += num; if (sum == 0 || [Link](sum)) return true;
[Link](sum); } return false; } int main() { vector<int> arr = {4, 2, -3, 1, 6}; cout <<
(isZeroSumSubarray(arr) ? "Yes" : "No") << endl; return 0; }
10. Count All Subarrays with Even Sum
Problem Statement:
Given an array, count the number of subarrays whose sum is even.
Input:
ini
Copy code
arr = [1, 2, 3, 4]
Output:
Copy code
4
C++ Solution:
cpp
Copy code
#include <iostream> #include <vector> using namespace std; int
countEvenSumSubarrays(vector<int>& arr) { int count = 0, sum = 0; int even = 1, odd = 0; for
(int num : arr) { sum += num; if (sum % 2 == 0) { count += even; even++; } else { count +=
odd; odd++; } } return count; } int main() { vector<int> arr = {1, 2, 3, 4}; cout <<
countEvenSumSubarrays(arr) << endl; return 0; }
11. Maximum Sum Subarray of Size K (Sliding Window)
Problem Statement:
Find the maximum sum of a subarray with size k.
Input:
ini
Copy code
arr = [2, 1, 5, 1, 3, 2], k = 3
Output:
Copy code
C++ Solution:
cpp
Copy code
#include <iostream> #include <vector> using namespace std; int
maxSumSubarrayOfSizeK(vector<int>& arr, int k) { int maxSum = 0, windowSum = 0; for (int i
= 0; i < k; i++) windowSum += arr[i]; maxSum = windowSum; for (int i = k; i < [Link](); i++) {
windowSum += arr[i] - arr[i - k]; maxSum = max(maxSum, windowSum); } return maxSum; }
int main() { vector<int> arr = {2, 1, 5, 1, 3, 2}; int k = 3; cout << maxSumSubarrayOfSizeK(arr,
k) << endl; return 0; }
Do you want all these combined into a PDF or document for practice and revision?
I can also include:
• Dry run examples
• Edge cases
• Time complexity analysis
Let me know!
You said:
1. Maximum Subarray Sum (Kadane’s Algorithm) Problem: Find the contiguous subarray
with the maximum sum. C++ Solution: cpp Copy Edit #include <iostream> #include
<vector> #include <climits> using namespace std; int maxSubArraySum(vector<int>& arr) {
int maxSum = INT_MIN, currSum = 0; for (int num : arr) { currSum = max(num, currSum +
num); maxSum = max(maxSum, currSum); } return maxSum; } int main() { vector<int> arr = {-
2, 1, -3, 4, -1, 2, 1, -5, 4}; cout << "Max subarray sum: " << maxSubArraySum(arr) << endl;
return 0; } 2. Count Subarrays with Given Sum Problem: Given an array and an integer k,
count the number of subarrays whose sum equals k. C++ Solution (Optimized with
HashMap): cpp Copy Edit #include <iostream> #include <unordered_map> #include
<vector> using namespace std; int countSubarraysWithSumK(vector<int>& arr, int k) {
unordered_map<int, int> prefixSum; int sum = 0, count = 0; prefixSum[0] = 1; for (int num :
arr) { sum += num; if ([Link](sum - k) != [Link]()) { count += prefixSum[sum -
k]; } prefixSum[sum]++; } return count; } int main() { vector<int> arr = {1, 2, 3}; int k = 3; cout
<< "Count of subarrays with sum " << k << ": " << countSubarraysWithSumK(arr, k) << endl;
return 0; } 3. Longest Subarray with Sum Zero Problem: Find the length of the longest
subarray with sum 0. C++ Solution: cpp Copy Edit #include <iostream> #include
<unordered_map> #include <vector> using namespace std; int
longestZeroSumSubarray(vector<int>& arr) { unordered_map<int, int> sumIndex; int sum =
0, maxLen = 0; for (int i = 0; i < [Link](); i++) { sum += arr[i]; if (sum == 0) maxLen = i + 1; else
if ([Link](sum) != [Link]()) { maxLen = max(maxLen, i - sumIndex[sum]); }
else { sumIndex[sum] = i; } } return maxLen; } int main() { vector<int> arr = {15, -2, 2, -8, 1, 7,
10, 23}; cout << "Length of longest zero sum subarray: " << longestZeroSumSubarray(arr) <<
endl; return 0; } 4. Maximum Product Subarray Problem: Find the maximum product of a
contiguous subarray. C++ Solution: cpp Copy Edit #include <iostream> #include <vector>
#include <algorithm> using namespace std; int maxProductSubarray(vector<int>& nums) {
int maxProd = nums[0], minProd = nums[0], result = nums[0]; for (int i = 1; i < [Link]();
i++) { if (nums[i] < 0) swap(maxProd, minProd); maxProd = max(nums[i], maxProd * nums[i]);
minProd = min(nums[i], minProd * nums[i]); result = max(result, maxProd); } return result; }
int main() { vector<int> nums = {2, 3, -2, 4}; cout << "Max product subarray: " <<
maxProductSubarray(nums) << endl; return 0; } 5. Number of Subarrays with Equal 0s
and 1s Problem: Given a binary array, count subarrays with equal number of 0s and 1s.
C++ Solution (Convert 0 to -1 and use prefix sum logic): cpp Copy Edit #include <iostream>
#include <vector> #include <unordered_map> using namespace std; int
countSubarraysEqual0and1(vector<int>& nums) { unordered_map<int, int> mp; int sum = 0,
count = 0; mp[0] = 1; for (int num : nums) { sum += (num == 0) ? -1 : 1; if ([Link](sum) !=
[Link]()) { count += mp[sum]; } mp[sum]++; } return count; } int main() { vector<int> nums
= {0, 0, 1, 0, 1, 1}; cout << "Count of subarrays with equal 0s and 1s: " <<
countSubarraysEqual0and1(nums) << endl; return 0; } 6. Print All Subarrays Problem:
Print all subarrays of a given array. C++ Solution: cpp Copy Edit #include <iostream>
#include <vector> using namespace std; void printAllSubarrays(vector<int>& arr) { int n =
[Link](); for (int start = 0; start < n; start++) { for (int end = start; end < n; end++) { for (int i =
start; i <= end; i++) { cout << arr[i] << " "; } cout << endl; } } } int main() { vector<int> arr = {1,
2, 3}; printAllSubarrays(arr); return 0; } give input and output of all this questions
ChatGPT said:
Sure! Below are the input-output examples for each of the 6 subarray-based problems
you've mentioned. These examples are designed to clarify the behavior and expected result
of each algorithm.
1. Maximum Subarray Sum (Kadane’s Algorithm)
Problem Statement:
Find the contiguous subarray with the maximum sum in a given array.
Input:
cpp
Copy code
arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4}
Output:
bash
Copy code
Max subarray sum: 6
Explanation:
Subarray [4, -1, 2, 1] has the maximum sum 4 + (-1) + 2 + 1 = 6.
2. Count Subarrays with Given Sum
Problem Statement:
Given an array and an integer k, count the number of subarrays that sum to k.
Input:
cpp
Copy code
arr = {1, 2, 3} k = 3
Output:
python
Copy code
Count of subarrays with sum 3: 2
Explanation:
Subarrays [1, 2] and [3] both have a sum of 3.
3. Longest Subarray with Sum Zero
Problem Statement:
Find the length of the longest subarray with a sum equal to 0.
Input:
cpp
Copy code
arr = {15, -2, 2, -8, 1, 7, 10, 23}
Output:
bash
Copy code
Length of longest zero sum subarray: 5
Explanation:
Subarray [-2, 2, -8, 1, 7] has a sum of 0 and length 5.
4. Maximum Product Subarray
Problem: Find the maximum product of a contiguous subarray.
C++ Solution:
cpp
Copy code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int maxProductSubarray(vector<int>& nums) {
int maxProd = nums[0], minProd = nums[0], result = nums[0];
for (int i = 1; i < [Link](); i++) {
if (nums[i] < 0) swap(maxProd, minProd);
maxProd = max(nums[i], maxProd * nums[i]);
minProd = min(nums[i], minProd * nums[i]);
result = max(result, maxProd);
return result;
4. Maximum Product Subarray
Problem Statement:
Find the maximum product of any contiguous subarray.
Input:
cpp
Copy code
nums = {2, 3, -2, 4}
Output:
yaml
Copy code
Max product subarray: 6
Explanation:
Subarray [2, 3] has the maximum product 2 * 3 = 6.
int main() {
vector<int> nums = {2, 3, -2, 4};
cout << "Max product subarray: " << maxProductSubarray(nums) << endl;
return 0;
5. Number of Subarrays with Equal 0s and 1s
Problem: Given a binary array, count subarrays with equal number of 0s and 1s.
C++ Solution (Convert 0 to -1 and use prefix sum logic):
cpp
Copy code
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int countSubarraysEqual0and1(vector<int>& nums) {
unordered_map<int, int> mp;
int sum = 0, count = 0;
mp[0] = 1;
for (int num : nums) {
sum += (num == 0) ? -1 : 1;
if ([Link](sum) != [Link]()) {
count += mp[sum];
mp[sum]++;
return count;
int main() {
vector<int> nums = {0, 0, 1, 0, 1, 1};
cout << "Count of subarrays with equal 0s and 1s: " << countSubarraysEqual0and1(nums)
<< endl;
return 0;
5. Number of Subarrays with Equal 0s and 1s
Problem Statement:
Count subarrays with equal number of 0s and 1s in a binary array.
Input:
cpp
Copy code
nums = {0, 0, 1, 0, 1, 1}
Output:
csharp
Copy code
Count of subarrays with equal 0s and 1s: 9
Explanation:
After converting 0s to -1s, the problem becomes finding subarrays with a sum of 0.
6. Print All Subarrays
Problem: Print all subarrays of a given array.
C++ Solution:
cpp
Copy code
#include <iostream>
#include <vector>
using namespace std;
void printAllSubarrays(vector<int>& arr) {
int n = [Link]();
for (int start = 0; start < n; start++) {
for (int end = start; end < n; end++) {
for (int i = start; i <= end; i++) {
cout << arr[i] << " ";
cout << endl;
}
6. Print All Subarrays
Problem Statement:
Print all contiguous subarrays of a given array.
Input:
cpp
Copy code
arr = {1, 2, 3}
Output:
Copy code
12
123
23
Explanation:
All possible contiguous subarrays are printed in order of start and end indices.
7. Longest Subarray with Sum K (Positive integers only)
Problem Statement:
Given an array of positive integers and an integer k, find the length of the longest subarray
whose sum is equal to k.
Input:
ini
Copy code
n=7
arr = [1, 2, 1, 0, 1, 1, 0]
k=4
Output:
Copy code
C++ Solution (Two Pointer):
cpp
Copy code
#include <iostream>
#include <vector>
using namespace std;
int longestSubarrayWithSumK(vector<int>& arr, int k) {
int left = 0, sum = 0, maxLen = 0;
for (int right = 0; right < [Link](); right++) {
sum += arr[right];
while (sum > k) {
sum -= arr[left++];
if (sum == k) {
maxLen = max(maxLen, right - left + 1);
return maxLen;
int main() {
vector<int> arr = {1, 2, 1, 0, 1, 1, 0};
int k = 4;
cout << longestSubarrayWithSumK(arr, k) << endl;
return 0;
8. Smallest Subarray with Sum ≥ K
Problem Statement:
Given an array and a value k, find the length of the smallest subarray whose sum is greater
than or equal to k.
Input:
ini
Copy code
arr = [2, 3, 1, 2, 4, 3], k = 7
Output:
Copy code
C++ Solution:
cpp
Copy code
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
int minSubArrayLen(int k, vector<int>& nums) {
int left = 0, sum = 0, minLen = INT_MAX;
for (int right = 0; right < [Link](); right++) {
sum += nums[right];
while (sum >= k) {
minLen = min(minLen, right - left + 1);
sum -= nums[left++];
return minLen == INT_MAX ? 0 : minLen;
int main() {
vector<int> nums = {2, 3, 1, 2, 4, 3};
int k = 7;
cout << minSubArrayLen(k, nums) << endl;
return 0;
9. Subarray with 0 Sum Exists
Problem Statement:
Check if a subarray with 0 sum exists in the array.
Input:
ini
Copy code
arr = [4, 2, -3, 1, 6]
Output:
yaml
Copy code
Yes
C++ Solution:
cpp
Copy code
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
bool isZeroSumSubarray(vector<int>& arr) {
unordered_set<int> prefixSums;
int sum = 0;
for (int num : arr) {
sum += num;
if (sum == 0 || [Link](sum)) return true;
[Link](sum);
return false;
int main() {
vector<int> arr = {4, 2, -3, 1, 6};
cout << (isZeroSumSubarray(arr) ? "Yes" : "No") << endl;
return 0;
10. Count All Subarrays with Even Sum
Problem Statement:
Given an array, count the number of subarrays whose sum is even.
Input:
ini
Copy code
arr = [1, 2, 3, 4]
Output:
Copy code
C++ Solution:
cpp
Copy code
#include <iostream>
#include <vector>
using namespace std;
int countEvenSumSubarrays(vector<int>& arr) {
int count = 0, sum = 0;
int even = 1, odd = 0;
for (int num : arr) {
sum += num;
if (sum % 2 == 0) {
count += even;
even++;
} else {
count += odd;
odd++;
return count;
}
int main() {
vector<int> arr = {1, 2, 3, 4};
cout << countEvenSumSubarrays(arr) << endl;
return 0;
11. Maximum Sum Subarray of Size K (Sliding Window)
Problem Statement:
Find the maximum sum of a subarray with size k.
Input:
ini
Copy code
arr = [2, 1, 5, 1, 3, 2], k = 3
Output:
Copy code
C++ Solution:
cpp
Copy code
#include <iostream>
#include <vector>
using namespace std;
int maxSumSubarrayOfSizeK(vector<int>& arr, int k) {
int maxSum = 0, windowSum = 0;
for (int i = 0; i < k; i++) windowSum += arr[i];
maxSum = windowSum;
for (int i = k; i < [Link](); i++) {
windowSum += arr[i] - arr[i - k];
maxSum = max(maxSum, windowSum);
return maxSum;
int main() {
vector<int> arr = {2, 1, 5, 1, 3, 2};
int k = 3;
cout << maxSumSubarrayOfSizeK(arr, k) << endl;
return 0;
int main() {
vector<int> arr = {1, 2, 3};
printAllSubarrays(arr);
return 0;