Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c2a229c

Browse files
Added question 17
1 parent 06d07cb commit c2a229c

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

‎README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,17 @@ Output:
154154
Explanation: This the only two sequences of balanced
155155
parenthesis formed using 2 pair of balanced parenthesis.
156156
```
157+
17) Given an array arr of size N and an integer K. The task is to find the pair of integers such that their sum is maximum and but less than K
158+
```sh
159+
Examples:
160+
161+
Input : arr = {30, 20, 50} , K = 70
162+
Output : 30, 20
163+
30 + 20 = 50, which is the maximum possible sum which is less than K
164+
165+
166+
Input : arr = {5, 20, 110, 100, 10}, K = 85
167+
Output : 20, 10
168+
```
157169

158170
Feel free to make an addition or improvement in the questions and if you like the effort, do give a star to the repo :-)

‎two_sum_less_than_k.rb‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Given an array arr of size N and an integer K. The task is to find the pair of integers such that their sum is maximum and but less than K
2+
3+
# Examples:
4+
5+
# Input : arr = {30, 20, 50} , K = 70
6+
# Output : 30, 20
7+
# 30 + 20 = 50, which is the maximum possible sum which is less than K
8+
9+
10+
# Input : arr = {5, 20, 110, 100, 10}, K = 85
11+
# Output : 20, 10
12+
13+
14+
def two_sum(arr, k)
15+
arr.sort!
16+
l, r = 0, arr.length-1
17+
max = 0
18+
while l < r
19+
puts "l = " + l.to_s + " , r = " + r.to_s
20+
temp = arr[l] + arr[r]
21+
if k > temp
22+
l += 1
23+
else
24+
r -= 1
25+
end
26+
end
27+
puts arr[l]
28+
puts arr[r]
29+
end
30+
31+
arr = [20, 10, 30, 100, 110]
32+
k = 85
33+
34+
two_sum arr, k

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /