# Different possible marks for n questions and negative marking

Given the number of questions as , and marks for the correct answer as and marks for the incorrect answer. One can either attempt to solve the question in an examination and get either marks if the answer is right, or marks if the answer is wrong, or leave the question unattended and get marks. The task is to find the count of all the different possible marks that one can score in the examination.

Examples:

```Input: n = 2, p = 1, q = -1
Output: 5
The different possible marks are: -2, -1, 0, 1, 2

Input: n = 4, p = 2, q = -1
Output: 12
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
Iterate through all the possible number of correctly solved and unsolved problems. Store the scores in a set containing distinct elements keeping in mind that there is a positive number of incorrectly solved problems.

Below is the implementation of the above approach:

## C++

 `// CPP program to find the count of ` `// all the different possible marks ` `// that one can score in the examination ` `#include ` ` `  `using` `namespace` `std; ` ` `  `    ``// Function to return ` `    ``// the count of distinct scores ` `    ``int` `scores(``int` `n, ``int` `p, ``int` `q) ` `    ``{ ` `        ``// Set to store distinct values ` `        ``set<``int``> hset; ` ` `  `        ``// iterate through all ` `        ``// possible pairs of (p, q) ` `        ``for` `(``int` `i = 0; i <= n; i++)  ` `        ``{ ` `            ``for` `(``int` `j = 0; j <= n; j++)  ` `            ``{ ` ` `  `                ``int` `correct = i; ` `                ``int` `not_solved = j; ` `                ``int` `incorrect = n - i - j; ` ` `  `                ``// if there are positive number ` `                ``// of incorrectly solved problems ` `                ``if` `(incorrect >= 0) ` `                    ``hset.insert(p * correct ` `                            ``+ q * incorrect); ` `                ``else` `                    ``break``; ` `            ``} ` `        ``} ` ` `  `        ``// return the size of the set ` `        ``// containing distinct elements ` `        ``return` `hset.size(); ` `    ``} ` ` `  `    ``// Driver code ` `    ``int` `main() ` `    ``{ ` ` `  `        ``// Get the number of questions ` `        ``int` `n = 4; ` ` `  `        ``// Get the marks for correct answer ` `        ``int` `p = 2; ` ` `  `        ``// Get the marks for incorrect answer ` `        ``int` `q = -1; ` ` `  `        ``// Get the count and print it ` `        ``cout << (scores(n, p, q)); ` `    ``} ` ` `  `// This code is contributed by ` `// Surendra_Gangwar `

## Java

 `// Java program to find the count of ` `// all the different possible marks ` `// that one can score in the examination ` ` `  `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to return ` `    ``// the count of distinct scores ` `    ``static` `int` `scores(``int` `n, ``int` `p, ``int` `q) ` `    ``{ ` `        ``// Set to store distinct values ` `        ``HashSet ` `            ``hset = ``new` `HashSet(); ` ` `  `        ``// iterate through all ` `        ``// possible pairs of (p, q) ` `        ``for` `(``int` `i = ``0``; i <= n; i++) { ` `            ``for` `(``int` `j = ``0``; j <= n; j++) { ` ` `  `                ``int` `correct = i; ` `                ``int` `not_solved = j; ` `                ``int` `incorrect = n - i - j; ` ` `  `                ``// if there are positive number ` `                ``// of incorrectly solved problems ` `                ``if` `(incorrect >= ``0``) ` `                    ``hset.add(p * correct ` `                             ``+ q * incorrect); ` `                ``else` `                    ``break``; ` `            ``} ` `        ``} ` ` `  `        ``// return the size of the set ` `        ``// containing distinct elements ` `        ``return` `hset.size(); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// Get the number of questions ` `        ``int` `n = ``4``; ` ` `  `        ``// Get the marks for correct answer ` `        ``int` `p = ``2``; ` ` `  `        ``// Get the marks for incorrect answer ` `        ``int` `q = -``1``; ` ` `  `        ``// Get the count and print it ` `        ``System.out.println(scores(n, p, q)); ` `    ``} ` `} `

## Python3

 `# Python3 program to find the count of  ` `# all the different possible marks  ` `# that one can score in the examination  ` ` `  `# Function to return the count of ` `# distinct scores  ` `def` `scores(n, p, q):  ` `     `  `    ``# Set to store distinct values  ` `    ``hset ``=` `set``()  ` ` `  `    ``# Iterate through all possible ` `    ``# pairs of (p, q)  ` `    ``for` `i ``in` `range``(``0``, n ``+` `1``):  ` `        ``for` `j ``in` `range``(``0``, n ``+` `1``):  ` ` `  `            ``correct ``=` `i  ` `            ``not_solved ``=` `j  ` `            ``incorrect ``=` `n ``-` `i ``-` `j  ` ` `  `            ``# If there are positive number  ` `            ``# of incorrectly solved problems  ` `            ``if` `incorrect >``=` `0``: ` `                ``hset.add(p ``*` `correct ``+` `                         ``q ``*` `incorrect)  ` `            ``else``: ` `                ``break` ` `  `    ``# return the size of the set  ` `    ``# containing distinct elements  ` `    ``return` `len``(hset)  ` `     `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``# Get the number of questions  ` `    ``n ``=` `4` ` `  `    ``# Get the marks for correct answer  ` `    ``p ``=` `2` ` `  `    ``# Get the marks for incorrect answer  ` `    ``q ``=` `-``1` ` `  `    ``# Get the count and print it  ` `    ``print``(scores(n, p, q))  ` `     `  `# This code is contributed by Rituraj Jain `

## C#

 `// C# program to find the count of ` `// all the different possible marks ` `// that one can score in the examination ` `using` `System; ` `using` `System.Collections.Generic;  ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to return ` `    ``// the count of distinct scores ` `    ``static` `int` `scores(``int` `n, ``int` `p, ``int` `q) ` `    ``{ ` `        ``// Set to store distinct values ` `        ``HashSet<``int``> ` `            ``hset = ``new` `HashSet<``int``>(); ` ` `  `        ``// iterate through all ` `        ``// possible pairs of (p, q) ` `        ``for` `(``int` `i = 0; i <= n; i++)  ` `        ``{ ` `            ``for` `(``int` `j = 0; j <= n; j++)  ` `            ``{ ` ` `  `                ``int` `correct = i; ` `                ``int` `not_solved = j; ` `                ``int` `incorrect = n - i - j; ` ` `  `                ``// if there are positive number ` `                ``// of incorrectly solved problems ` `                ``if` `(incorrect >= 0) ` `                    ``hset.Add(p * correct ` `                            ``+ q * incorrect); ` `                ``else` `                    ``break``; ` `            ``} ` `        ``} ` ` `  `        ``// return the size of the set ` `        ``// containing distinct elements ` `        ``return` `hset.Count; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` ` `  `        ``// Get the number of questions ` `        ``int` `n = 4; ` ` `  `        ``// Get the marks for correct answer ` `        ``int` `p = 2; ` ` `  `        ``// Get the marks for incorrect answer ` `        ``int` `q = -1; ` ` `  `        ``// Get the count and print it ` `        ``Console.WriteLine(scores(n, p, q)); ` `    ``} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

Output:

```12
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.