Skip to content

Commit ff1150d

Browse files
author
Aman Kumar
committed
add arrays
1 parent b5b8e7e commit ff1150d

16 files changed

+712
-8
lines changed

README.md

+118
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,121 @@ example: `ruby longest_consecutive_subsequence.rb` will print:
2020
Did you find a bug? any way to do it better? please feel free to pull-request it :)
2121

2222

23+
24+
- __algorithms\_ruby__
25+
- [README.md](README.md)
26+
- __arrays__
27+
- [2\_number\_sum\_without\_hash.rb](arrays/2_number_sum_without_hash.rb)
28+
- [3\_number\_sum.rb](arrays/3_number_sum.rb)
29+
- [4\_number\_sum.rb](arrays/4_number_sum.rb)
30+
- [array\_of\_products\_except\_self.rb](arrays/array_of_products_except_self.rb)
31+
- [common\_elements\_3\_sorted\_arrays.rb](arrays/common_elements_3_sorted_arrays.rb)
32+
- [delete\_duplicates\_sorted\_array.rb](arrays/delete_duplicates_sorted_array.rb)
33+
- [find\_dublicates\_in\_array.rb](arrays/find_dublicates_in_array.rb)
34+
- [first\_duplicate\_n\_array.rb](arrays/first_duplicate_n_array.rb)
35+
- [first\_non\_repeating\_char.rb](arrays/first_non_repeating_char.rb)
36+
- [first\_repeating\_element.rb](arrays/first_repeating_element.rb)
37+
- [getMissing1to100Xor.rb](arrays/getMissing1to100Xor.rb)
38+
- [is\_monotonic.rb](arrays/is_monotonic.rb)
39+
- [is\_subsequence.rb](arrays/is_subsequence.rb)
40+
- [largest\_continous\_subarray.rb](arrays/largest_continous_subarray.rb)
41+
- [largest\_product\_subarray.rb](arrays/largest_product_subarray.rb)
42+
- [longest\_mountain.rb](arrays/longest_mountain.rb)
43+
- [merge\_overlapping\_intervals.rb](arrays/merge_overlapping_intervals.rb)
44+
- [min\_number\_off\_platforms.rb](arrays/min_number_off_platforms.rb)
45+
- [min\_sorted\_rotated\_array.rb](arrays/min_sorted_rotated_array.rb)
46+
- [missingNumber1to100.rb](arrays/missingNumber1to100.rb)
47+
- [move\_zeros\_to\_end.rb](arrays/move_zeros_to_end.rb)
48+
- [pair\_whose\_sum\_equals\_number.rb](arrays/pair_whose_sum_equals_number.rb)
49+
- [smallest\_difference.rb](arrays/smallest_difference.rb)
50+
- [smallest\_non\_constructible\_change.rb](arrays/smallest_non_constructible_change.rb)
51+
- [square\_of\_sorted\_arrays.rb](arrays/square_of_sorted_arrays.rb)
52+
- [tournament\_winner.rb](arrays/tournament_winner.rb)
53+
- [union\_sorted\_arrays.rb](arrays/union_sorted_arrays.rb)
54+
- __bit\_manipulation__
55+
- __bst__
56+
- [bst.rb](bst/bst.rb)
57+
- __dp__
58+
- [01\_knapsack.rb](dp/01_knapsack.rb)
59+
- [coin\_change.rb](dp/coin_change.rb)
60+
- [edit\_distance.rb](dp/edit_distance.rb)
61+
- [fibonacci.rb](dp/fibonacci.rb)
62+
- [job\_scheduling\_max\_profit.rb](dp/job_scheduling_max_profit.rb)
63+
- [max\_sum\_non\_adjacent.rb](dp/max_sum_non_adjacent.rb)
64+
- [ways\_to\_make\_coin\_change.rb](dp/ways_to_make_coin_change.rb)
65+
- __graph__
66+
- [dfs.rb](graph/dfs.rb)
67+
- [graph.rb](graph/graph.rb)
68+
- __greedy__
69+
- [fractional\_knapsack.rb](greedy/fractional_knapsack.rb)
70+
- [huffman\_coding.rb](greedy/huffman_coding.rb)
71+
- [job\_sequencing\_max\_profit.rb](greedy/job_sequencing_max_profit.rb)
72+
- [max\_meetings\_in\_a\_room.rb](greedy/max_meetings_in_a_room.rb)
73+
- [minimum\_number\_of\_coins.rb](greedy/minimum_number_of_coins.rb)
74+
- [water\_connection\_problem.rb](greedy/water_connection_problem.rb)
75+
- __heap__
76+
- [build\_heap\_from\_array.rb](heap/build_heap_from_array.rb)
77+
- [heap.rb](heap/heap.rb)
78+
- [heap\_sort.rb](heap/heap_sort.rb)
79+
- [kth\_largest\_element.rb](heap/kth_largest_element.rb)
80+
- [merge\_k\_sorted\_arrays.rb](heap/merge_k_sorted_arrays.rb)
81+
- [sliding\_window\_max\_from\_array.rb](heap/sliding_window_max_from_array.rb)
82+
- __linkedlist__
83+
- [detect\_cycle\_floyds\_mathemetical.rb](linkedlist/detect_cycle_floyds_mathemetical.rb)
84+
- [detect\_cycle\_hashing.rb](linkedlist/detect_cycle_hashing.rb)
85+
- [detect\_cycle\_one\_by\_one.rb](linkedlist/detect_cycle_one_by_one.rb)
86+
- [has\_cycle.rb](linkedlist/has_cycle.rb)
87+
- [intersection\_point\_2\_pointers.rb](linkedlist/intersection_point_2_pointers.rb)
88+
- [intersection\_point\_hashmap.rb](linkedlist/intersection_point_hashmap.rb)
89+
- [intersection\_point\_using\_node\_counts.rb](linkedlist/intersection_point_using_node_counts.rb)
90+
- [linklist.rb](linkedlist/linklist.rb)
91+
- [merge\_sort\_ll.rb](linkedlist/merge_sort_ll.rb)
92+
- [middle\_of\_linkedlist.rb](linkedlist/middle_of_linkedlist.rb)
93+
- [node.rb](linkedlist/node.rb)
94+
- [nth\_node\_from\_end.rb](linkedlist/nth_node_from_end.rb)
95+
- [nth\_node\_from\_end\_single\_pass.rb](linkedlist/nth_node_from_end_single_pass.rb)
96+
- [reverse\_in\_group\_iterative.rb](linkedlist/reverse_in_group_iterative.rb)
97+
- [reverse\_in\_groups.rb](linkedlist/reverse_in_groups.rb)
98+
- [reverse\_linklist.rb](linkedlist/reverse_linklist.rb)
99+
- [rotate\_right.rb](linkedlist/rotate_right.rb)
100+
- __matrix__
101+
- [diagonal\_traversal\_matrix.rb](matrix/diagonal_traversal_matrix.rb)
102+
- [spiral\_traversal\_matrix.rb](matrix/spiral_traversal_matrix.rb)
103+
- __others__
104+
- [rail\_fence.rb](others/rail_fence.rb)
105+
- __practice__
106+
- [merge\_sort.rb](practice/merge_sort.rb)
107+
- __searching__
108+
- [binary\_search.rb](searching/binary_search.rb)
109+
- __sorting__
110+
- [bubble\_sort.rb](sorting/bubble_sort.rb)
111+
- [merge\_sort.rb](sorting/merge_sort.rb)
112+
- [quick\_sort.rb](sorting/quick_sort.rb)
113+
- __strings__
114+
- [Manachers\_algo.rb](strings/Manachers_algo.rb)
115+
- [anagram\_string.rb](strings/anagram_string.rb)
116+
- [duplicates\_in\_str.rb](strings/duplicates_in_str.rb)
117+
- [interleaving\_string.rb](strings/interleaving_string.rb)
118+
- [is\_palindrome.rb](strings/is_palindrome.rb)
119+
- [longest\_consecutive\_subsequence.rb](strings/longest_consecutive_subsequence.rb)
120+
- [longest\_palindromic\_substring.rb](strings/longest_palindromic_substring.rb)
121+
- [string\_is\_rotation\_of\_other.rb](strings/string_is_rotation_of_other.rb)
122+
- [subsequences\_string.rb](strings/subsequences_string.rb)
123+
- __trees__
124+
- [convert\_to\_mirror\_of\_tree.rb](trees/convert_to_mirror_of_tree.rb)
125+
- [dfs.rb](trees/dfs.rb)
126+
- [diameter\_of\_tree.rb](trees/diameter_of_tree.rb)
127+
- [height.rb](trees/height.rb)
128+
- [height\_of\_tree.rb](trees/height_of_tree.rb)
129+
- [in\_order\_traversal.rb](trees/in_order_traversal.rb)
130+
- [inorder\_Morris\_algo.rb](trees/inorder_Morris_algo.rb)
131+
- [left\_view\_bt.rb](trees/left_view_bt.rb)
132+
- [lever\_order\_traversal\_tree.rb](trees/lever_order_traversal_tree.rb)
133+
- [mirror\_of\_tree.rb](trees/mirror_of_tree.rb)
134+
- [postorder\_traversal.rb](trees/postorder_traversal.rb)
135+
- [preorder\_traversal.rb](trees/preorder_traversal.rb)
136+
- [reverse\_level\_order.rb](trees/reverse_level_order.rb)
137+
- [right\_view\_bt.rb](trees/right_view_bt.rb)
138+
- [tree\_generator.rb](trees/tree_generator.rb)
139+
- [tree\_node.rb](trees/tree_node.rb)
140+

arrays/2_number_sum_without_hash.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# https://www.geeksforgeeks.org/given-an-array-a-and-a-number-x-check-for-pair-in-a-with-sum-as-x/
2+
3+
4+
def two_sum(nums, target)
5+
return nums if nums.length < 2
6+
nums.sort!
7+
left = 0
8+
right = num.length - 1
9+
while left < right
10+
11+
end
12+
end
13+
14+
15+
puts two_sum([0, -1, 2, -3, 1], -2)

arrays/3_number_sum.rb

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Write a function that takes in a non-empty array of distinct integers and an integer representing a target sum. The function should find all triplets in the array that sum up to the target sum and return a two-dimensional array of all these triplets. The numbers in each triplet should be ordered in ascending order, and the triplets themselves should be ordered in ascending order with respect to the numbers they hold.
2+
3+
# If no three numbers sum up to the target sum, the function should return an empty array.
4+
5+
# Sample Input
6+
# array = [12, 3, 1, 2, -6, 5, -8, 6]
7+
# targetSum = 0
8+
# Sample Output
9+
# [[-8, 2, 6], [-8, 3, 5], [-6, 1, 5]]
10+
require 'set'
11+
12+
def three_number_sum(nums, target)
13+
return if nums.length < 3
14+
15+
output = []
16+
nums.sort!
17+
Range.new(0, nums.length - 2).each do |i|
18+
left = i + 1
19+
right = nums.length - 1
20+
while left < right
21+
sum = nums[i] + nums[left] + nums[right]
22+
if sum == target
23+
output << [nums[i], nums[left], nums[right]]
24+
left += 1
25+
right -= 1
26+
elsif sum < target
27+
left += 1
28+
else
29+
right -= 1
30+
end
31+
end
32+
end
33+
output.to_a
34+
end
35+
36+
# Leet code solution.
37+
# Here the variation is, nums can have dublicate values and output should have uniq valeus only
38+
# target here is zero
39+
# https://leetcode.com/problems/3sum/
40+
def three_sum(nums)
41+
target = 0
42+
43+
return [] if nums.length < 3
44+
return [[0, 0, 0]] if nums.uniq == [0]
45+
46+
output = Set.new []
47+
nums.sort!
48+
(0..(nums.length - 3)).each do |i|
49+
left = i + 1
50+
right = nums.length - 1
51+
while left < right
52+
sum = nums[i] + nums[left] + nums[right]
53+
if sum == target
54+
output << [nums[i], nums[left], nums[right]]
55+
left += 1
56+
right -= 1
57+
elsif sum < target
58+
left += 1
59+
else
60+
right -= 1
61+
end
62+
end
63+
end
64+
output.to_a
65+
end
66+
67+
puts three_number_sum([12, 3, 1, 2, -6, 5, -8, 6], 0).to_s
68+
puts three_number_sum([1, 2, 3], 7).to_s
69+
puts three_number_sum([1, 2, 3, 4, 5, 6, 7, 8, 9, 15], 18).to_s

arrays/4_number_sum.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# https://leetcode.com/problems/4sum/
2+
# Write a function that takes in a non-empty array of distinct integers and an integer representing a target sum. The function should find all quadruplets in the array that sum up to the target sum and return a two-dimensional array of all these quadruplets in no particular order.
3+
# If no four numbers sum up to the target sum, the function should return an empty array.
4+
5+
# Sample Input, array = [7, 6, 4, -1, 1, 2] , targetSum = 16
6+
# Sample Output [[7, 6, 4, -1], [7, 6, 1, 2]] // the quadruplets could be ordered differently
7+
8+
9+
# This solution works fir there are no duplicates in the input
10+
def four_sum(nums, target)
11+
return [] if nums.length < 4
12+
13+
hash = {}
14+
output = []
15+
Range.new(1, nums.length - 2).each do |i|
16+
Range.new(i + 1, nums.length - 1).each do |j|
17+
sum = nums[i] + nums[j]
18+
diff = target - sum
19+
next unless hash.key? diff
20+
21+
hash[diff].each do |pair|
22+
output << [*pair, nums[i], nums[j]]
23+
end
24+
end
25+
26+
# We are adding pairs when we have visited 2nd number of pair to avoid duplicates
27+
(0..i).each do |k|
28+
pair = [nums[i], nums[k]]
29+
sum = pair.sum
30+
if hash.key? sum
31+
hash[sum].append pair
32+
else
33+
hash[sum] = [pair]
34+
end
35+
end
36+
end
37+
output
38+
end
39+
40+
puts four_sum([1, 0, -1, 0, -2, 2], 0).to_s
41+
42+
puts four_sum([2, 2, 2, 2, 2], 8).to_s
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# https://leetcode.com/problems/product-of-array-except-self/
2+
# Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
3+
4+
# The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
5+
6+
# You must write an algorithm that runs in O(n) time and without using the division operation.
7+
8+
# def product_except_self(nums)
9+
# product = nums.inject(1) { |product, num| product * num }
10+
# nums.map { |n| n.zero? ? product : product / n }
11+
# end
12+
13+
# def product_except_self(nums)
14+
# return nums if nums.empty?
15+
16+
# left = 1
17+
# right = 1
18+
# output = []
19+
# nums.each_with_index do |num, i|
20+
# right = 1
21+
# Range.new(i + 1, nums.length - 1).each do |j|
22+
# right *= nums[j]
23+
# end
24+
# output[i] = left * right
25+
# left *= num
26+
# end
27+
# output
28+
# end
29+
#
30+
def product_except_self_1(nums)
31+
return nums if nums.empty?
32+
33+
length = nums.length
34+
left_products = Array.new(length, 1)
35+
left_products[1] = nums[0]
36+
37+
right_products = Array.new(length, 1)
38+
right_products[length - 2] = nums[-1]
39+
(2..length - 1).each do |i|
40+
left_products[i] = nums[i - 1] * left_products[i - 1]
41+
end
42+
43+
(length - 2).downto(0).each do |i|
44+
right_products[i] = nums[i + 1] * right_products[i + 1]
45+
end
46+
47+
(0..length - 1).each do |i|
48+
nums[i] = left_products[i] * right_products[i]
49+
end
50+
nums
51+
end
52+
53+
def product_except_self(nums)
54+
return nums if nums.empty?
55+
56+
length = nums.length
57+
58+
output = Array.new(length, 1)
59+
left_running_prod = 1
60+
61+
# We filled left products first. Loop from L to R
62+
(0..length - 1).each do |i|
63+
output[i] = left_running_prod
64+
left_running_prod *= nums[i]
65+
end
66+
67+
# Loop from R to L, we will now multiple right products
68+
right_running_prod = 1
69+
(length - 1).downto(0).each do |i|
70+
output[i] *= right_running_prod
71+
right_running_prod *= nums[i]
72+
end
73+
output
74+
end
75+
76+
puts product_except_self([1, 2, 3, 4]).to_s
77+
78+
puts product_except_self([-1, 1, 0, -3, 3]).to_s
79+
80+
puts product_except_self([-1, 1, 0, -3, 3]).to_s

arrays/find_dublicates_in_array.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Find duplicates in a given array when elements are not limited to a range
22

33
def findDuplicates(ar)
4-
dublicates = []
4+
duplicates = []
55
ar_hash = {}
66
size = ar.length
77
ar.each_with_index do |val, index|
@@ -10,22 +10,22 @@ def findDuplicates(ar)
1010
end
1111
end
1212
ar_hash.each do |k, v|
13-
dublicates.push k if v > 1
13+
duplicates.push k if v > 1
1414
end
15-
dublicates.to_s
15+
duplicates.to_s
1616
end
1717

1818
def findDuplicatesWithHash(ar)
19-
dublicates = []
19+
duplicates = []
2020
ar_hash = {}
2121
size = ar.length
2222
ar.each_with_index do |val, _index|
2323
ar_hash[val] = ar_hash[val].to_i + 1
2424
end
2525
ar_hash.each do |k, v|
26-
dublicates.push k if v > 1
26+
duplicates.push k if v > 1
2727
end
28-
dublicates.to_s
28+
duplicates.to_s
2929
end
3030

3131
puts findDuplicatesWithHash([2, 10, 10, 100, 2, 10, 11, 2, 11, 2])

0 commit comments

Comments
 (0)