-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHR_FredoandArrayUpdate.c
73 lines (62 loc) · 1.48 KB
/
HR_FredoandArrayUpdate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
int main(){
int num;
scanf("%d", &num); // Reading input from STDIN
// printf("Input number is %d.\n", num); // Writing output to STDOUT
if( (num>=1) && (num<=100000) )
{
int* pArr = (int*) calloc(num, sizeof(int));
int tmp = -1;
int64_t inputSum = 0;
for(int i=0; i<num; i++)
{
scanf("%d", &tmp);
if( (tmp>=1) && (tmp<=1000) )
{
pArr[i] = tmp;
inputSum = inputSum + tmp;
// printf("%d ",tmp);
}
}
// printf("\n\r");
// all inputs are taken
//take each number from the pArr & multiply it with num to get total sum
//then compare this sum with inputSum
//now if this indexedSum is greater than the inputSum then keep the element as it is
//if this indexed sum is less than the inputSum then change the pArr element to 9999
int64_t indexedSumNow = 0;
for(int i=0; i<num; i++)
{
indexedSumNow = pArr[i] * num;
if(indexedSumNow > inputSum)
{
; //no change on element
}
else
{
pArr[i] = 9999;
}
}
//now after loop is complete ,
// take one element of the pArr and compare with all others remaining to see if it is the smallest
int finalNum = pArr[0];
for(int i=0; i<num; i++)
{
int elemNow = pArr[i];
for(int j=(i+1); j<num; j++)
{
if(elemNow < pArr[j])
{
if(elemNow < finalNum)
{
finalNum = elemNow;
}
}
}
}
free(pArr);
printf("%d", finalNum);
}
}