Solve Me And Get Marks

The problem setter loves movies but you want marks, after all, you have to pass. Listening to your second year, and not taking any advice from third and fourth-year seniors, you cheated in your exam. Now you are worried if you will be caught and run to your problem setter, who is a third-year senior. He tells you not to worry. You should only worry if your paper will go through a plagiarism test.  

The good thing about IIITA this year is that there is a strange law about plagiarism checking. If there are students in the course, and the score of the ith student is X, then his paper is subjected to plagiarism if and only if there is at least 1 more student whose score lies in the interval [ X-K, X+K ], both included.  

The Profs like to call this number K as NoMercy Value. 

Now, you have all the details regarding the above checking. You know N, number of students taking that course, K =  the Mercy value set by the Prof and the marks scored by all N students. 

You want to find how many students should actually take the pressure of getting caught in the plagiarism test and how many students should simply sit back and relax. You also want to print these values with a message as shown in the sample output. 

Example:

Input: noMercy = 6;  marksArray = { 100, 9, 10, 15, 1 };
Output: 3 students need to worry!
        2 students should relax!

Approach

Java

import java.io.IOException;
import java.util.Arrays;

public class SolveMeAndGetMarks {
    public static void main(String[] argsthrows IOException {
        int noMercy = 6;
        int[] marksArray = { 100910151 };
        Arrays.sort(marksArray);
        int noWorry = 0;
        for (int i = 0; i < marksArray.length; i++) {
            if (i == 0) {
                if (marksArray[0] + noMercy < marksArray[1])
                    noWorry++;
            } else if (i == marksArray.length - 1) {
                if (marksArray[marksArray.length - 1] - noMercy >
 marksArray[marksArray.length - 2])
                    noWorry++;
            } else if (marksArray[i] - noMercy >
 marksArray[i - 1] && 
marksArray[i] + noMercy < marksArray[i + 1])
                noWorry++;
        }
        System.out.println(marksArray.length - noWorry 
" students need to worry!");
        System.out.println(noWorry +
 " students should relax!");
    }
}

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int noMercy = 6;
    vector<intmarksArray = {100910151};
    sort(marksArray.begin(), marksArray.end());
    int noWorry = 0;
    for (int i = 0i < marksArray.size(); i++)
    {
        if (i == 0)
        {
            if (marksArray[0] + noMercy < marksArray[1])
                noWorry++;
        }
        else if (i == marksArray.size() - 1)
        {
            if (marksArray[marksArray.size() - 1] - noMercy > 
marksArray[marksArray.size() - 2])
                noWorry++;
        }
        else if (marksArray[i] - noMercy > marksArray[i - 1] && 
marksArray[i] + noMercy < marksArray[i + 1])
            noWorry++;
    }
    cout << marksArray.size() - noWorry 
<< " students need to worry!\n";
    cout << noWorry << " students should relax!\n";

    return 0;
}


No comments:

Post a Comment