Category: subarray-sum

Minimum cost to convert all elements of a K-size subarray to 0 from given Ternary Array with subarray sum as cost

import java.io.*;import java.util.Arrays;class GFG{           static int minCost(int arr[], int N, int K)    {                       int pcount1[] = new int[N + 1];                       int pcount2[] = new int[N + 1];        Arrays.fill(pcount1, 0);        Arrays.fill(pcount2, 0);                for (int i = 1; i

Continue Reading

Minimum removal of elements from end of an array required to obtain sum K

  #include using namespace std;  int minSizeArr(int A[], int N, int K){            int leftTaken = N, rightTaken = N;          int leftSum = 0, rightSum = 0;          for (int left = -1; left < N; left++) {          if (left != -1)            leftSum += A[left];          rightSum = 0;                  for (int right = N - 1; right > left; right–) {              rightSum += A[right];              if (leftSum + rightSum == […]

Continue Reading

Maximum subarray sum possible after removing at most one subarray

import java.io.*;import java.util.*;  class GFG {                  public static void maximumSum(        int arr[], int n)    {        long[] preSum = new long[n];          long sum = 0;        long maxSum = 0;                  for (int i = 0; i < n; i++) {                          sum = Math.max(arr[i],                           sum + arr[i]);                          maxSum = Math.max(maxSum,                              sum);                          preSum[i] = maxSum;        }          sum = 0;        maxSum = 0;          long[] postSum = new long[n + 1];                  for (int i = n - 1; i […]

Continue Reading