Programs


Title : WAP to find the maximum circular sub array sum of given array

#include <stdio.h>

int kadane(int arr1[], int n);

int SumOfMxCircur(int arr1[], int n)

{

   int maxKadaneSum = kadane(arr1, n);

   int maxWrap = 0, i;

   for (i = 0; i < n; i++)

   {

       maxWrap += arr1[i];

       arr1[i] = -arr1[i];

   }

   maxWrap = maxWrap + kadane(arr1, n);

   return (maxWrap > maxKadaneSum) ? maxWrap : maxKadaneSum;

}

int kadane(int arr1[], int n)

{

   int maxUpto = 0, maxAtPos = 0;

   int i;

   for (i = 0; i < n; i++)

   {

       maxAtPos = maxAtPos + arr1[i];

       if (maxAtPos < 0)

           maxAtPos = 0;

       if (maxUpto < maxAtPos)

           maxUpto = maxAtPos;

   }

   return maxUpto;

}

int main()

{

   int arr1[] = {11, 9, -20, 6, -4, -5, 10, -13, 10};

   int n = sizeof(arr1) / sizeof(arr1[0]);

   int i;

   printf("The given array is :  ");

   for (i = 0; i < n; i++)

   {

       printf("%d  ", arr1[i]);

   }

   printf("\n");

     printf("The maximum circular sum in the above array is:  %d\n", SumOfMxCircur(arr1, n));

   return 0;

}

OUTPUT:

The given array is :  11  9  -20  6  -4  -5  10  -13  10 

The maximum circular sum in the above array is:  30

Related Topics

  • Integer x appears more than n by 2 times in sorted array

      Jan 15, 2022

  • Program to find majority element of an array

      Jan 03, 2022

  • Program to print a matrix in spiral form

      Jan 03, 2022

  • Program to count the number of triangles from a given array

      Jan 03, 2022

  • WAP to find the number of times occurs a given number in an array

      Jan 03, 2022

  • Program to sort an array of 0s, 1s and 2s.

      Jun 30, 2021

  • Subscribe Now