Skip to content

Commit 13b784f

Browse files
committed
Various array algorithms
1 parent 71b7606 commit 13b784f

8 files changed

+208
-16
lines changed

Diff for: ChekPairWithGivenSum.rb

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
#Given an array A[] and a number x, check for pair in A[] with sum as x(Using Hash Map)
2-
#Time-complexity: O(n), Space-complexity: O(n)
1+
#Given an array A[] and a number x, check for pair in A[] with sum as x
2+
3+
#Approach 1: Using Hash Map
4+
#Time-complexity: O(n), Space-complexity: O(n) {extra space required for hashmap}
35

46
def check_pair(arr,x)
57
len=arr.length
@@ -14,10 +16,33 @@ def check_pair(arr,x)
1416
map[arr[i]]=1
1517
end
1618
if flag
17-
print "Array has two elements with sum:#{x}"
19+
return "Array has two elements with sum:#{x}"
1820
else
19-
print "Array doesn't have two elements with sum:#{x}"
21+
return "Array doesn't have two elements with sum:#{x}"
2022
end
2123
end
2224

2325
check_pair([5,4,10,-2,3,2,-1,9],9)
26+
27+
28+
##Approach 2: Sorting and then searching using left and right indexes.
29+
#Time-complexity: O(nlogn) //O(nlogn)+O(n), Space-complexity: O(1)
30+
31+
def check_pair(arr,x)
32+
len=arr.length
33+
arr.sort! # You can choose your own sorting algorithm
34+
left=0
35+
right=len-1
36+
while left<right
37+
if (arr[left]+arr[right]==x)
38+
return "Array has two elements with sum:#{x}"
39+
elsif (arr[left]+arr[right]>x)
40+
right-=1
41+
else
42+
left+=1
43+
end
44+
end
45+
return "Array doesn't have two elements with sum:#{x}"
46+
end
47+
48+
check_pair([5,4,10,-2,3,2,-1,9],9)

Diff for: ClosestSum.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#Given array of integers(both +ve and -ve) find the two elements such that their sum is closest to given number x.
2+
#Time-complexity: O(nlogn) // O(nlogn){for sorting}+O(n), Auxiliary-space:O(1)
3+
4+
#Algorithm: Sort the array and using two indexes left and right update the closest sum
5+
6+
def closest_sum(a,x)
7+
a.sort! #You can choose any sorting algorithm of your choice with
8+
left=min_l=0
9+
right=min_r=a.length-1
10+
min_sum = 1.0/0.0 #Initializing min_sum with infinity
11+
while(left<right)
12+
sum=(a[left]+a[right])-x
13+
if sum.abs<min_sum
14+
min_sum=sum.abs
15+
min_l=left
16+
min_r=right
17+
end
18+
if sum<0
19+
left+=1
20+
else
21+
right-=1
22+
end
23+
end
24+
print " The two elements whose sum is minimum are #{a[min_l]} and #{a[min_r]}"
25+
end
26+
27+
closest_sum([1,3,2,4,5],9) # => The two elements whose sum is minimum are 4 and 5

Diff for: LeadersInArray.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=begin
2+
Algorithm to print all the LEADERS in the array.
3+
An element is leader if it is greater than all the elements to its right side,the rightmost element is always a leader.
4+
Time-complexity: O(n)
5+
Auxiliary-space: O(1)
6+
=end
7+
8+
def find_leaders(a)
9+
n = a.length
10+
max_from_right = a[n-1]
11+
print "#{max_from_right}"+" "
12+
13+
for i in (n-2).downto(0)
14+
if a[i]>max_from_right
15+
max_from_right=a[i]
16+
print "#{max_from_right}"+" "
17+
end
18+
end
19+
end
20+
21+
find_leaders([16, 17, 4, 3, 5, 2]) # => 2 5 17
22+

Diff for: MaxSliceSumAlgorithm.rb

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Algorithm to find the largest possible continuos sum in an array.
22
#Time-complexity: O(n), Auxiliary space: O(1)
3+
# Algorithm-paradigm: Dynamic-programming
34

4-
5-
5+
# This algorithm doesn't work for negative numbers,it simply returns zero if all numbers are negative.
66
def max_continuos_sum(a)
77
max_ending_sum = max_slice_sum =0
88
n=a.length
@@ -13,4 +13,18 @@ def max_continuos_sum(a)
1313
return max_slice_sum
1414
end
1515

16-
max_continuos_sum([1,1,-1,2,3,4,-9,3,4,5,-6])
16+
max_continuos_sum([1,1,-1,2,3,4,-9,3,4,5,-6]) # => 13
17+
18+
19+
# Algorithm to handle negative numbers as well
20+
def max_continuos_sum(a)
21+
max_ending_sum = max_slice_sum =a[0]
22+
n=a.length
23+
for i in 0...n
24+
max_ending_sum = [a[i],(max_ending_sum+a[i])].max
25+
max_slice_sum = [max_slice_sum,max_ending_sum].max
26+
end
27+
return max_slice_sum
28+
end
29+
30+
max_continuos_sum([-5,-4,-10,-2,-3,-2,-1,-9]) #=> -1

Diff for: README.md

+15-9
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,34 @@ The solutions are easy to understand and complete.
2222
6. [Merge Sort](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/sorting/MergeSort.rb)
2323
7. [Quick Sort](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/sorting/QuickSort.rb)
2424
8. [3 way Quick Sort](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/sorting/3wayQuickSort.rb)
25-
9. [Counting Sort](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/sorting/3wayQuickSort.rb)
25+
9. [Counting Sort](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/sorting/CountingSort.rb)
26+
10. [Sort array in wave form](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/sorting/SortInWaveForm.rb)
2627

2728

2829
### Others ###
2930

30-
1. Algorithms for array rotation
31+
1. [Swapping two numbers](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/SwappingTwoNumbers.rb)
32+
2. [Algorithm for reversing an array]((https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/ReverseAnArray.rb))
33+
3. Algorithms for array rotation
3134
* [Juggling Algorithm for array rotation](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/JugglingAlgorithm.rb)
3235
* [Reversal Algorithm for array rotation](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/ReversalAlgorithm.rb)
3336
* Block Swap Algorithm for array rotation
3437
* [Recursive](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/BlockSwapAlgorithmRecursive.rb)
3538
* [Iterative](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/BlockSwapAlgorithmIterative.rb)
36-
2. [Algorithm to find maximum continuos sum in an array(Kadane's Algorithm)](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/MaxSliceSumAlgorithm.rb)
37-
3. [Find missing number in array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/FindMissingNumber.rb)
38-
4. [Swapping two numbers](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/SwappingTwoNumbers.rb)
39-
5. Shuffling an Array
39+
4. [Algorithm to find maximum continuos sum in an array(Kadane's Algorithm)](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/MaxSliceSumAlgorithm.rb)
40+
5. [Find missing number in array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/FindMissingNumber.rb)
41+
6. Shuffling an Array
4042
* [Knuth shuffling Algorithm](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/KnuthShufflingAlgorithm.rb)
4143
* [Fitcher-Yates shuffling Algorithm](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/FitcherYatesShufflingAlgorithm.rb)
42-
6. [Find pair in array with sum equal to given sum](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/ChekPairWithGivenSum.rb)
43-
7. [Find equilibrium index in an array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/EquilibriumIndexofArray.rb)
44-
8. Find fixed point in an array
44+
7. [Find pair in array with sum equal to given sum](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/ChekPairWithGivenSum.rb)
45+
8. [Find pair in array with sum closest to given sum](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/ClosestSum.rb)
46+
9. [Find equilibrium index in an array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/EquilibriumIndexofArray.rb)
47+
10. Find fixed point in an array
4548
* [fixed point in unsorted array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/FixedPointInUnsorted.rb)
4649
* [fixed point in sorted array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/FixedPointInSorted.rb)
50+
11. Dutch National Flag Problem
51+
* [Segregate Os and 1s in a given array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/Segregate0and1.rb)
52+
12. [Find all Leaders in an array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/LeadersInArray.rb)
4753

4854

4955

Diff for: ReverseAnArray.rb

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#Given an array reverse it without using extra-space
2+
#Time-complexity: O(n), Auxiliary-space: O(1)
3+
4+
# Iterative
5+
def reverse_array(a)
6+
right=a.length-1
7+
left=0
8+
while(left<right)
9+
#swap the elements
10+
temp=a[left] #Swapping can be like done like this a[left],a[right]=a[right],a[left]
11+
a[left]=a[right]
12+
a[right]=temp
13+
14+
left+=1
15+
right-=1
16+
end
17+
print a
18+
end
19+
20+
reverse_array([1,2,3,4,5]) # => [5,4,3,2,1]
21+
22+
# Recursive
23+
24+
#driver program
25+
def driver_program(a)
26+
right=a.length-1
27+
left=0
28+
reverse_array(a,left,right)
29+
print a
30+
end
31+
def reverse_array(a,left,right)
32+
33+
return if left>=right
34+
#swap the elements
35+
temp=a[left]
36+
a[left]=a[right]
37+
a[right]=temp
38+
reverse_array(a,left+1,right-1)
39+
end
40+
41+
driver_program([1,2,3,4,5]) # => [5,4,3,2,1]

Diff for: Segregate0and1.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#Given an array of 0s and 1s in random order.Segregate 0s on left side and 1s on right side of the array.
2+
##Brute-force: Count 0s and rewrite array first with counted zeros then by 1
3+
#Time-complexity:O(n),Auxiliary-space:O(1)
4+
5+
6+
##Algorithm: Dutch National Flag Algorithm
7+
#Time-complexity:O(n),Auxiliary-space:O(1)
8+
9+
def segregate(a)
10+
left=0
11+
right=a.length-1
12+
while left<right
13+
#Increment left index while we see 0 at left
14+
while a[left]== 0
15+
left+=1
16+
end
17+
#Decrement right index while we see 1 at right
18+
while a[right]==1
19+
right-=1
20+
end
21+
#Exchange arr[left] and arr[right]
22+
if left<right
23+
a[left],a[right]=a[right],a[left]
24+
left+=1
25+
right-=1
26+
end
27+
end
28+
return a
29+
end
30+
segregate([1,0,1,1,0,0]) # => [0, 0, 0, 1, 1, 1]

Diff for: sorting/SortInWaveForm.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=begin
2+
Given an unsorted array of integers, sort the array into a wave like array.
3+
An array ‘arr[0..n-1]’ is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= …..
4+
=end
5+
#Time-complexity: O(n)
6+
#Auxiliary-space: O(1)
7+
8+
def sort_in_wave(a)
9+
n= a.length
10+
(0...n).step(2) do |i|
11+
if ((i>0) && (a[i]<a[i-1]))
12+
swap(a,i,i-1)
13+
end
14+
15+
if ((i<n-1) && (a[i]<a[i+1]))
16+
swap(a,i,i+1)
17+
end
18+
end
19+
return a
20+
end
21+
22+
def swap(a,i,j)
23+
a[i],a[j]=a[j],a[i]
24+
end
25+
26+
sort_in_wave([10,90,49,2,1,5,23]) # => [90,10,49,1,5,2,23]
27+

0 commit comments

Comments
 (0)