From f71493cddbb332ca4d315c32ae670e4a0b723229 Mon Sep 17 00:00:00 2001 From: AreebAlam99 Date: Tue, 3 Jan 2023 00:50:10 +0500 Subject: [PATCH 01/46] Problem 9 Palindrome Number in ruby --- Ruby/palindrome_number.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Ruby/palindrome_number.rb diff --git a/Ruby/palindrome_number.rb b/Ruby/palindrome_number.rb new file mode 100644 index 0000000..3014d0a --- /dev/null +++ b/Ruby/palindrome_number.rb @@ -0,0 +1,8 @@ +# Name: Areeb Alam +# Username: AreebAlam99 +# Approach: Converted the number into an array of digits and checked if it was equal to its reverse or not. + +def is_palindrome(x) + return false if x < 0 + x.digits == x.digits.reverse ? true : false +end From da023ecbcfb6e719818cab1cfa1fd4a810482879 Mon Sep 17 00:00:00 2001 From: AreebAlam99 Date: Thu, 5 Jan 2023 22:55:32 +0500 Subject: [PATCH 02/46] [LeetCode-26] Remove duplicates from an array. --- Ruby/remove_duplicates_from_array.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Ruby/remove_duplicates_from_array.rb diff --git a/Ruby/remove_duplicates_from_array.rb b/Ruby/remove_duplicates_from_array.rb new file mode 100644 index 0000000..82dccf9 --- /dev/null +++ b/Ruby/remove_duplicates_from_array.rb @@ -0,0 +1,8 @@ +# Name: Areeb Alam +# Username: AreebAlam99 +# Approach: Just used uniq to remove duplicates and returned count :p. + +def remove_duplicates(nums) + nums.uniq! + nums.count +end From 5235359e98ba751cd132f8df862e2e68ac12cdcd Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Wed, 11 Jan 2023 23:51:51 +0500 Subject: [PATCH 03/46] count remaining brackets --- Ruby/count_remaining_brackets.rb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Ruby/count_remaining_brackets.rb diff --git a/Ruby/count_remaining_brackets.rb b/Ruby/count_remaining_brackets.rb new file mode 100644 index 0000000..6923f2b --- /dev/null +++ b/Ruby/count_remaining_brackets.rb @@ -0,0 +1,6 @@ +# Name: Shoaib Sabir +# Username: Shoaib019 +# Approach: loop runs n times for each element and checks for appropriate conditions +def count_remaining_brackets(str) + +end \ No newline at end of file From 45d237b7366c50ab9d288a57b833bb4b81959d0a Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Wed, 11 Jan 2023 23:58:56 +0500 Subject: [PATCH 04/46] added missing code --- Ruby/count_remaining_brackets.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Ruby/count_remaining_brackets.rb b/Ruby/count_remaining_brackets.rb index 6923f2b..eab393e 100644 --- a/Ruby/count_remaining_brackets.rb +++ b/Ruby/count_remaining_brackets.rb @@ -1,6 +1,9 @@ # Name: Shoaib Sabir # Username: Shoaib019 -# Approach: loop runs n times for each element and checks for appropriate conditions +# Approach: deduct open from close bracket to get remaining brackets def count_remaining_brackets(str) - + open_brackets = str.count('(') + close_brackets = str.count(')') + remaining = open_brackets - close_brackets + remaining.negative? ? remaining.abs : remaining end \ No newline at end of file From 228d9307e10aabfee82dbfe7568446f9f9d9fe68 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Thu, 12 Jan 2023 02:01:59 +0500 Subject: [PATCH 05/46] 2 more ruby questions --- Ruby/count_consective_numbers.rb | 10 ++++++++++ Ruby/jump_tiles.rb | 11 +++++++++++ 2 files changed, 21 insertions(+) create mode 100644 Ruby/count_consective_numbers.rb create mode 100644 Ruby/jump_tiles.rb diff --git a/Ruby/count_consective_numbers.rb b/Ruby/count_consective_numbers.rb new file mode 100644 index 0000000..4b8dc20 --- /dev/null +++ b/Ruby/count_consective_numbers.rb @@ -0,0 +1,10 @@ +# Name: Shoaib Sabir +# Username: Shoaib019 +# Approach: make hash count as value and number as key then find max value return its key +def count_consective_numbers(arr) + hash_one = {} + arr.each do |val| + hash_one[val] = arr.count(val) + end + hash_one.max_by {|k,v| v}.first +end diff --git a/Ruby/jump_tiles.rb b/Ruby/jump_tiles.rb new file mode 100644 index 0000000..6de7308 --- /dev/null +++ b/Ruby/jump_tiles.rb @@ -0,0 +1,11 @@ +# Name: Shoaib Sabir +# Username: Shoaib019 +# Approach: in loop added only those elements on which pointer value is same +# then compare pointer value with size of array +def jump_tiles(arr) + pointer = 0 + arr.each_with_index do |_,idx| + pointer = pointer + arr[idx] if pointer == idx + end + pointer >= arr.length - 1 ? true : false +end \ No newline at end of file From d02e51f7882deadc7754083421035a9e87f842c7 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Fri, 13 Jan 2023 23:52:12 +0500 Subject: [PATCH 06/46] begin with sorting algos --- sorting_algos/bubble_sort.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 sorting_algos/bubble_sort.cpp diff --git a/sorting_algos/bubble_sort.cpp b/sorting_algos/bubble_sort.cpp new file mode 100644 index 0000000..d3c6ae9 --- /dev/null +++ b/sorting_algos/bubble_sort.cpp @@ -0,0 +1,18 @@ +void bubble_sort(int arr[],int size) +{ + for(int i=0; i < size -1; i++) + { + for(int j=0; j < size - i -1; j++) + { + if(arr[j] > arr[j+1]) + { + // std::cout << arr[i] << " > " << arr[j+1] << std::endl; + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + + } + } + +} \ No newline at end of file From 078aa5181fba8073c13e2cc76d9bffe6b313f840 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Sat, 14 Jan 2023 01:14:12 +0500 Subject: [PATCH 07/46] bubble sort recursive --- sorting_algos/bubble_sort.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/sorting_algos/bubble_sort.cpp b/sorting_algos/bubble_sort.cpp index d3c6ae9..d110245 100644 --- a/sorting_algos/bubble_sort.cpp +++ b/sorting_algos/bubble_sort.cpp @@ -1,18 +1,15 @@ void bubble_sort(int arr[],int size) -{ - for(int i=0; i < size -1; i++) +{ + if (size == 0 || size == 1) + return; + for(int i = 0; i < size - 1 ; i++) { - for(int j=0; j < size - i -1; j++) + if(arr[i] > arr[i + 1]) { - if(arr[j] > arr[j+1]) - { - // std::cout << arr[i] << " > " << arr[j+1] << std::endl; - int temp = arr[i]; - arr[i] = arr[j]; - arr[j] = temp; - } - - } + int temp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = temp; + } } - + bubble_sort(arr, size - 1); } \ No newline at end of file From 6585fa8b8de9ec751b9fc674433523bfdcda95d6 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Mon, 16 Jan 2023 23:58:39 +0500 Subject: [PATCH 08/46] bubble sort itrative --- sorting_algos/bubble_sort.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/sorting_algos/bubble_sort.cpp b/sorting_algos/bubble_sort.cpp index d110245..e2b00a9 100644 --- a/sorting_algos/bubble_sort.cpp +++ b/sorting_algos/bubble_sort.cpp @@ -1,3 +1,5 @@ +// recursive approach + void bubble_sort(int arr[],int size) { if (size == 0 || size == 1) @@ -12,4 +14,22 @@ void bubble_sort(int arr[],int size) } } bubble_sort(arr, size - 1); +} + +// itrative approach +void bubble_sort(int arr[],int size) +{ + for(int i = 0; i < size - 1 ; i++) + { + for(int j = 0 ; j < size - i - 1; j++) + { + if(arr[j] > arr[j + 1]) + { + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[i + j] = temp; + } + + } + } } \ No newline at end of file From c44605123bdd4014d7d07e666f21264c9185709e Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Tue, 17 Jan 2023 00:08:42 +0500 Subject: [PATCH 09/46] corrected bubble sort error --- sorting_algos/bubble_sort.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorting_algos/bubble_sort.cpp b/sorting_algos/bubble_sort.cpp index e2b00a9..f94a0c2 100644 --- a/sorting_algos/bubble_sort.cpp +++ b/sorting_algos/bubble_sort.cpp @@ -21,13 +21,13 @@ void bubble_sort(int arr[],int size) { for(int i = 0; i < size - 1 ; i++) { - for(int j = 0 ; j < size - i - 1; j++) + for(int j = 0; j < size - i - 1; j++) { if(arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; - arr[i + j] = temp; + arr[j + 1] = temp; } } From e267490ae66a3e04d11ad22ee43bd7d247ffed93 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Tue, 17 Jan 2023 23:55:36 +0500 Subject: [PATCH 10/46] selection sort --- sorting_algos/selection_sort.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 sorting_algos/selection_sort.cpp diff --git a/sorting_algos/selection_sort.cpp b/sorting_algos/selection_sort.cpp new file mode 100644 index 0000000..d9cc788 --- /dev/null +++ b/sorting_algos/selection_sort.cpp @@ -0,0 +1,16 @@ +void selection_sort(int arr[],int size) +{ + int min_idx; + for(int i = 0; i < size - 1; i++) + { + min_idx = i; + for(int j = i+1; j < size; j++) + { + if(arr[j] < arr[min_idx]) + min_idx = j; + } + int temp = arr[min_idx]; + arr[min_idx] = arr[i]; + arr[i] = temp; + } +} \ No newline at end of file From 666d99816dad471373528afe2ca784cb8a6f94bc Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Wed, 18 Jan 2023 23:12:19 +0500 Subject: [PATCH 11/46] code to convert arr of hashes into single hash --- Ruby/make_one_hash.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Ruby/make_one_hash.rb diff --git a/Ruby/make_one_hash.rb b/Ruby/make_one_hash.rb new file mode 100644 index 0000000..79a372f --- /dev/null +++ b/Ruby/make_one_hash.rb @@ -0,0 +1,12 @@ +def make_one_hash(arr) + hash = {} + arr.each do |ele| + ele.each do |k, v| + hash[k] = v + end + end + hash +end + +arr = [{ a: 3, b: 2, c: 3, d: 4 }, { e: 3, f: 2 }, { g: 2, h: 3 }] +puts make_one_hash(arr) From c451e7ba56093752e8fb138286efe346f5d72793 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Wed, 18 Jan 2023 23:12:59 +0500 Subject: [PATCH 12/46] sort hash by key size --- Ruby/sort_hash_by_key_size.rb | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Ruby/sort_hash_by_key_size.rb diff --git a/Ruby/sort_hash_by_key_size.rb b/Ruby/sort_hash_by_key_size.rb new file mode 100644 index 0000000..a606daf --- /dev/null +++ b/Ruby/sort_hash_by_key_size.rb @@ -0,0 +1,2 @@ +hash = {"abcd": 4, "abc": 3, "abcdf": 5, "ab": 2 } +puts Hash[hash.sort_by {|k,_| k.size}] \ No newline at end of file From f06d62938747be4fdafa7fed4eee7b8a90e497e7 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Wed, 18 Jan 2023 23:20:20 +0500 Subject: [PATCH 13/46] rubocop fixed --- Ruby/count_consective_numbers.rb | 6 ++++-- Ruby/count_remaining_brackets.rb | 12 +++++++----- Ruby/fizzbuzz.rb | 27 +++++++++++++++------------ Ruby/jump_tiles.rb | 14 ++++++++------ Ruby/make_one_hash.rb | 2 ++ Ruby/palindrome_number.rb | 9 ++++++--- Ruby/remove_duplicates_from_array.rb | 2 ++ Ruby/sort_hash_by_key_size.rb | 6 ++++-- 8 files changed, 48 insertions(+), 30 deletions(-) diff --git a/Ruby/count_consective_numbers.rb b/Ruby/count_consective_numbers.rb index 4b8dc20..d057666 100644 --- a/Ruby/count_consective_numbers.rb +++ b/Ruby/count_consective_numbers.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + # Name: Shoaib Sabir # Username: Shoaib019 # Approach: make hash count as value and number as key then find max value return its key def count_consective_numbers(arr) hash_one = {} arr.each do |val| - hash_one[val] = arr.count(val) + hash_one[val] = arr.count(val) end - hash_one.max_by {|k,v| v}.first + hash_one.max_by { |_k, v| v }.first end diff --git a/Ruby/count_remaining_brackets.rb b/Ruby/count_remaining_brackets.rb index eab393e..54e744a 100644 --- a/Ruby/count_remaining_brackets.rb +++ b/Ruby/count_remaining_brackets.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + # Name: Shoaib Sabir # Username: Shoaib019 # Approach: deduct open from close bracket to get remaining brackets def count_remaining_brackets(str) - open_brackets = str.count('(') - close_brackets = str.count(')') - remaining = open_brackets - close_brackets - remaining.negative? ? remaining.abs : remaining -end \ No newline at end of file + open_brackets = str.count('(') + close_brackets = str.count(')') + remaining = open_brackets - close_brackets + remaining.negative? ? remaining.abs : remaining +end diff --git a/Ruby/fizzbuzz.rb b/Ruby/fizzbuzz.rb index 6ec738b..2e2b93a 100644 --- a/Ruby/fizzbuzz.rb +++ b/Ruby/fizzbuzz.rb @@ -1,14 +1,17 @@ +# frozen_string_literal: true + # Name: Shoaib Sabir # Username: Shoaib019 -# Approach: loop runs n times for each element and checks for appropriate conditions -def fizz_buzz(n) - arr = [] - (1..n).each do |num| - if (num.modulo(3).zero? && num.modulo(5).zero?) then arr << "FizzBuzz" - elsif num.modulo(3).zero? then arr << "Fizz" - elsif num.modulo(5).zero? then arr << "Buzz" - else arr << num.to_s - end - end - arr -end \ No newline at end of file +# Approach: loop runs number times for each element and checks for appropriate conditions +def fizz_buzz(number) + arr = [] + (1..number).each do |num| + arr << if num.modulo(3).zero? && num.modulo(5).zero? then 'FizzBuzz' + elsif num.modulo(3).zero? then 'Fizz' + elsif num.modulo(5).zero? then 'Buzz' + else + num.to_s + end + end + arr +end diff --git a/Ruby/jump_tiles.rb b/Ruby/jump_tiles.rb index 6de7308..0e5fd79 100644 --- a/Ruby/jump_tiles.rb +++ b/Ruby/jump_tiles.rb @@ -1,11 +1,13 @@ +# frozen_string_literal: true + # Name: Shoaib Sabir # Username: Shoaib019 # Approach: in loop added only those elements on which pointer value is same # then compare pointer value with size of array def jump_tiles(arr) - pointer = 0 - arr.each_with_index do |_,idx| - pointer = pointer + arr[idx] if pointer == idx - end - pointer >= arr.length - 1 ? true : false -end \ No newline at end of file + pointer = 0 + arr.each_with_index do |_, idx| + pointer += arr[idx] if pointer == idx + end + pointer >= arr.length - 1 +end diff --git a/Ruby/make_one_hash.rb b/Ruby/make_one_hash.rb index 79a372f..bf6b450 100644 --- a/Ruby/make_one_hash.rb +++ b/Ruby/make_one_hash.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + def make_one_hash(arr) hash = {} arr.each do |ele| diff --git a/Ruby/palindrome_number.rb b/Ruby/palindrome_number.rb index 3014d0a..4936da6 100644 --- a/Ruby/palindrome_number.rb +++ b/Ruby/palindrome_number.rb @@ -1,8 +1,11 @@ +# frozen_string_literal: true + # Name: Areeb Alam # Username: AreebAlam99 # Approach: Converted the number into an array of digits and checked if it was equal to its reverse or not. -def is_palindrome(x) - return false if x < 0 - x.digits == x.digits.reverse ? true : false +def palindrome?(num) + return false if num.negative? + + num.digits == num.digits.reverse end diff --git a/Ruby/remove_duplicates_from_array.rb b/Ruby/remove_duplicates_from_array.rb index 82dccf9..5201dd7 100644 --- a/Ruby/remove_duplicates_from_array.rb +++ b/Ruby/remove_duplicates_from_array.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Name: Areeb Alam # Username: AreebAlam99 # Approach: Just used uniq to remove duplicates and returned count :p. diff --git a/Ruby/sort_hash_by_key_size.rb b/Ruby/sort_hash_by_key_size.rb index a606daf..863a349 100644 --- a/Ruby/sort_hash_by_key_size.rb +++ b/Ruby/sort_hash_by_key_size.rb @@ -1,2 +1,4 @@ -hash = {"abcd": 4, "abc": 3, "abcdf": 5, "ab": 2 } -puts Hash[hash.sort_by {|k,_| k.size}] \ No newline at end of file +# frozen_string_literal: true + +hash = { "abcd": 4, "abc": 3, "abcdf": 5, "ab": 2 } +puts Hash[hash.sort_by { |k, _| k.size }] From 23e09e80381d9b8639e863df76cfbf12d515b28f Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Fri, 20 Jan 2023 23:56:35 +0500 Subject: [PATCH 14/46] find missing number in game --- Ruby/missing_game_number.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Ruby/missing_game_number.rb diff --git a/Ruby/missing_game_number.rb b/Ruby/missing_game_number.rb new file mode 100644 index 0000000..c41b070 --- /dev/null +++ b/Ruby/missing_game_number.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +def missing_game_number(arr) + (1..10).each do |num| + arr << num if arr.exclude?(num) + end + arr.sort +end From 180adcfa240e076ef9105f5fad76047556ea5cc5 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Sat, 21 Jan 2023 00:28:33 +0500 Subject: [PATCH 15/46] do multiply without sum * operator --- Ruby/inventing_multiplication | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Ruby/inventing_multiplication diff --git a/Ruby/inventing_multiplication b/Ruby/inventing_multiplication new file mode 100644 index 0000000..a2b587f --- /dev/null +++ b/Ruby/inventing_multiplication @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +def inventing_multiplication(num1, num2) + res = 0 + num2.times.each do |_num| + res += num1 + end + res +end From d304d4a9b52e0d2179c3252f739e92b3a9e2b6e3 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Sat, 21 Jan 2023 14:57:35 +0500 Subject: [PATCH 16/46] mremoved nested loop --- Ruby/make_one_hash.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Ruby/make_one_hash.rb b/Ruby/make_one_hash.rb index bf6b450..6325990 100644 --- a/Ruby/make_one_hash.rb +++ b/Ruby/make_one_hash.rb @@ -3,9 +3,7 @@ def make_one_hash(arr) hash = {} arr.each do |ele| - ele.each do |k, v| - hash[k] = v - end + hash.merge!(ele) end hash end From 03f6642e53ef0f867f77a107109e8e0ca2a8af08 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Wed, 25 Jan 2023 22:32:57 +0500 Subject: [PATCH 17/46] insertion sort incomplete --- sorting_algos/insertion_sort.cpp | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 sorting_algos/insertion_sort.cpp diff --git a/sorting_algos/insertion_sort.cpp b/sorting_algos/insertion_sort.cpp new file mode 100644 index 0000000..4bcf1c0 --- /dev/null +++ b/sorting_algos/insertion_sort.cpp @@ -0,0 +1,4 @@ +void insertion_sort(int arr[],int size) +{ + //code here +} \ No newline at end of file From c5c459506487bd57936eb3471276e35359ac0b58 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Thu, 26 Jan 2023 23:29:42 +0500 Subject: [PATCH 18/46] insertion sort --- sorting_algos/insertion_sort.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/sorting_algos/insertion_sort.cpp b/sorting_algos/insertion_sort.cpp index 4bcf1c0..b97a4dd 100644 --- a/sorting_algos/insertion_sort.cpp +++ b/sorting_algos/insertion_sort.cpp @@ -1,4 +1,16 @@ void insertion_sort(int arr[],int size) { - //code here + for(int i = 1; i < size; i++) + { + int key = arr[i]; + int j = i - 1; + + while( j >= 0 && arr[j] > key) + { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } + } \ No newline at end of file From 54e8293b3ce2106bbc5a812b527649a6d1ab0fae Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Fri, 27 Jan 2023 23:32:47 +0500 Subject: [PATCH 19/46] find largest sum of subarray from number array --- Ruby/largest_sum.rb | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Ruby/largest_sum.rb diff --git a/Ruby/largest_sum.rb b/Ruby/largest_sum.rb new file mode 100644 index 0000000..5b221b1 --- /dev/null +++ b/Ruby/largest_sum.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +def largest_sum; end From a240dcd0959f4da58d283bca0a9f7898fd6711d9 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Wed, 1 Feb 2023 00:00:48 +0500 Subject: [PATCH 20/46] puting zeros at the end of the array --- C++/put_zeros_at_end_of_array.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 C++/put_zeros_at_end_of_array.cpp diff --git a/C++/put_zeros_at_end_of_array.cpp b/C++/put_zeros_at_end_of_array.cpp new file mode 100644 index 0000000..2650f6a --- /dev/null +++ b/C++/put_zeros_at_end_of_array.cpp @@ -0,0 +1,28 @@ +// Name: Shoaib Sabir +// Username: Shoaib019 +// Approach: find how many zeros there by loop then another loop will put zeros to the end +class Solution { +public: + void append_zeros(int nums [],int size) { + int last_idex = size - 1; + int count = 0; + for(int i = 0; i < size; i++){ + if(nums[i] == 0) + count++; + } + std::cout << "count : "<< count << std::endl; + for(int i = 0; i < size && count <= 0; i++) + { + if(nums[i] == 0) + { + int tmp = nums[i]; + nums[i] = nums[last_idex]; + nums[last_idex] = tmp; + last_idex--; + count--; + } + + } + +} +}; From bd46a21d2a922070df60f99ff72cb370c708f3a1 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Wed, 1 Feb 2023 23:24:47 +0500 Subject: [PATCH 21/46] fixed the code of shifting zeros at end of array --- C++/put_zeros_at_end_of_array.cpp | 47 ++++++++++++++++++------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/C++/put_zeros_at_end_of_array.cpp b/C++/put_zeros_at_end_of_array.cpp index 2650f6a..580b77e 100644 --- a/C++/put_zeros_at_end_of_array.cpp +++ b/C++/put_zeros_at_end_of_array.cpp @@ -1,28 +1,35 @@ // Name: Shoaib Sabir // Username: Shoaib019 -// Approach: find how many zeros there by loop then another loop will put zeros to the end +// Approach: take two pointers one at end and one at beginning of array +// use while loop to terminate when both pointers colid +// inside loop just swap when non zero at beginning pointer +// and when non zero at end pointer otherwise just increase beginning pointer class Solution { public: void append_zeros(int nums [],int size) { - int last_idex = size - 1; - int count = 0; - for(int i = 0; i < size; i++){ - if(nums[i] == 0) - count++; - } - std::cout << "count : "<< count << std::endl; - for(int i = 0; i < size && count <= 0; i++) + int last_indx = size - 1; + int start_indx = 0; + while(start_indx < last_indx) { - if(nums[i] == 0) - { - int tmp = nums[i]; - nums[i] = nums[last_idex]; - nums[last_idex] = tmp; - last_idex--; - count--; - } - + if(nums[start_indx] == 0) + { + if(nums[last_indx] == 0) + { + last_indx--; + } + else + { + nums[start_indx] = nums[last_indx]; + nums[last_indx] = 0; + start_indx++; + last_indx--; + } + + } + else + { + start_indx++; + } } - -} + } }; From 0c3e2018375673b39dcb485bf32369e85f28db64 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Thu, 2 Feb 2023 00:38:19 +0500 Subject: [PATCH 22/46] make template for problem to solve later --- C++/remove_special_characters_from_aaray.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 C++/remove_special_characters_from_aaray.cpp diff --git a/C++/remove_special_characters_from_aaray.cpp b/C++/remove_special_characters_from_aaray.cpp new file mode 100644 index 0000000..f73ea04 --- /dev/null +++ b/C++/remove_special_characters_from_aaray.cpp @@ -0,0 +1,8 @@ +// Name: Shoaib Sabir +// Username: Shoaib019 +// Approach: +class Solution { +public: + void remove_special_charactes(int arr [],int size) { + } +}; From 357a04d464bf47a127aa1bc3af51953671ee3faf Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Thu, 2 Feb 2023 23:37:40 +0500 Subject: [PATCH 23/46] removing specials characters from array problem still in progress --- C++/remove_special_characters_from_aaray.cpp | 29 ++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/C++/remove_special_characters_from_aaray.cpp b/C++/remove_special_characters_from_aaray.cpp index f73ea04..532c538 100644 --- a/C++/remove_special_characters_from_aaray.cpp +++ b/C++/remove_special_characters_from_aaray.cpp @@ -3,6 +3,31 @@ // Approach: class Solution { public: - void remove_special_charactes(int arr [],int size) { - } + int remove_character(char arr [], char ch, int size){ + int i; + for(i=0; i< size; i++) + { + if(arr[i] == ch) + break; + } + if(i < size) + { + size -= 1; + for(int j=i; j < size; j++) + { + arr[j] = arr[j + 1]; + } + arr[size + 1] = 0; + } + return size; +} + +void remove_special_charactes(char arr [],int size) { + + for(int i=0; i< size; i++) + { + if(arr[i] < 97 && arr[i] < 122) + size = remove_character(arr,arr[i],size); + } +} }; From b317e7e01ca3e67c4e5c50aa6a9595c881726d59 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Fri, 3 Feb 2023 23:59:15 +0500 Subject: [PATCH 24/46] removes special character from array algo is complete --- C++/remove_special_characters_from_aaray.cpp | 41 ++++++++++++++------ 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/C++/remove_special_characters_from_aaray.cpp b/C++/remove_special_characters_from_aaray.cpp index 532c538..2134058 100644 --- a/C++/remove_special_characters_from_aaray.cpp +++ b/C++/remove_special_characters_from_aaray.cpp @@ -1,9 +1,14 @@ // Name: Shoaib Sabir // Username: Shoaib019 -// Approach: +// Approach: remove_character shifts array to let by one character +// is_special_character checks whether character is special or not +// remove_special_charactes reduce size if last character is special +// and runs loop whenever special character finds in the array it removes +// find special character using is_special_character method and remove +// special charactes using remove_character method class Solution { public: - int remove_character(char arr [], char ch, int size){ + int remove_character(char arr [], char ch, int size){ int i; for(i=0; i< size; i++) { @@ -12,7 +17,7 @@ class Solution { } if(i < size) { - size -= 1; + size--; for(int j=i; j < size; j++) { arr[j] = arr[j + 1]; @@ -22,12 +27,26 @@ class Solution { return size; } -void remove_special_charactes(char arr [],int size) { - - for(int i=0; i< size; i++) - { - if(arr[i] < 97 && arr[i] < 122) - size = remove_character(arr,arr[i],size); - } -} + bool is_special_character(char ch){ + if((!(ch >= 97 && ch <= 122) && !(ch >= 65 && ch <= 90) )) + return true; + else + return false; + } + + int remove_special_charactes(char arr [],int size) { + if(is_special_character(arr[size - 1])) + { + size--; + } + for(int i=0; i< size; i++) + { + if(is_special_character(arr[i])) + { + size = remove_character(arr,arr[i],size); + i--; + } + } + return size; + } }; From a27b983131827a4bd4d666937f0863d67dc41f01 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Sat, 4 Feb 2023 22:15:10 +0500 Subject: [PATCH 25/46] created template to start on problem --- Ruby/count_max_letter_in_array.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Ruby/count_max_letter_in_array.rb diff --git a/Ruby/count_max_letter_in_array.rb b/Ruby/count_max_letter_in_array.rb new file mode 100644 index 0000000..c667a27 --- /dev/null +++ b/Ruby/count_max_letter_in_array.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# Name: Shoaib Sabir +# Username: Shoaib019 +# Approach: +def find_max_frequency_character(arr) + +end From 5195391748d711dbdf2efda66e109b552fa23a1f Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Sun, 5 Feb 2023 00:43:39 +0500 Subject: [PATCH 26/46] started problem to write algo in n time complexcity max frquency of char --- Ruby/count_max_letter_in_array.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Ruby/count_max_letter_in_array.rb b/Ruby/count_max_letter_in_array.rb index c667a27..458ae56 100644 --- a/Ruby/count_max_letter_in_array.rb +++ b/Ruby/count_max_letter_in_array.rb @@ -3,6 +3,9 @@ # Name: Shoaib Sabir # Username: Shoaib019 # Approach: -def find_max_frequency_character(arr) - +def find_max_frequency_character(str) + arr = [nil,nil] + str.each_char_with_index do |ch,idx| + + end end From 33dd0d46c0d38fd166511c5876d23f79e3609674 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Mon, 6 Feb 2023 22:46:53 +0500 Subject: [PATCH 27/46] tried again but faild to make logic --- Ruby/count_max_letter_in_array.rb | 7 ++++--- ...nventing_multiplication => inventing_multiplication.rb} | 0 2 files changed, 4 insertions(+), 3 deletions(-) rename Ruby/{inventing_multiplication => inventing_multiplication.rb} (100%) diff --git a/Ruby/count_max_letter_in_array.rb b/Ruby/count_max_letter_in_array.rb index 458ae56..a82942a 100644 --- a/Ruby/count_max_letter_in_array.rb +++ b/Ruby/count_max_letter_in_array.rb @@ -4,8 +4,9 @@ # Username: Shoaib019 # Approach: def find_max_frequency_character(str) - arr = [nil,nil] - str.each_char_with_index do |ch,idx| - + arr = [nil, nil] + str.each_char_with_index do |ch, idx| + # do from here + end end diff --git a/Ruby/inventing_multiplication b/Ruby/inventing_multiplication.rb similarity index 100% rename from Ruby/inventing_multiplication rename to Ruby/inventing_multiplication.rb From f0cc457c6464b4ff22065071b7c202846a6e2c9d Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Tue, 7 Feb 2023 22:42:45 +0500 Subject: [PATCH 28/46] finally done max frequncy in string in n time complexcity --- Ruby/count_max_letter_in_array.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Ruby/count_max_letter_in_array.rb b/Ruby/count_max_letter_in_array.rb index a82942a..e5285fa 100644 --- a/Ruby/count_max_letter_in_array.rb +++ b/Ruby/count_max_letter_in_array.rb @@ -2,11 +2,13 @@ # Name: Shoaib Sabir # Username: Shoaib019 -# Approach: +# Approach: take hash and initialize it with zero run loop over string +# increase count every time in hash key which is character +# then use max_by built in method to find letter with max count def find_max_frequency_character(str) - arr = [nil, nil] - str.each_char_with_index do |ch, idx| - # do from here - + hash = Hash.new(0) + str.each_char do |ch| + hash[ch] += 1 end + hash.max_by { |_, v| v }.first end From 56f2631dd9f071500e56f091bda1240eecf04f4f Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Wed, 8 Feb 2023 00:08:56 +0500 Subject: [PATCH 29/46] template for query --- MYSQL/user_name_and_count_of_invoices.sql | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 MYSQL/user_name_and_count_of_invoices.sql diff --git a/MYSQL/user_name_and_count_of_invoices.sql b/MYSQL/user_name_and_count_of_invoices.sql new file mode 100644 index 0000000..71cd1a5 --- /dev/null +++ b/MYSQL/user_name_and_count_of_invoices.sql @@ -0,0 +1,4 @@ +Write an SQL query to find username and count of invoices from user and invoice table. + +Return the result table in any order. +# Write your MySQL query statement below \ No newline at end of file From 5469c661594e097448f6114101613abcbcb67a7a Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Thu, 9 Feb 2023 23:17:00 +0500 Subject: [PATCH 30/46] js phone shop template --- JS/phone_shop.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 JS/phone_shop.js diff --git a/JS/phone_shop.js b/JS/phone_shop.js new file mode 100644 index 0000000..78c2f87 --- /dev/null +++ b/JS/phone_shop.js @@ -0,0 +1,17 @@ +TAX_RATE = +PHONE_PRICE = +ACCESSORY_PRICE = +SPENDING_THRESHOLD = +var bank_account_balance = + +function calculateTotalPrice() { + +} + +function calculatePurchaseAmount() { + +} + +function calculatingTax(){ + +} \ No newline at end of file From c686122711557e2d732056578c4288714e84dea7 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Sat, 11 Feb 2023 02:44:21 +0500 Subject: [PATCH 31/46] query solved --- MYSQL/user_name_and_count_of_invoices.sql | 39 +++++++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/MYSQL/user_name_and_count_of_invoices.sql b/MYSQL/user_name_and_count_of_invoices.sql index 71cd1a5..1ed00ea 100644 --- a/MYSQL/user_name_and_count_of_invoices.sql +++ b/MYSQL/user_name_and_count_of_invoices.sql @@ -1,4 +1,37 @@ -Write an SQL query to find username and count of invoices from user and invoice table. +-- Write an SQL query to find username and count of invoices from user and invoice table. -Return the result table in any order. -# Write your MySQL query statement below \ No newline at end of file +-- Return the result table in any order. +-- Write your MySQL query statement below +-- create a table +CREATE TABLE user ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + gender TEXT NOT NULL +); + +CREATE TABLE invoice ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + total INTEGER NOT NULL, + user_id INTEGER, + FOREIGN KEY (user_id) REFERENCES user(id) +); +-- insert some values +INSERT INTO user VALUES (1, 'Ryan', 'M'); +INSERT INTO user VALUES (2, 'Joanna', 'F'); +INSERT INTO user VALUES (3, 'Saul', 'M'); +INSERT INTO user VALUES (4, 'Norma', 'F'); + +INSERT INTO invoice VALUES (1, 'backery', 99,1); +INSERT INTO invoice VALUES (2, 'meat', 88,1); +INSERT INTO invoice VALUES (3, 'candy', 77,3); +INSERT INTO invoice VALUES (4, 'soaps', 66,4); +INSERT INTO invoice VALUES (5, 'meat', 55,2); +INSERT INTO invoice VALUES (6, 'candy', 44,2); +INSERT INTO invoice VALUES (7, 'soaps', 33,2); +-- fetch some values +SELECT user.name , COUNT(*) as count +FROM user +INNER JOIN invoice ON user.id = invoice.user_id +GROUP BY user.id +HAVING COUNT(*) > 1; \ No newline at end of file From 04fbc5607bce1374bb68339dfafd69c77fe6fe51 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Sun, 12 Feb 2023 16:32:47 +0500 Subject: [PATCH 32/46] js phone shop code --- JS/phone_shop.js | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/JS/phone_shop.js b/JS/phone_shop.js index 78c2f87..f0c1352 100644 --- a/JS/phone_shop.js +++ b/JS/phone_shop.js @@ -1,17 +1,40 @@ -TAX_RATE = -PHONE_PRICE = -ACCESSORY_PRICE = -SPENDING_THRESHOLD = -var bank_account_balance = +// DATA +TAX_RATE = 0.08; +PHONE_PRICE = 99.99; +ACCESSORY_PRICE = 9.99; +SPENDING_THRESHOLD = 200; +var bank_account_balance = 303.91; -function calculateTotalPrice() { +console.log(calculatePurchaseAmount()); // this is starting point +function calculatePurchaseAmount() { + totalSpent = calculateTotalPhonePrice(); + totalAmount = totalSpent + calculatingTax(totalSpent); + return formatThePrice(totalAmount); +} + +function canBuy(spent){ // this function keeps some amount in bank account + remaining_bal = bank_account_balance - spent; + return remaining_bal > ACCESSORY_PRICE ? true : false; +} + +function calculateTotalPhonePrice(){ + let spent = 0; + while (canBuy(spent)){ + spent += PHONE_PRICE; + + if(spent < SPENDING_THRESHOLD){ + spent += ACCESSORY_PRICE; + } + } + return spent; } -function calculatePurchaseAmount() { +function calculatingTax(totalSpent){ + return totalSpent * TAX_RATE; } -function calculatingTax(){ - +function formatThePrice(totalAmount){ + return "Total Price: $" + String(totalAmount.toFixed(2)); } \ No newline at end of file From 36e4b8678030a335f20436f3b893a938601c1439 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Tue, 14 Feb 2023 23:07:06 +0500 Subject: [PATCH 33/46] template ready --- Ruby/largest_sum_of_numbers_subarray.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Ruby/largest_sum_of_numbers_subarray.rb diff --git a/Ruby/largest_sum_of_numbers_subarray.rb b/Ruby/largest_sum_of_numbers_subarray.rb new file mode 100644 index 0000000..ff07f2f --- /dev/null +++ b/Ruby/largest_sum_of_numbers_subarray.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# Name: Shoaib Sabir +# Username: Shoaib019 +# Approach: +def largest_sum(arr) + # code here +end From 2f4585ed1918b63747e9f09eb5ba2c45415c13ac Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Sat, 18 Feb 2023 21:21:04 +0500 Subject: [PATCH 34/46] added questions --- C++/put_zeros_at_end_of_array.cpp | 1 + C++/remove_special_characters_from_aaray.cpp | 1 + JS/phone_shop.js | 5 +++++ Ruby/inventing_multiplication.rb | 3 +++ Ruby/jump_tiles.rb | 1 + Ruby/missing_game_number.rb | 3 +++ 6 files changed, 14 insertions(+) diff --git a/C++/put_zeros_at_end_of_array.cpp b/C++/put_zeros_at_end_of_array.cpp index 580b77e..cc04b1f 100644 --- a/C++/put_zeros_at_end_of_array.cpp +++ b/C++/put_zeros_at_end_of_array.cpp @@ -1,5 +1,6 @@ // Name: Shoaib Sabir // Username: Shoaib019 +// Question: given a array of numbers move all zeros to the end of the array // Approach: take two pointers one at end and one at beginning of array // use while loop to terminate when both pointers colid // inside loop just swap when non zero at beginning pointer diff --git a/C++/remove_special_characters_from_aaray.cpp b/C++/remove_special_characters_from_aaray.cpp index 2134058..d1f5a0b 100644 --- a/C++/remove_special_characters_from_aaray.cpp +++ b/C++/remove_special_characters_from_aaray.cpp @@ -1,5 +1,6 @@ // Name: Shoaib Sabir // Username: Shoaib019 +// Question: remove all special characters from characters array // Approach: remove_character shifts array to let by one character // is_special_character checks whether character is special or not // remove_special_charactes reduce size if last character is special diff --git a/JS/phone_shop.js b/JS/phone_shop.js index f0c1352..326e919 100644 --- a/JS/phone_shop.js +++ b/JS/phone_shop.js @@ -1,3 +1,8 @@ +// Name: Shoaib Sabir +// Username: Shoaib019 +// Question: find out you can afford phone or not by calculating +// phone price with accessories also add tax + // DATA TAX_RATE = 0.08; PHONE_PRICE = 99.99; diff --git a/Ruby/inventing_multiplication.rb b/Ruby/inventing_multiplication.rb index a2b587f..fdcd195 100644 --- a/Ruby/inventing_multiplication.rb +++ b/Ruby/inventing_multiplication.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +# Name: Shoaib Sabir +# Username: Shoaib019 +# Question: do multiplication without * operator def inventing_multiplication(num1, num2) res = 0 num2.times.each do |_num| diff --git a/Ruby/jump_tiles.rb b/Ruby/jump_tiles.rb index 0e5fd79..7c7ee49 100644 --- a/Ruby/jump_tiles.rb +++ b/Ruby/jump_tiles.rb @@ -2,6 +2,7 @@ # Name: Shoaib Sabir # Username: Shoaib019 +# Question: jump n times where n is current number in array and find did you able to reach to end of array # Approach: in loop added only those elements on which pointer value is same # then compare pointer value with size of array def jump_tiles(arr) diff --git a/Ruby/missing_game_number.rb b/Ruby/missing_game_number.rb index c41b070..9f49223 100644 --- a/Ruby/missing_game_number.rb +++ b/Ruby/missing_game_number.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +# Name: Shoaib Sabir +# Username: Shoaib019 +# Question: find missing number in array def missing_game_number(arr) (1..10).each do |num| arr << num if arr.exclude?(num) From df388e4f1c9cf7de13ba2e2040456a4e14ed8733 Mon Sep 17 00:00:00 2001 From: Shoaib sabir <86943632+Shoaib19@users.noreply.github.com> Date: Wed, 29 Mar 2023 22:29:11 +0500 Subject: [PATCH 35/46] added another approach --- sorting_algos/insertion_sort.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/sorting_algos/insertion_sort.cpp b/sorting_algos/insertion_sort.cpp index b97a4dd..d7708de 100644 --- a/sorting_algos/insertion_sort.cpp +++ b/sorting_algos/insertion_sort.cpp @@ -13,4 +13,21 @@ void insertion_sort(int arr[],int size) arr[j + 1] = key; } -} \ No newline at end of file +} + + +// another apporach apparently easy one + +void insertion_sort(int arr[], int size){ + for(int i = 1; i < size ; i++) + { + int j = i; + while(j > 0 && arr[j - 1] > arr[j]) + { + arr[j] = arr[j] + arr[j-1]; + arr[j-1] = arr[j] - arr[j-1]; + arr[j] = arr[j] - arr[j-1]; + j--; + } + } +} From 1c9907d63c2d87f9ba7c548ce9d6fb06a059ee16 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Wed, 29 Mar 2023 22:36:12 +0500 Subject: [PATCH 36/46] changed msg --- sorting_algos/insertion_sort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting_algos/insertion_sort.cpp b/sorting_algos/insertion_sort.cpp index d7708de..caa09e4 100644 --- a/sorting_algos/insertion_sort.cpp +++ b/sorting_algos/insertion_sort.cpp @@ -16,7 +16,7 @@ void insertion_sort(int arr[],int size) } -// another apporach apparently easy one +// another approach apparently easy one developed by me void insertion_sort(int arr[], int size){ for(int i = 1; i < size ; i++) From 1a28c1b23bdb8f90b933735a1dc8eaf338922fed Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Thu, 30 Mar 2023 21:56:05 +0500 Subject: [PATCH 37/46] removed temp var from bubble sort --- sorting_algos/bubble_sort.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sorting_algos/bubble_sort.cpp b/sorting_algos/bubble_sort.cpp index f94a0c2..a580af0 100644 --- a/sorting_algos/bubble_sort.cpp +++ b/sorting_algos/bubble_sort.cpp @@ -8,9 +8,9 @@ void bubble_sort(int arr[],int size) { if(arr[i] > arr[i + 1]) { - int temp = arr[i]; - arr[i] = arr[i + 1]; - arr[i + 1] = temp; + arr[i] = arr[i] + arr[i + 1]; + arr[i + 1] = arr[i] - arr[i + 1]; + arr[i] = arr[i] - arr[i + 1]; } } bubble_sort(arr, size - 1); @@ -25,9 +25,9 @@ void bubble_sort(int arr[],int size) { if(arr[j] > arr[j + 1]) { - int temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; + arr[j] = arr[j] + arr[j + 1]; + arr[j + 1] = arr[j] - arr[j + 1]; + arr[j] = arr[j] - arr[j + 1]; } } From 4f749b80e7ae5e96f02c0662d75fdddce0efc96f Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Fri, 31 Mar 2023 22:55:37 +0500 Subject: [PATCH 38/46] optimised selection sort --- sorting_algos/selection_sort.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sorting_algos/selection_sort.cpp b/sorting_algos/selection_sort.cpp index d9cc788..fa5a62e 100644 --- a/sorting_algos/selection_sort.cpp +++ b/sorting_algos/selection_sort.cpp @@ -9,8 +9,11 @@ void selection_sort(int arr[],int size) if(arr[j] < arr[min_idx]) min_idx = j; } - int temp = arr[min_idx]; - arr[min_idx] = arr[i]; - arr[i] = temp; + if(min_idx != i) + { + arr[i] = arr[i] + arr[min_idx]; + arr[min_idx] = arr[i] - arr[min_idx]; + arr[i] = arr[i] - arr[min_idx]; + } } } \ No newline at end of file From 2619db5508f254ae6f940d9bb9c31e6baf83f646 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Sat, 1 Apr 2023 22:35:06 +0500 Subject: [PATCH 39/46] created template for merge sort --- sorting_algos/merge_sort.cpp | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 sorting_algos/merge_sort.cpp diff --git a/sorting_algos/merge_sort.cpp b/sorting_algos/merge_sort.cpp new file mode 100644 index 0000000..5dcfa6a --- /dev/null +++ b/sorting_algos/merge_sort.cpp @@ -0,0 +1,4 @@ +void merge_sort(int arr[], int size) +{ + +} \ No newline at end of file From a1431e769d5d9e11317efa156d7f92024c3e4c1b Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Tue, 4 Apr 2023 21:29:57 +0500 Subject: [PATCH 40/46] started merge sort --- sorting_algos/merge_sort.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sorting_algos/merge_sort.cpp b/sorting_algos/merge_sort.cpp index 5dcfa6a..8912d07 100644 --- a/sorting_algos/merge_sort.cpp +++ b/sorting_algos/merge_sort.cpp @@ -1,4 +1,7 @@ void merge_sort(int arr[], int size) { - + if(left == right) + { + return arr; + } } \ No newline at end of file From 3eaa808ee31039c1c70ef166777e7a33b560123c Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Sat, 8 Apr 2023 15:54:04 +0500 Subject: [PATCH 41/46] not practicing recurrsion algos for now --- sorting_algos/merge_sort.cpp | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 sorting_algos/merge_sort.cpp diff --git a/sorting_algos/merge_sort.cpp b/sorting_algos/merge_sort.cpp deleted file mode 100644 index 8912d07..0000000 --- a/sorting_algos/merge_sort.cpp +++ /dev/null @@ -1,7 +0,0 @@ -void merge_sort(int arr[], int size) -{ - if(left == right) - { - return arr; - } -} \ No newline at end of file From dbbc8ec2dd6a7e4a17f35d69f8449911a38ad6b1 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Sun, 9 Apr 2023 21:45:09 +0500 Subject: [PATCH 42/46] started searching algos --- searching_algos/binary_search.cpp | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 searching_algos/binary_search.cpp diff --git a/searching_algos/binary_search.cpp b/searching_algos/binary_search.cpp new file mode 100644 index 0000000..5cf98e5 --- /dev/null +++ b/searching_algos/binary_search.cpp @@ -0,0 +1,4 @@ +void binary_search(int arr[],int size, int key) +{ + // code here +} \ No newline at end of file From 11ca583817dc69a2452641f17fda7490eecda575 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Mon, 10 Apr 2023 23:37:15 +0500 Subject: [PATCH 43/46] incomplete binary search --- searching_algos/binary_search.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/searching_algos/binary_search.cpp b/searching_algos/binary_search.cpp index 5cf98e5..58a4f68 100644 --- a/searching_algos/binary_search.cpp +++ b/searching_algos/binary_search.cpp @@ -1,4 +1,17 @@ -void binary_search(int arr[],int size, int key) +int binary_search(int arr[],int size, int key) { - // code here + int mid = size / 2; + if(arr[mid] == key) + return mid; + while(mid > 0 && mid < size) + { + if(key < arr[mid]) + { + mid = mid / 2; + } + else if(key > arr[mid]) + { + + } + } } \ No newline at end of file From 8270d86b6137e959c30df159607056a1d9f5dabe Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Tue, 11 Apr 2023 22:58:55 +0500 Subject: [PATCH 44/46] first try binary search --- searching_algos/binary_search.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/searching_algos/binary_search.cpp b/searching_algos/binary_search.cpp index 58a4f68..2475ea4 100644 --- a/searching_algos/binary_search.cpp +++ b/searching_algos/binary_search.cpp @@ -3,15 +3,21 @@ int binary_search(int arr[],int size, int key) int mid = size / 2; if(arr[mid] == key) return mid; - while(mid > 0 && mid < size) + int low = 0; + int high = size - 1; + + while(arr[mid] != key) { if(key < arr[mid]) { - mid = mid / 2; + high = mid; + mid = low + high / 2; } else if(key > arr[mid]) { - + low = mid; + mid = low + high / 2; } } + return (arr[mid] == key) ? mid : -1; } \ No newline at end of file From 33088ceb4b94faecf5fbac683e44eb4de68e2a7b Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Wed, 12 Apr 2023 22:52:42 +0500 Subject: [PATCH 45/46] still working on binary search --- searching_algos/binary_search.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/searching_algos/binary_search.cpp b/searching_algos/binary_search.cpp index 2475ea4..a122aa5 100644 --- a/searching_algos/binary_search.cpp +++ b/searching_algos/binary_search.cpp @@ -1,13 +1,12 @@ int binary_search(int arr[],int size, int key) { - int mid = size / 2; - if(arr[mid] == key) - return mid; int low = 0; int high = size - 1; - while(arr[mid] != key) + while(low <= high) { + mid = low + (high - low) / 2; + // start from here if(key < arr[mid]) { high = mid; From ad1ef008f839adce9c49a0f331c370b21ea5b4e4 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Sat, 15 Apr 2023 15:17:41 +0500 Subject: [PATCH 46/46] searching algos done for now --- searching_algos/binary_search.cpp | 19 +++++++++---------- searching_algos/linear_search.cpp | 9 +++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 searching_algos/linear_search.cpp diff --git a/searching_algos/binary_search.cpp b/searching_algos/binary_search.cpp index a122aa5..be0ccb2 100644 --- a/searching_algos/binary_search.cpp +++ b/searching_algos/binary_search.cpp @@ -1,22 +1,21 @@ int binary_search(int arr[],int size, int key) { - int low = 0; + int low, mid = 0; int high = size - 1; - while(low <= high) { - mid = low + (high - low) / 2; - // start from here + mid = low + (high - low) / 2; + if(arr[mid] == key) + return mid; + if(key < arr[mid]) { - high = mid; - mid = low + high / 2; + high = mid - 1; } - else if(key > arr[mid]) + else { - low = mid; - mid = low + high / 2; + low = mid + 1; } } - return (arr[mid] == key) ? mid : -1; + return -1; } \ No newline at end of file diff --git a/searching_algos/linear_search.cpp b/searching_algos/linear_search.cpp new file mode 100644 index 0000000..b7e6e05 --- /dev/null +++ b/searching_algos/linear_search.cpp @@ -0,0 +1,9 @@ +int linear_search(int arr[],int size, int key) +{ + for(int i = 0; i < size; i++) + { + if(arr[i] == key) + return i; + } + return -1; +} \ No newline at end of file