Last active
December 22, 2015 18:39
-
-
Save beck03076/6514755 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#================================== | |
# Author : Senthil | |
# Author Nick : beck03076 | |
# Description : Given N integers, Count the total pairs of Integers that have a difference of k | |
#================================== | |
# Getting input from console | |
puts "Enter comma separated numbers" | |
a = gets.chomp.split(",").map(&:to_i) | |
puts "Enter the value of K(difference)" | |
k = gets.chomp.to_i | |
# setting count | |
count = 0 | |
# looping the n number of integers | |
for i in 1..a.length - 1 | |
# iterating backwards from every element | |
i.downto(0) { |j| | |
p "Going to try #{a[i]} minus #{a[j-1]}.." | |
try = a[i] - a[j-1] | |
if try == k | |
count = count + 1 | |
p "#####==SUCCESS==#### at #{a[i]} minus #{a[j-1]}.." | |
elsif try > k | |
break | |
end | |
} | |
end | |
# Result | |
p "There were #{a.size} number of integers in the given array .." | |
p "The value of difference k preferred by the user is #{k} .." | |
p "####== THERE ARE #{count} PAIRS OF INTEGERS THAT HAVE A DIFFERENCE OF #{k} ==####" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment