1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
BigorU [14]
3 years ago
9

Given an array of non-negative integers arr , your task is to find the number of ways it can be split into three non-empty conti

guous subarrays such that the sum of the elements in the first subarray is less than or equal to the sum of the elements in the second subarray, and the sum of the elements in the second subarray is less than or equal to the sum of the elements in the third subarray.
Note:
• Each element of arr must appear in exactly one of the three contiguous subarrays.
• Although the given numbers are 32-bit integers, the sum of the elements may exceed the limits of the 32-bit integer type. .

Computers and Technology
1 answer:
Bad White [126]3 years ago
7 0

Answer:

Here is the C++ program:

#include<iostream>  //to use input output functions

using namespace std;  // to identify objects cin cout

int CountSubArrays(int arr[], int size){   /*function that an array and the size of array as parameters and returns the number of ways it can be split into three non-empty contiguous subarrays such that the sum of the elements in the first subarray is less than or equal to the sum of the elements in the second subarray, and the sum of the elements in the second subarray is less than or equal to the sum of the elements in the third subarray */

   int previous[size];  //stores the sum of previous elements

   previous[0] = arr[0];   //sets the first element of previous array to first element of arr

   for(int i = 1; i < size; i++)  //iterates through the array

previous[i] = previous[i - 1] + arr[i];   //adds the present element i-th element of arr to the previous element of previous array

int next[size];   //to store the sum of next elements

next[size - 1] = arr[size - 1]; //sets the last element of previous array to last element of arr

    for(int i = size ; i >= 0; i--)    //iterates through the array

next[i] = next[i + 1] + arr[i];   //adds the present element i-th element of arr to the next element of next array

int i = 1, j = 1;   //sets two variable to move through the array

int current = 0, count = 0;  

while (i < size - 1 && j < size - 1)  { //moves through the array  

 while (j < size - 1 && current <=previous[i - 1])  {  // updates current array until  it is less than previous[i - 1]

 current += arr[j++];  }   //adds value of current to j-th+1 value of arr

 if (current <= next[j])   {  //if value of current is less than or equal to the j-th value of next  

  count++;    }   //adds 1 to the count and count stores number of ways array can be split

 current -= arr[i++];   }   //decrements current by arr[i++]

return count;  } //returns the number of ways arr can be split into three non-empty contiguous subarrays

int main()  {  //start of main function

int arr[] = {1,2,2,2,5,0};  //declares and assigns values to array arr

int size = sizeof arr / sizeof arr[0]; //computes the size of array arr

cout << (CountSubArrays(arr, size)); }  //calls method by passing arr and size to it in order to find the number of ways it can be split into three non-empty contiguous subarrays

Explanation:

The program generates the previous array sum by adding the present element i-th element of arr to the previous element of previous array

It then generates the next array sum by adding the present element i-th element of arr to the next element of next array

Then two pointers i  and j are declared in order to find the sum of the second subarray.

Then while loop iterates over the array, increments current array with arr[j++] and this loop continues to execute as long as current stays less than previous[i – 1] .

When the current array is greater than or equal to previous[i – 1], then the if condition checks if current array is less than or equal to next[j]. This part basically works for the condition that the sum of the elements in the first subarray is less than or equal to the sum of the elements in the second subarray, and the sum of the elements in the second subarray is less than or equal to the sum of the elements in the third subarray. The value of count is incremented to 1 when the if statement evaluates to true.

Now the current array is decremented by arr[i+1]

The program repeats the above steps and then the function returns the computed value of count.

The output of the program is attached.

You might be interested in
How do say phone in French?
olga_2 [115]

Answer:

téléphone

Explanation:

thats how you say phone in French also you can go on Google translate or word reference to translate words.

6 0
4 years ago
Read 2 more answers
What is a wiki website?
expeople1 [14]

Answer:

A wiki website is a website we're people share ideas and information.

7 0
3 years ago
Use______ to format cells when they meet a certain criteria.
olchik [2.2K]
Data Calculations

Good luck
7 0
3 years ago
The background image does not affect any cell’s format or content. _______
Korvikt [17]
Hmmm...that is true 
Here a quizlet about if you need it!
https://quizlet.com/15207805/csci-241-flash-cards/
3 0
3 years ago
1. A _______ causes the computer program to behave in an incorrect or unexpected way.
Ede4ka [16]

Answer:

Bug

Explanation:

A <u>bug </u>causes the computer program to behave in an incorrect or unexpected way.

3 0
2 years ago
Read 2 more answers
Other questions:
  • GPS data can be used to track the rate and direction of plate movement. If a GPS unit measures a latitude velocity of 28.2 mm/yr
    8·1 answer
  • Design a class FileArray that has a static method named writeArray. The method should take two arguments: the name of a file and
    11·1 answer
  • This is my new horrible subject lol
    13·1 answer
  • Kerry wants to save her file but give it a new name. Kerry should use the ____ command
    7·1 answer
  • What was used to enhance silent films of the early 1900s
    15·1 answer
  • Can anyone help me with these assignments??? Hands On Assignments- Insertion of Symbols, Special Characters, and Images.... and
    9·1 answer
  • Explain what iteration is and why we need it in code
    11·1 answer
  • Choose the term that best matches the definition.
    9·1 answer
  • Which type of relationship is responsible for teaching you values and how to communicate?
    9·1 answer
  • What does playstation network is currently undergoing maintenance?.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!