Answer to Question #231618 in C for aman

Question #231618

Harry loves numbers in the range [m1, m2] (m1 and m2 included). carry decides to gift harry an array of numbers for his birthday. harry wants to find the number of pairs of indices [l, r] for which the sum of all the elements in the range [l, r] lies between m1 and m2.  

Now improvise this algorithm, assuming all numbers are positive integers. 



1
Expert's answer
2021-08-31T23:51:11-0400


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 6000
int main()
{
	printf("Enter the numbers that Harry loves:\n");
	int x1, x2;
	printf("m1:");
	scanf("%d", &x1);




	printf("m2:");
	scanf("%d", &x2);
	
	int y;
	int array[MAXSIZE];
	printf("Enter the numbers count: ");
	scanf("%d", &y);
	printf("Please enter the numbers:\n");
	for (int i = 0; i < y; i++)
		scanf("%d", &array[i]);
	int count = 0;
	long long sum[MAXSIZE];
	for (int i = 0; i < MAXSIZE; i++)
		sum[i] = 0;
	sum[0] = array[0];
   //sorting numbers
	for (int i = 1; i < y; i++)
		sum[i] = sum[i - 1] + array[i];
	for (int l = 0; l < y; l++)
	{
		for (int r = l; r < y; r++)
		{
			long long sums = sum[r] - sum[l] + array[l];
			if (sums >=(long long) x1 && sums <=(long long) x2)
				count++;
		}
	}
	printf("Number of pair indices for which the sum of elements lie is: %d\n", count);
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS