-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArraySum.sol
More file actions
32 lines (27 loc) · 881 Bytes
/
ArraySum.sol
File metadata and controls
32 lines (27 loc) · 881 Bytes
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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;
contract ArraySum {
// you are allowed to change these state vars, but not to remove them
uint256 public total;
uint256 gasUsed;
// optimize the for loop in this function
function sumIfEvenAndLessThan99(uint256[] memory nums) external {
uint256 startGas = gasleft();
for (uint256 i = 0; i < nums.length; i += 1) {
bool isEven = nums[i] % 2 == 0;
bool isLessThan99 = nums[i] < 99;
if (isEven && isLessThan99) {
total += nums[i];
}
}
gasUsed = startGas - gasleft();
}
// dont modify this function
function gasOptimized(uint256 optimizedGas) public view returns (bool) {
if (gasUsed < optimizedGas) {
return true;
} else {
return false;
}
}
}