 # DISTRIBUTING MEDALS

Its the medal distribution ceremony.10^6 police officers,numbered from one to 10^6,are standing in a line.There are N(1<=N<=1000)iterations of medal distribution.
In iteration i(0 < = i < N),count [i] (1<=count[i]<=100) medals are given to all officers from [i] to [i] (1<=from[i]<=to[i]<=to[i]<=10^6).

if we sum up the number of medals recived starting from the first officer,who would be the first officer for which the cumulative sum exceeds a given medal count THERESOLD(1<=THERESOLD<=10^9)?

INPUT/OUPUT SPECIFICATIONS

INPUT FORMAT:-

yor are given 5 inputs:
input1=N,the number of iterations
input2=count,the array of medals counts in each iteration
input3=from,the array of starting indices in each iteration
input4=to,the aray of ending indices in each iteration
input5=THERESHOLD,the medal count threshold

OUTPUT FORMAT:-
AN integer,representing the number of the first officer such that the cumulative sum of medals from the first officer upto this officer exceeds THERESHOLD. The output should be -1 if such an officer does not exist .

instructions:
Do not change the function and parameter names given in editor code.
Do not include any new libraries in the code.
You need to return the required output in the given function.
Return type must be the same as mentioned in the problem statement.
Do not print any variables in the code.
Do not write main function.

Examples:

Example:

input
1,{1},{1},{10},2

output:
3

Explanation:
1 medal is given to each of the officers 1 to 10.Officer is the first officer for which the cumulative sum starting from first officer exceeds 2.

public static int DistributingMedals(int input1, int[] input2, int[] input3, int[] input4, int input5)
{
int officerIndex = -1;
if (InputsValid(input1, input2, input3, input4, input5))
{
int medalsCount = 0;
for (int i = 0; i < input1; i++)
{
for (int o = input3[i]; o <= input4[i]; o++)
{
medalsCount += input2[i];
if (medalsCount > input5)
{
officerIndex = o;
break;
}
}
if (medalsCount > input5)
break;
}
}
return officerIndex;
}

``````    private static bool InputsValid(int input1, int[] input2, int[] input3, int[] input4, int input5)
{
if (((1 <= input1) && (input1 <= 1000))
&& ((input2.Length == input1) && (input3.Length == input1) && (input4.Length == input1))
&& ((1 <= input5) && (input5 <= 1000000000)))
{
int ok = 0;
for (int i = 0; i < input1; i++)
{
if ((1 <= input3[i] && input3[i] <= input4[i] && input4[i] <= 1000000)
&& (1 <= input2[i] && input2[i] <= 100))
ok++;
}
if (ok == input1)
return true;
}
return false;
}
``````

Hope it helps!

using System;
using System.Collections.Generic;

``````class DistributingMedals
{
static long FindExceed (int n, long[] count, long[] start, long[] end, long thrh)
{
int i = 0;
long exceedAt = -1, sum = 0, keyVal;
long spt = start[i], ept = end[i];
SortedDictionary<long, long> ofcr = new SortedDictionary<long, long>();

while ((spt < ept + 1) && (i < n))
{
if (ofcr.ContainsKey(spt))
{
keyVal = ofcr[spt] + count[i];
ofcr[spt] = keyVal;
}
else
{
}

spt++;

if (spt == ept + 1)
{
i++;
if (i < n)
{
spt = start[i];
ept = end[i];
}
}
}

foreach (KeyValuePair<long, long> medal in ofcr)
{
sum += medal.Value;

if (sum > thrh)
{
exceedAt = medal.Key;
break;
}
}
return exceedAt;
}
static void Main(string[] args)
{
int i;
long exceedAt;

try
{

if (1 <= n && n <= 1000)
{
long[] count = new long[input2.Length];
for (i = 0; i < input2.Length; i++)
{
count[i] = Convert.ToInt64(input2[i]);
}
long[] start = new long[input3.Length];
for (i = 0; i < input3.Length; i++)
{
start[i] = Convert.ToInt64(input3[i]);
}
long[] end = new long[input4.Length];
for (i = 0; i < input4.Length; i++)
{
end[i] = Convert.ToInt64(input4[i]);
}