split the array there is an array val of n integers . A good subarray is defined as

To split the array into good subarrays, you can follow these steps in C++:

  1. Define the array val of size n and populate it with n integers.
  2. Initialize a variable count to 0 to keep track of the number of good subarrays.
  3. Initialize two pointers start and end to 0, representing the start and end indices of the current subarray.
  4. Start a loop that runs until the end pointer reaches the end of the array.
  5. Inside the loop, check if the current subarray is a good subarray.
  6. A subarray is considered good if the sum of its elements is divisible by the length of the subarray.
  7. Calculate the sum of the subarray using a variable sum.
  8. If sum is divisible by end - start + 1, increment count.
  9. Move the end pointer to the next index.
  10. If the end pointer is now at the end of the array, exit the loop.
  11. Inside the loop, move the start pointer to the next index.
  12. Repeat steps 5-8 until the start pointer reaches the end of the array.
  13. After the loop, the variable count will contain the number of good subarrays in the array val.

Here is an example implementation of the above steps in C++:

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter the size of the array: ";
    cin >> n;

    int val[n];
    cout << "Enter " << n << " integers: ";
    for (int i = 0; i < n; i++) {
        cin >> val[i];
    }

    int count = 0;
    for (int start = 0; start < n; start++) {
        for (int end = start; end < n; end++) {
            int sum = 0;
            for (int i = start; i <= end; i++) {
                sum += val[i];
            }
            if (sum % (end - start + 1) == 0) {
                count++;
            }
        }
    }

    cout << "Number of good subarrays: " << count << endl;

    return 0;
}

This code prompts the user to enter the size of the array and the array elements. It then calculates the number of good subarrays using nested loops and prints the result.