Saturday, December 3, 2022
HomeSoftware DevelopmentMaximize non lowering Array dimension by changing Subarray with sum

# Maximize non lowering Array dimension by changing Subarray with sum

Given an array A[] of dimension N. In a single operation just one subarray may be chosen and changed with the sum of the subarray. The duty is to search out the utmost dimension of the array after making it non-decreasing.

Examples:

Enter: N = 5, A[] = {5, 1, 6, 6, 6}
Output: 4
Clarification: most dimension non-decreasing array, on this case, is {6, 6, 6, 6} which is obtained by changing subarray(0, 1) = A[0] + A[1] = 6

Enter: N = 9, A[] = {5, 1, 6, 7, 7, 1, 6, 4, 5 }
Output: 6
Clarification: most dimension non-decreasing array, on this case, is {5, 7, 7, 7, 7, 9} which is obtained by changing subarray(1, 2) = A[1] + A[2] = 7. Subarray(5, 6) = A[5] + A[6] = 7. Subarray(7, 8) = A[7] + A[8] = 9

Method: This drawback may be solved utilizing grasping strategy primarily based on the beneath statement:

Iterate linearly and contemplate the primary ingredient to be made up of subarray from 0 to i. Now to search out the remaining components, discover the minimal dimension subarray whose sum is at the least similar because the earlier ingredient.

Observe the beneath steps to implement the thought:

• Let the primary ingredient of the ultimate non-decreasing subarray be begin.
• Iterate from i = 0 to N-1,
• Calculate begin because the sum of the prefix of the array until i.
• For every worth of begin, iterate from j = i+1, and initialize temp=1
• temp retailer the worth of the scale of optimum non-decreasing array dimension for the present worth of begin.
• Take into account subarray ranging from j till the sum of the subarray is bigger than equal to begin.
• If a subarray is discovered then, enhance the temp by 1 and replace the begin to the brand new subarray sum.
• Proceed this iteration until j turns into N.
• The utmost worth of temp amongst all iterations is the reply.

Under is the Implementation of the above strategy:

## C++

 ` `  `#embrace ` `utilizing` `namespace` `std;` ` `  `int` `findmaxsize(``int` `N, ``int` `A[])` `{` `    ``int` `ans = 0, sum = 0;` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``int` `temp = 1;` `        ``sum += A[i];` `        ``int` `j = i + 1;` ` `  `        ` `        ``int` `begin = sum;` `        ``whereas` `(j < N) {` `            ``int` `depend = 0;` `            ``int` `ok = j;` `            ``whereas` `(ok < N && depend < begin) {` `                ``depend += A[k];` `                ``ok++;` `            ``}` ` `  `            ` `            ` `            ``if` `(depend >= begin) {` `                ``begin = depend;` `                ``temp++;` `            ``}` `            ``j = ok;` `        ``}` `        ``ans = max(ans, temp);` `    ``}` ` `  `    ` `    ``return` `ans;` `}` ` `  `int` `principal()` `{` `    ``int` `A[] = { 5, 1, 6, 6, 6 };` `    ``int` `N = ``sizeof``(A) / ``sizeof``(A[0]);` ` `  `    ` `    ``cout << findmaxsize(N, A) << endl;` ` `  `    ``return` `0;` `}`

Time Complexity: O(N * N)
Auxiliary area: O(1)

Associated Articles:

RELATED ARTICLES