Sunday, 28 Nov 2021
Category: subarray-sum

Given two arrays A[] and B[] both consisting of N integers, the task is to find the maximum length of subarray [i, j] such that the sum of A[i… j] is equal to B[i… j].Examples:Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at […]

Find minimum subarray sum for each index i in subarray [i, N-1]Given an array arr[] of size N, the task is to find the minimum subarray sum in the subarrays [i, N-1] for all i in [0, N-1].Examples:Input: arr[ ] = {3, -1, -2}Output: 3 -3 -2Explanation: For (i = 1) i.e. {3, -1, -2}, the […]

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

#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 == […]

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 […]