Easy

Given integers n, l and r, find the number of ways to represent n as a sum of two integers A and B such that l ≤ A ≤ B ≤ r.

Example

For n = 6, l = 2, and r = 4, the output should be
countSumOfTwoRepresentations2(n, l, r) = 2.

There are just two ways to write 6 as A + B, where 2 ≤ A ≤ B ≤ 4: 6 = 2 + 4 and 6 = 3 + 3.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    A positive integer.

    Guaranteed constraints:
    5 ≤ n ≤ 10^9.

  • [input] integer l

    A positive integer.

    Guaranteed constraints:
    1 ≤ l ≤ r.

  • [input] integer r

    A positive integer.

    Guaranteed constraints:
    l ≤ r ≤ 10^9,
    r - l ≤ 10^6.

  • [output] integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

int countSumOfTwoRepresentations2(int n,int l,int r)
{
	int count=0;

	for(int A=l;A<=r;A++)
		for(int B=A;B<=r;B++)
			if(A+B==n)
			{
				count++;
				break;
			}
			else if(A+B>n)
				break;

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Lineup  (0) 2020.04.12
<Codesignal> Magical Well  (0) 2020.04.12
<Codesignal> Least Factorial  (0) 2020.04.12
<Codesignal> Second-Rightmost Zero Bit  (0) 2020.04.12
<Codesignal> Mirror Bits  (0) 2020.04.09

+ Recent posts