BackeasyArraysTCSInfosys

Sum Elements Greater Than K Solution

Problem Statement

Given an array of integers nums and a threshold value K, calculate and return the total sum of all elements that are strictly greater than K.

Example 1
Input
[1, 2, 3, 4, 5], K = 3
Output
9

Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and K = 3, we filter out elements less than or equal to 3, giving us [4, 5], then sum these elements, resulting in 4 + 5 = 9

Example 2
Input
[10, 20, 30], K = 15
Output
50

Explanation: Step-by-step: with input [10, 20, 30] and K = 15, we filter out elements less than or equal to 15, giving us [20, 30], then sum these elements, resulting in 20 + 30 = 50

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • -10^4 <= K <= 10^4
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Sum Elements Greater Than K — Problem Statement & Solution Guide

ArraysEasyLinear Scan
TimeO(n)
|
SpaceO(n)

Problem Description

Given an array of integers nums and a threshold value K, calculate and return the total sum of all elements that are strictly greater than K.

Examples

Example 1

Input

[1, 2, 3, 4, 5], K = 3

Output

9

Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and K = 3, we filter out elements less than or equal to 3, giving us [4, 5], then sum these elements, resulting in 4 + 5 = 9

Example 2

Input

[10, 20, 30], K = 15

Output

50

Explanation: Step-by-step: with input [10, 20, 30] and K = 15, we filter out elements less than or equal to 15, giving us [20, 30], then sum these elements, resulting in 20 + 30 = 50

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • -10^4 <= K <= 10^4

Optimal Approach & Strategy

Single-pass linear scan O(N) time and O(1) auxiliary space.

Brute Force Approach

Iterate through all array elements and accumulate values > K in linear time O(N).

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums, K) { return nums.filter(num => num > K).reduce((a, b) => a + b, 0); }

Asked in Top Tech Interviews

TCSInfosysWipro

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.