From 7ab1c9a398582935b112108408708ad03a8875bc Mon Sep 17 00:00:00 2001 From: rithik sharma <40556303+SharmaRithik@users.noreply.github.com> Date: Wed, 30 Sep 2020 14:01:43 +0530 Subject: [PATCH 01/23] Frugal numbers This is the implementation for finding frugal numbers in c++. --- frugal.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 frugal.cpp diff --git a/frugal.cpp b/frugal.cpp new file mode 100644 index 0000000..7566cd6 --- /dev/null +++ b/frugal.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; +int countdigits(int num) { + int count = 0; + while (num > 0) { + count += 1; + num /= 10; + } + return count; +} +int count(int n) { + int count1 = 0, x = n, i = 0; + for (i = 2; i * i <= x; i++) { + int count = 0; + while (n > 0 and n % i == 0) { + count += 1; + n /= i; + } + if (count > 0) { + count1 += countdigits(i); + if (count > 1) { + count1 += countdigits(count); + } + } + if (n <= 0) break; + } + return count1; +} +int main() { + int num; + cin >> num; + cout << count(num) << endl; + if (countdigits(num) > count(num)) + cout << "yes\n"; + else + cout << "no\n"; +} From 79c67dfe9c4005da6d60f2e44f72f9917306fe92 Mon Sep 17 00:00:00 2001 From: rithik sharma <40556303+SharmaRithik@users.noreply.github.com> Date: Thu, 1 Oct 2020 19:29:33 +0530 Subject: [PATCH 02/23] Binary search This includes deatils about binary search and it's recursive and iterative implementation in c++ --- binary-search-iterative.cpp | 31 +++++++++++++++++++++++++++++++ binary-search-recursion.cpp | 31 +++++++++++++++++++++++++++++++ binary-search.txt | 10 ++++++++++ 3 files changed, 72 insertions(+) create mode 100644 binary-search-iterative.cpp create mode 100644 binary-search-recursion.cpp create mode 100644 binary-search.txt diff --git a/binary-search-iterative.cpp b/binary-search-iterative.cpp new file mode 100644 index 0000000..18946da --- /dev/null +++ b/binary-search-iterative.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +int binarySearch(int num, int startingPoint, int endPoint, int arr[]) { + while (endPoint >= startingPoint) { + int mid = (startingPoint + endPoint) / 2; + if (arr[mid] == num) { + return mid; + } + if (arr[mid] > num) { + endPoint = mid - 1; + } + if (arr[mid] < num) { + startingPoint = mid + 1; + } + } + return 0; +} +int main() { + int a, num; + cout << "Input array size = "; + cin >> a; + int arr[a]; + cout << "Input array elements = "; + for (int i = 0; i < a; i++) { + cin >> arr[i]; + } + cout << "Input the number to be searched = "; + cin >> num; + cout << binarySearch(num, 0, a - 1, arr); + return 0; +} diff --git a/binary-search-recursion.cpp b/binary-search-recursion.cpp new file mode 100644 index 0000000..2220798 --- /dev/null +++ b/binary-search-recursion.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +int binarySearch(int num, int startingPoint, int endPoint, int arr[]) { + if (endPoint >= startingPoint) { + int mid = (startingPoint + endPoint) / 2; + if (arr[mid] == num) { + return mid; + } + if (arr[mid] > num) { + return binarySearch(num, startingPoint, mid - 1, arr); + } + if (arr[mid] < num) { + return binarySearch(num, mid + 1, endPoint, arr); + } + } + return 0; +} +int main() { + int a, num; + cout << "Input array size = "; + cin >> a; + int arr[a]; + cout << "Input array elements = "; + for (int i = 0; i < a; i++) { + cin >> arr[i]; + } + cout << "Input the number to be searched = "; + cin >> num; + cout << binarySearch(num, 0, a - 1, arr); + return 0; +} diff --git a/binary-search.txt b/binary-search.txt new file mode 100644 index 0000000..bb1c771 --- /dev/null +++ b/binary-search.txt @@ -0,0 +1,10 @@ +Binary search +* works on sorted array. +* repeatedly halving the array and searching in the half. + +Two ways: +1. Recusrsion +2. Iterative + +recursion, calls again the binarySearch function, iterative method is fast and easy. + From fa9e11ef0cd3bbe8ea9c981fbe774bd7793bf6bf Mon Sep 17 00:00:00 2001 From: SharmaRithik Date: Fri, 9 Oct 2020 23:41:04 +0530 Subject: [PATCH 03/23] Minimum time required to produce m items --- .../binary.cpp | 43 +++++++++++++++++++ .../brute.cpp | 27 ++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 gfg-practice/Minimum_time_required_to_produce_m_items/binary.cpp create mode 100644 gfg-practice/Minimum_time_required_to_produce_m_items/brute.cpp diff --git a/gfg-practice/Minimum_time_required_to_produce_m_items/binary.cpp b/gfg-practice/Minimum_time_required_to_produce_m_items/binary.cpp new file mode 100644 index 0000000..bd660e9 --- /dev/null +++ b/gfg-practice/Minimum_time_required_to_produce_m_items/binary.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; +int findItems(int arr[], int n, int temp) { + int ans = 0; + for (int i = 0; i < n; i++) ans += (temp / arr[i]); + return ans; +} +int bs(int arr[], int n, int m, int high) { + int low = 1; + while (low < high) { + int mid = (low + high) >> 1; + int itm = findItems(arr, n, mid); + if (itm < m) + low = mid + 1; + else + high = mid; + } + + return high; +} +int minTime(int arr[], int n, int m) { + int maxval = INT_MIN; + for (int i = 0; i < n; i++) maxval = max(maxval, arr[i]); + return bs(arr, n, m, maxval * m); +} +int main() { + int main() { + int num, m; + cout << "Enter the size of the array = "; + cin >> num; + int arr[num]; + cout << "Enter elements = "; + for (int i = 0; i < num; i++) { + cin >> arr[i]; + } + cout << "Enter the value to be generated = "; + cin >> m; + cout << "Minimum time = "; + cout << minTime(arr, num, m) << endl; + + return 0; + } + diff --git a/gfg-practice/Minimum_time_required_to_produce_m_items/brute.cpp b/gfg-practice/Minimum_time_required_to_produce_m_items/brute.cpp new file mode 100644 index 0000000..b3eecfe --- /dev/null +++ b/gfg-practice/Minimum_time_required_to_produce_m_items/brute.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +int minTime(int arr[], int n, int m) { + int t = 0; + while (1) { + int items = 0; + for (int i = 0; i < n; i++) items += (t / arr[i]); + if (items >= m) return t; + t++; + } +} +int main() { + int num, m; + cout << "Enter the size of the array = "; + cin >> num; + int arr[num]; + cout << "Enter elements = "; + for (int i = 0; i < num; i++) { + cin >> arr[i]; + } + cout << "Enter the value to be generated = "; + cin >> m; + cout << "Minimum time = "; + cout << minTime(arr, num, m) << endl; + + return 0; +} From 4faa93e5cf23d9bba2d3ee182edecdca62cbda2e Mon Sep 17 00:00:00 2001 From: SharmaRithik Date: Fri, 9 Oct 2020 23:44:17 +0530 Subject: [PATCH 04/23] fixures --- .../binary.cpp | 43 ------------------- .../brute.cpp | 27 ------------ 2 files changed, 70 deletions(-) delete mode 100644 gfg-practice/Minimum_time_required_to_produce_m_items/binary.cpp delete mode 100644 gfg-practice/Minimum_time_required_to_produce_m_items/brute.cpp diff --git a/gfg-practice/Minimum_time_required_to_produce_m_items/binary.cpp b/gfg-practice/Minimum_time_required_to_produce_m_items/binary.cpp deleted file mode 100644 index bd660e9..0000000 --- a/gfg-practice/Minimum_time_required_to_produce_m_items/binary.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include -using namespace std; -int findItems(int arr[], int n, int temp) { - int ans = 0; - for (int i = 0; i < n; i++) ans += (temp / arr[i]); - return ans; -} -int bs(int arr[], int n, int m, int high) { - int low = 1; - while (low < high) { - int mid = (low + high) >> 1; - int itm = findItems(arr, n, mid); - if (itm < m) - low = mid + 1; - else - high = mid; - } - - return high; -} -int minTime(int arr[], int n, int m) { - int maxval = INT_MIN; - for (int i = 0; i < n; i++) maxval = max(maxval, arr[i]); - return bs(arr, n, m, maxval * m); -} -int main() { - int main() { - int num, m; - cout << "Enter the size of the array = "; - cin >> num; - int arr[num]; - cout << "Enter elements = "; - for (int i = 0; i < num; i++) { - cin >> arr[i]; - } - cout << "Enter the value to be generated = "; - cin >> m; - cout << "Minimum time = "; - cout << minTime(arr, num, m) << endl; - - return 0; - } - diff --git a/gfg-practice/Minimum_time_required_to_produce_m_items/brute.cpp b/gfg-practice/Minimum_time_required_to_produce_m_items/brute.cpp deleted file mode 100644 index b3eecfe..0000000 --- a/gfg-practice/Minimum_time_required_to_produce_m_items/brute.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include -using namespace std; -int minTime(int arr[], int n, int m) { - int t = 0; - while (1) { - int items = 0; - for (int i = 0; i < n; i++) items += (t / arr[i]); - if (items >= m) return t; - t++; - } -} -int main() { - int num, m; - cout << "Enter the size of the array = "; - cin >> num; - int arr[num]; - cout << "Enter elements = "; - for (int i = 0; i < num; i++) { - cin >> arr[i]; - } - cout << "Enter the value to be generated = "; - cin >> m; - cout << "Minimum time = "; - cout << minTime(arr, num, m) << endl; - - return 0; -} From 3ce88d0a223e21b89bbf26c28bdb6c8d7f3e0db7 Mon Sep 17 00:00:00 2001 From: rithik sharma <40556303+SharmaRithik@users.noreply.github.com> Date: Fri, 9 Oct 2020 23:46:02 +0530 Subject: [PATCH 05/23] Minimum time required to produce m items A implementation of finding minimum time required to produce m items using brute force and binary search in cpp --- ...ime-required-to-produce-m-items-binary.cpp | 41 +++++++++++++++++++ ...time-required-to-produce-m-items-brute.cpp | 28 +++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 Minimum-time-required-to-produce-m-items-binary.cpp create mode 100644 Minimum-time-required-to-produce-m-items-brute.cpp diff --git a/Minimum-time-required-to-produce-m-items-binary.cpp b/Minimum-time-required-to-produce-m-items-binary.cpp new file mode 100644 index 0000000..a521ba9 --- /dev/null +++ b/Minimum-time-required-to-produce-m-items-binary.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; +int findItems(int arr[], int n, int temp) { + int ans = 0; + for (int i = 0; i < n; i++) ans += (temp / arr[i]); + return ans; +} +int bs(int arr[], int n, int m, int high) { + int low = 1; + while (low < high) { + int mid = (low + high) >> 1; + int itm = findItems(arr, n, mid); + if (itm < m) + low = mid + 1; + else + high = mid; + } + + return high; +} +int minTime(int arr[], int n, int m) { + int maxval = INT_MIN; + for (int i = 0; i < n; i++) maxval = max(maxval, arr[i]); + return bs(arr, n, m, maxval * m); +} +int main() { + int num, m; + cout << "Enter the size of the array = "; + cin >> num; + int arr[num]; + cout << "Enter elements = "; + for (int i = 0; i < num; i++) { + cin >> arr[i]; + } + cout << "Enter the value to be generated = "; + cin >> m; + cout << "Minimum time = "; + cout << minTime(arr, num, m) << endl; + + return 0; +} diff --git a/Minimum-time-required-to-produce-m-items-brute.cpp b/Minimum-time-required-to-produce-m-items-brute.cpp new file mode 100644 index 0000000..cd9bcf8 --- /dev/null +++ b/Minimum-time-required-to-produce-m-items-brute.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; +int minTime(int arr[], int n, int m) { + int t = 0; + while (1) { + int items = 0; + for (int i = 0; i < n; i++) items += (t / arr[i]); + if (items >= m) return t; + t++; + } +} +int main() { + int num, m; + cout << "Enter the size of the array = "; + cin >> num; + int arr[num]; + cout << "Enter elements = "; + for (int i = 0; i < num; i++) { + cin >> arr[i]; + } + cout << "Enter the value to be generated = "; + cin >> m; + cout << "Minimum time = "; + cout << minTime(arr, num, m) << endl; + + return 0; +} + From 11dd3faed2cc559ea380452a959cf7bd8a6de2a2 Mon Sep 17 00:00:00 2001 From: rithik sharma <40556303+SharmaRithik@users.noreply.github.com> Date: Sat, 10 Oct 2020 00:00:24 +0530 Subject: [PATCH 06/23] Floor in a Sorted Array This file has implementation of finding the floor of a sorted array, the file contains linear and binary implementations in cpp. --- Floor-in-a-Sorted-Array-Binary.cpp | 32 ++++++++++++++++++++++++++++++ Floor-in-a-Sorted-Array-Linear.cpp | 31 +++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 Floor-in-a-Sorted-Array-Binary.cpp create mode 100644 Floor-in-a-Sorted-Array-Linear.cpp diff --git a/Floor-in-a-Sorted-Array-Binary.cpp b/Floor-in-a-Sorted-Array-Binary.cpp new file mode 100644 index 0000000..26524f8 --- /dev/null +++ b/Floor-in-a-Sorted-Array-Binary.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; +int floorSearch(int arr[], int low, int high, int x) { + if (low > high) return -1; + if (x >= arr[high]) return high; + int mid = (low + high) / 2; + if (arr[mid] == x) return mid; + if (mid > 0 && arr[mid - 1] <= x && x < arr[mid]) return mid - 1; + if (x < arr[mid]) return floorSearch(arr, low, mid - 1, x); + return floorSearch(arr, mid + 1, high, x); +} +int main() { + int num, m; + printf("Enter the size of the array = "); + cin >> num; + int arr[num]; + printf("Enter elements = "); + for (int i = 0; i < num; i++) { + cin >> arr[i]; + } + printf("Enter the value to be generated = "); + cin >> m; + printf("Minimum time = "); + int index = floorSearch(arr, 0, num - 1, m); + if (index == -1) + printf("Floor of %d doesn't exist in array ", m); + else + printf("Floor of %d is %d", m, arr[index]); + return 0; +} + diff --git a/Floor-in-a-Sorted-Array-Linear.cpp b/Floor-in-a-Sorted-Array-Linear.cpp new file mode 100644 index 0000000..e6655d4 --- /dev/null +++ b/Floor-in-a-Sorted-Array-Linear.cpp @@ -0,0 +1,31 @@ +#include +#include +using namespace std; +int floorSearch(int arr[], int n, int x) { + if (x >= arr[n - 1]) return n - 1; + if (x < arr[0]) return -1; + for (int i = 1; i < n; i++) + if (arr[i] > x) return (i - 1); + + return -1; +} +int main() { + int num, m; + printf("Enter the size of the array = "); + cin >> num; + int arr[num]; + printf("Enter elements = "); + for (int i = 0; i < num; i++) { + cin >> arr[i]; + } + printf("Enter the value to be generated = "); + cin>>m; + printf("Minimum time = "); + int index = floorSearch(arr, num - 1, m); + if (index == -1) + printf("Floor of %d doesn't exist in array ", m); + else + printf("Floor of %d is %d", m, arr[index]); + return 0; +} + From cb018139cc0c83ae4aa936156279a2f10053819b Mon Sep 17 00:00:00 2001 From: Wahidur Zaman <42376226+neshadsfz@users.noreply.github.com> Date: Sat, 10 Oct 2020 13:35:17 +0600 Subject: [PATCH 07/23] Add files via upload --- main.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 main.c diff --git a/main.c b/main.c new file mode 100644 index 0000000..99b0deb --- /dev/null +++ b/main.c @@ -0,0 +1,25 @@ +// +// main.c +// 2D sum +// +// Created by APPLE on 10/4/18. +// Copyright © 2018 APPLE. All rights reserved. +// + +#include + +int main() +{ + int a[2][2],i,j,sum=0; + printf("enter the elmnt"); + for(i=0;i<2;i++) + { + for(j=0;j<2;j++) + + { + scanf("%d",&a[i][j]); + sum=sum+a[i][j]; + } + printf("%d",sum); + } +} From 2a367bfd0aeb83971f46ab593cbeb248e0fc810e Mon Sep 17 00:00:00 2001 From: Wahidur Zaman <42376226+neshadsfz@users.noreply.github.com> Date: Sat, 10 Oct 2020 13:40:34 +0600 Subject: [PATCH 08/23] Array Array Structure --- Array structure.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Array structure.c diff --git a/Array structure.c b/Array structure.c new file mode 100644 index 0000000..b91216d --- /dev/null +++ b/Array structure.c @@ -0,0 +1,25 @@ +// +// main.c +// Array structure +// +// Created by APPLE on 11/13/18. +// Copyright © 2018 APPLE. All rights reserved. +// + +#include + +struct friend +{ + int phoneno; + char name[10]; + int hno; + +}s[5]; +int main() +{ + int i; + for(i=0;i<5;i++) + scanf("%s%d%d",s[i].name,&s[i].phoneno,&s[i].hno); + for(i=0;i<5;i++) + printf("%s%d%d\n",s[i].name,&s[i].phoneno,&s[i].hno); +} From db2892931daf43490f6f40ede6ad0e9ff84f76dc Mon Sep 17 00:00:00 2001 From: Wahidur Zaman <42376226+neshadsfz@users.noreply.github.com> Date: Sat, 10 Oct 2020 13:43:34 +0600 Subject: [PATCH 09/23] calculate the interest --- calculate the interest.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 calculate the interest.c diff --git a/calculate the interest.c b/calculate the interest.c new file mode 100644 index 0000000..ac84d21 --- /dev/null +++ b/calculate the interest.c @@ -0,0 +1,29 @@ +// +// main.c +// Calculate The Interst +// +// Created by APPLE on 23/08/18. +// Copyright © 2018 APPLE. All rights reserved. +// + +#include + +int main() +{ + float principle, time, rate, SI; + + printf("Enter principle amount: "); + scanf("%f", &principle); + + printf("Enter time: "); + scanf("%f", &time); + + printf("Enter rate: "); + scanf("%f", &rate); + + SI = (principle * time * rate) / 100; + + printf("Simple Interest = %f", SI); + + return 0; +} From 9d6327715242e737551c84c53e0f43740c2da986 Mon Sep 17 00:00:00 2001 From: Wahidur Zaman <42376226+neshadsfz@users.noreply.github.com> Date: Sat, 10 Oct 2020 13:46:46 +0600 Subject: [PATCH 10/23] Circle --- Circle.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Circle.c diff --git a/Circle.c b/Circle.c new file mode 100644 index 0000000..df68fec --- /dev/null +++ b/Circle.c @@ -0,0 +1,22 @@ +// +// main.c +// CIRCLE +// +// Created by APPLE on 23/08/18. +// Copyright © 2018 APPLE. All rights reserved. +// + +#include + +int main() { + float radius, area; + + printf("\nEnter the radius of Circle : "); + scanf("%f", &radius); + + area = 3.14 * radius * radius; + printf("\nArea of Circle : %f", area); + + return (0); +} + From 7734fbd227f6d99928b5efc6ccc952d7eced1a16 Mon Sep 17 00:00:00 2001 From: Wahidur Zaman <42376226+neshadsfz@users.noreply.github.com> Date: Sat, 10 Oct 2020 13:52:36 +0600 Subject: [PATCH 11/23] Cpy to dma --- Cpy to dma.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Cpy to dma.c diff --git a/Cpy to dma.c b/Cpy to dma.c new file mode 100644 index 0000000..281805d --- /dev/null +++ b/Cpy to dma.c @@ -0,0 +1,28 @@ +// +// main.c +// CPY DMA +// +// Created by APPLE on 11/1/18. +// Copyright © 2018 APPLE. All rights reserved. +// + +#include +#include +#include + +int main() +{ + char s1[20]; + char s2[20]; + int result; + printf("\nEnter the string"); + gets(s1); + strcpy(s2,s1); + strrev(s2); + result=strcmp(s1,s2); + if(result==0) + printf("\nIt is a palindrome string"); + else + printf("\n It is not a palindrome string"); + +} From c09a87741db4574eb4812f862fa578732c6ac3fc Mon Sep 17 00:00:00 2001 From: wneshad <72607607+wneshad@users.noreply.github.com> Date: Sat, 10 Oct 2020 14:04:59 +0600 Subject: [PATCH 12/23] Reverse array --- Reverse array.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Reverse array.c diff --git a/Reverse array.c b/Reverse array.c new file mode 100644 index 0000000..9d78aa5 --- /dev/null +++ b/Reverse array.c @@ -0,0 +1,36 @@ +// +// main.c +// Reverse Array +// +// Created by APPLE on 9/27/18. +// Copyright © 2018 APPLE. All rights reserved. +// + +#include +int main() +{ + int n, c, d, a[100], b[100]; + + printf("Enter the number of elements in array\n"); + scanf("%d", &n); + + printf("Enter the array elements\n"); + + for (c = 0; c < n ; c++) + scanf("%d", &a[c]); + + + for (c = n - 1, d = 0; c >= 0; c--, d++) + b[d] = a[c]; + + + for (c = 0; c < n; c++) + a[c] = b[c]; + + printf("Reverse array is\n"); + + for (c = 0; c < n; c++) + printf("%d\n", a[c]); + + return 0; +} From e46b4f1b6e061451cf081a0d367a58947562d14c Mon Sep 17 00:00:00 2001 From: mehuldev <64658167+mehuldev@users.noreply.github.com> Date: Thu, 15 Oct 2020 22:30:49 +0530 Subject: [PATCH 13/23] Add files via upload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Given a set of n segments {[a0 , b0 ], [a1 , b1 ], . . . , [an−1 , bn−1 ]} with integer coordinates on a line, find the minimum number m of points such that each segment contains at least one point. That is, find a set of integers X of the minimum size such that for any segment [ai , bi ] there is a point x ∈ X such that ai ≤ x ≤ bi . --- covering_segments.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 covering_segments.py diff --git a/covering_segments.py b/covering_segments.py new file mode 100644 index 0000000..92a9489 --- /dev/null +++ b/covering_segments.py @@ -0,0 +1,33 @@ +# Uses python3 +import sys +from collections import namedtuple +def optimal_points(n,segments): + points = [] + i = 0 + flag = [True] * n + while i < n-1: + if flag[i]: + points.append(segments[i][1]) + j = i+1 + while j < n: + if flag[j]: + if segments[i][1] in range(segments[j][0], segments[j][1] + 1): + flag[j] = False + j += 1 + flag[i] = False + i +=1 + if flag[n-1]: + points.append(segments[n-1][0]) + return points + + +if __name__ == '__main__': + n=int(input()) + segments=[] + for i in range(n): + segments.append(list(map(int,input().split()))) + segments.sort(key=lambda x:x[1]) + #print(*segments) + points = optimal_points(n,segments) + print(len(points)) + print(*points) From 25dd7aff94b855642c37a042a35a13f1bd07abc2 Mon Sep 17 00:00:00 2001 From: Prateek Khandelwal <43922247+pkkh2000@users.noreply.github.com> Date: Sun, 18 Oct 2020 13:30:46 +0530 Subject: [PATCH 14/23] Create Circular_Queue.cpp --- Circular_Queue.cpp | 134 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 Circular_Queue.cpp diff --git a/Circular_Queue.cpp b/Circular_Queue.cpp new file mode 100644 index 0000000..97ea8fe --- /dev/null +++ b/Circular_Queue.cpp @@ -0,0 +1,134 @@ +// C or C++ program for insertion and +// deletion in Circular Queue +#include +using namespace std; + +struct Queue +{ + // Initialize front and rear + int rear, front; + + // Circular Queue + int size; + int *arr; + + Queue(int s) + { + front = rear = -1; + size = s; + arr = new int[s]; + } + + void enQueue(int value); + int deQueue(); + void displayQueue(); +}; + + +/* Function to create Circular queue */ +void Queue::enQueue(int value) +{ + if ((front == 0 && rear == size-1) || + (rear == (front-1)%(size-1))) + { + printf("\nQueue is Full"); + return; + } + + else if (front == -1) /* Insert First Element */ + { + front = rear = 0; + arr[rear] = value; + } + + else if (rear == size-1 && front != 0) + { + rear = 0; + arr[rear] = value; + } + + else + { + rear++; + arr[rear] = value; + } +} + +// Function to delete element from Circular Queue +int Queue::deQueue() +{ + if (front == -1) + { + printf("\nQueue is Empty"); + return INT_MIN; + } + + int data = arr[front]; + arr[front] = -1; + if (front == rear) + { + front = -1; + rear = -1; + } + else if (front == size-1) + front = 0; + else + front++; + + return data; +} + +// Function displaying the elements +// of Circular Queue +void Queue::displayQueue() +{ + if (front == -1) + { + printf("\nQueue is Empty"); + return; + } + printf("\nElements in Circular Queue are: "); + if (rear >= front) + { + for (int i = front; i <= rear; i++) + printf("%d ",arr[i]); + } + else + { + for (int i = front; i < size; i++) + printf("%d ", arr[i]); + + for (int i = 0; i <= rear; i++) + printf("%d ", arr[i]); + } +} + +/* Driver of the program */ +int main() +{ + Queue q(5); + + // Inserting elements in Circular Queue + q.enQueue(14); + q.enQueue(22); + q.enQueue(13); + q.enQueue(-6); + + // Display elements present in Circular Queue + q.displayQueue(); + + // Deleting elements from Circular Queue + printf("\nDeleted value = %d", q.deQueue()); + printf("\nDeleted value = %d", q.deQueue()); + + q.displayQueue(); + + q.enQueue(9); + q.enQueue(20); + q.enQueue(5); + + q.displayQueue(); + + q.enQueue(20); + return 0; +} From a8e4a2d68dd9bd03bdd98b7335ae60122ba1da1e Mon Sep 17 00:00:00 2001 From: Prateek Khandelwal <43922247+pkkh2000@users.noreply.github.com> Date: Sun, 18 Oct 2020 13:50:47 +0530 Subject: [PATCH 15/23] Create Binary_Search.cpp Maximize the number of days for which P chocolates can be distributed consecutively to N people Given an integer, P denoting the number of chocolates and an array a[] where ai denotes the type of ith chocolate. There are N people who want to eat chocolate every day. Find the maximum number of consecutive days for which N people can eat chocolates considering the following conditions: Each of the N people must eat exactly one chocolate on a particular day. A single person can eat chocolate of the same type only for all days. --- Binary_Search.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 Binary_Search.cpp diff --git a/Binary_Search.cpp b/Binary_Search.cpp new file mode 100644 index 0000000..8d060ef --- /dev/null +++ b/Binary_Search.cpp @@ -0,0 +1,88 @@ +// C++ program to implement +// the above approach + +#include +using namespace std; + +// Stores the frequency of +// each type of chocolate +map mp; + +int N, P; + +// Function to check if chocolates +// can be eaten for 'mid' no. of days +bool helper(int mid) +{ + + int cnt = 0; + for (auto i : mp) { + int temp = i.second; + + while (temp >= mid) { + temp -= mid; + cnt++; + } + } + + // If cnt exceeds N, + // return true + return cnt >= N; +} + +// Function to find the maximum +// number of days for which +// chocolates can be eaten +int findMaximumDays(int arr[]) +{ + + // Store the frequency + // of each type of chocolate + for (int i = 0; i < P; i++) { + mp[arr[i]]++; + } + + // Initialize start and end + // with 0 and P respectively + int start = 0, end = P, ans = 0; + while (start <= end) { + + // Calculate mid + int mid = start + + ((end - start) / 2); + + // Check if chocolates can be + // distributed for mid days + if (mid != 0 and helper(mid)) { + + ans = mid; + + // Check if chocolates can + // be distributed for more + // than mid consecutive days + start = mid + 1; + } + else if (mid == 0) { + start = mid + 1; + } + else { + end = mid - 1; + } + } + + return ans; +} + +// Driver code +int main() +{ + + N = 3, P = 10; + int arr[] = { 1, 2, 2, 1, 1, + 3, 3, 3, 2, 4 }; + + // Function call + cout << findMaximumDays(arr); + + return 0; +} From 19f19945f8878b9ae4bce111e00790f2ca386d37 Mon Sep 17 00:00:00 2001 From: j2704 <58335336+j2704@users.noreply.github.com> Date: Thu, 29 Oct 2020 21:03:20 +0530 Subject: [PATCH 16/23] Create Naive_Pattern_Searching.cpp --- Naive_Pattern_Searching.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Naive_Pattern_Searching.cpp diff --git a/Naive_Pattern_Searching.cpp b/Naive_Pattern_Searching.cpp new file mode 100644 index 0000000..6093f08 --- /dev/null +++ b/Naive_Pattern_Searching.cpp @@ -0,0 +1,36 @@ +// C++ program for Naive Pattern +// Searching algorithm +#include +using namespace std; + +void search(char* pat, char* txt) +{ + int M = strlen(pat); + int N = strlen(txt); + + /* A loop to slide pat[] one by one */ + for (int i = 0; i <= N - M; i++) { + int j; + + /* For current index i, check for pattern match */ + for (j = 0; j < M; j++) + if (txt[i + j] != pat[j]) + break; + + if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1] + cout << "Pattern found at index " + << i << endl; + } +} + +// Driver Code +int main() +{ + char txt[] = "AABAACAADAABAAABAA"; + char pat[] = "AABA"; + search(pat, txt); + return 0; +} + +// This code is contributed +// by Akanksha Rai From 6519793b88b8bf5dbd27024abbc9e8dfc73ead7b Mon Sep 17 00:00:00 2001 From: j2704 <58335336+j2704@users.noreply.github.com> Date: Thu, 29 Oct 2020 21:08:06 +0530 Subject: [PATCH 17/23] Create normal.cpp --- normal.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 normal.cpp diff --git a/normal.cpp b/normal.cpp new file mode 100644 index 0000000..46f8a19 --- /dev/null +++ b/normal.cpp @@ -0,0 +1,16 @@ +#include + +int main() +{ + struct student + { + int roll_no; + char name[30]; + int age; + int marks; + }; + struct student p1 = {1,"Brown",14,78}; + cout << p1.roll_no << " " << p1.name << " " << p1.age << " " << p1.marks << "\n"; + return 0; +} + From 64984f448a62467ad4ac3fbfaf42737a488b8e50 Mon Sep 17 00:00:00 2001 From: Vinayak Saxena <41303186+vinayaksaxena1507@users.noreply.github.com> Date: Sun, 10 Oct 2021 15:19:24 +0530 Subject: [PATCH 18/23] Create digit_removal.cpp --- digit_removal.cpp | 120 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 digit_removal.cpp diff --git a/digit_removal.cpp b/digit_removal.cpp new file mode 100644 index 0000000..dfb24e5 --- /dev/null +++ b/digit_removal.cpp @@ -0,0 +1,120 @@ +#include +using namespace std; + +#define tc ll t sc cin >> t sc while (t--) +#define ff first +#define sc ; +#define ss second +#define pb push_back +#define pp pop_back +#define mp make_pair +#define ll long long +#define Radhe ios::sync_with_stdio(false); +#define Krishna cin.tie(NULL); + +int main() +{ + tc + { + ll n; + ll d; + + cin >> n; + cin >> d; + + string str = to_string(n); + ll len = str.length(); + + if (d == 0) + { + int ind2=len; + + for (int i = 0; i < len; i++) + { + if (str[i] == '0') + { + str[i] = '1'; + ind2=i; + break; + } + } + + for (int j = ind2 + 1; j < len; j++) + { + str[j] = '1'; + } + } + else if (d == 9) + { + if (str[0] == '9') + { + for (int i = 0; i < len; i++) + { + str[i] = '0'; + } + str = "1" + str; + } + else + { + int ind=len; + for (int i = 0; i < len; i++) + { + if (str[i] == '9') + { + + for(int k=i-1; k >= 0; k--) + { + if(str[k] <= '7') + { + str[k]++; + ind=k; + goto cvv; + } + } + + for (int i = 0; i < len; i++) + { + str[i] = '0'; + } + str = "1" + str; + + goto fvv; + } + } + + + + + cvv:; + + for (int j = ind+1; j < len; j++) + { + str[j] = '0'; + } + + fvv:; + } + } + else + { + int i = 0; + for (i = 0; i < len; i++) + { + if ((str[i] - 48) == d) + { + str[i]=str[i]+1; + break; + } + } + + for (int j = i + 1; j < len; j++) + { + str[j] = '0'; + } + } + + ll nn = stoll(str); + + cout << nn - n << "\n"; + } +} From 5e79ac40c205739c1e4f395211a9d799e9705867 Mon Sep 17 00:00:00 2001 From: rithik sharma <40556303+SharmaRithik@users.noreply.github.com> Date: Mon, 11 Oct 2021 08:52:04 +0530 Subject: [PATCH 19/23] Create int_representation_llvmir C++ Code: int main() { int a = 5; } --- int_representation_llvmir | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 int_representation_llvmir diff --git a/int_representation_llvmir b/int_representation_llvmir new file mode 100644 index 0000000..6699c3c --- /dev/null +++ b/int_representation_llvmir @@ -0,0 +1,12 @@ + +define dso_local i32 @main() #0 !dbg !7 { + %1 = alloca i32, align 4 + call void @llvm.dbg.declare(metadata i32* %1, metadata !12, metadata !DIExpression()), !dbg !13 + store i32 5, i32* %1, align 4, !dbg !13 + ret i32 0, !dbg !14 +} + +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +attributes #0 = { noinline norecurse nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone speculatable willreturn } From 1f1571fb5a9cda0546a207fd73155a7d91f6d3be Mon Sep 17 00:00:00 2001 From: rithik sharma <40556303+SharmaRithik@users.noreply.github.com> Date: Mon, 11 Oct 2021 08:56:12 +0530 Subject: [PATCH 20/23] Create C++_add_in_llvmir C++ code: int main() { int a = 5; int b = 4; int c = a + b; } --- C++_add_in_llvmir | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 C++_add_in_llvmir diff --git a/C++_add_in_llvmir b/C++_add_in_llvmir new file mode 100644 index 0000000..40a1070 --- /dev/null +++ b/C++_add_in_llvmir @@ -0,0 +1,21 @@ + +define dso_local i32 @main() #0 !dbg !7 { + %1 = alloca i32, align 4 + %2 = alloca i32, align 4 + %3 = alloca i32, align 4 + call void @llvm.dbg.declare(metadata i32* %1, metadata !12, metadata !DIExpression()), !dbg !13 + store i32 5, i32* %1, align 4, !dbg !13 + call void @llvm.dbg.declare(metadata i32* %2, metadata !14, metadata !DIExpression()), !dbg !15 + store i32 4, i32* %2, align 4, !dbg !15 + call void @llvm.dbg.declare(metadata i32* %3, metadata !16, metadata !DIExpression()), !dbg !17 + %4 = load i32, i32* %1, align 4, !dbg !18 + %5 = load i32, i32* %2, align 4, !dbg !19 + %6 = add nsw i32 %4, %5, !dbg !20 + store i32 %6, i32* %3, align 4, !dbg !17 + ret i32 0, !dbg !21 +} + +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +attributes #0 = { noinline norecurse nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone speculatable willreturn } From 9f4e74fd427e53557165a0d246fa672e5019cd7b Mon Sep 17 00:00:00 2001 From: rithik sharma <40556303+SharmaRithik@users.noreply.github.com> Date: Mon, 11 Oct 2021 09:10:56 +0530 Subject: [PATCH 21/23] Create while_llvmir --- while_llvmir | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 while_llvmir diff --git a/while_llvmir b/while_llvmir new file mode 100644 index 0000000..4795c4c --- /dev/null +++ b/while_llvmir @@ -0,0 +1,35 @@ + +define dso_local i32 @main() #0 !dbg !7 { + %1 = alloca i32, align 4 + %2 = alloca i32, align 4 + %3 = alloca i32, align 4 + %4 = alloca i32, align 4 + store i32 0, i32* %1, align 4 + call void @llvm.dbg.declare(metadata i32* %2, metadata !12, metadata !DIExpression()), !dbg !13 + store i32 5, i32* %2, align 4, !dbg !13 + call void @llvm.dbg.declare(metadata i32* %3, metadata !14, metadata !DIExpression()), !dbg !15 + store i32 4, i32* %3, align 4, !dbg !15 + call void @llvm.dbg.declare(metadata i32* %4, metadata !16, metadata !DIExpression()), !dbg !17 + %5 = load i32, i32* %2, align 4, !dbg !18 + %6 = load i32, i32* %3, align 4, !dbg !19 + %7 = add nsw i32 %5, %6, !dbg !20 + store i32 %7, i32* %4, align 4, !dbg !17 + br label %8, !dbg !21 + +8: ; preds = %11, %0 + %9 = load i32, i32* %2, align 4, !dbg !22 + %10 = icmp sgt i32 %9, 0, !dbg !23 + br i1 %10, label %11, label %12, !dbg !21 + +11: ; preds = %8 + br label %8, !dbg !21, !llvm.loop !24 + +12: ; preds = %8 + %13 = load i32, i32* %1, align 4, !dbg !26 + ret i32 %13, !dbg !26 +} + +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +attributes #0 = { noinline norecurse nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone speculatable willreturn } From bdb57f35bdd5207d4607881c4d2fa4f8a6714ecd Mon Sep 17 00:00:00 2001 From: infinixaco <56000525+infinixaco@users.noreply.github.com> Date: Mon, 11 Oct 2021 09:33:48 +0530 Subject: [PATCH 22/23] Create Codeforces 1580A Portal --- Codeforces 1580A Portal | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Codeforces 1580A Portal diff --git a/Codeforces 1580A Portal b/Codeforces 1580A Portal new file mode 100644 index 0000000..1893f92 --- /dev/null +++ b/Codeforces 1580A Portal @@ -0,0 +1,26 @@ + #include + using namespace std; + + const int N=400+5; + char mp[N][N]; + int n,m,ans,f[N],s[N][N]; + void solve(){ + cin>>n>>m,ans=1e9; + for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) + cin>>mp[i][j],s[i][j]=s[i-1][j]+mp[i][j]-'0'; + for(int i=1;i<=n;i++) + for(int j=i+4;j<=n;j++){ + int pre=1e9,cur=0; + for(int k=1;k<=m;k++){ + if(k>3)pre=min(pre,f[k-3]); + int nd=s[j-1][k]-s[i][k]; + ans=min(ans,pre+cur+(f[k]=j-i-1-nd)); + cur+=nd+(mp[i][k]=='0')+(mp[j][k]=='0'),f[k]-=cur; + } + } cout<>T; + while(T--)solve(); + return 0; + } From 32706fdf6cc9033a967d887d2311ddfc9936a567 Mon Sep 17 00:00:00 2001 From: wneshad <72607607+wneshad@users.noreply.github.com> Date: Sat, 23 Oct 2021 14:26:06 +0600 Subject: [PATCH 23/23] Create Adding two numbers in Ruby --- Adding two numbers in Ruby | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Adding two numbers in Ruby diff --git a/Adding two numbers in Ruby b/Adding two numbers in Ruby new file mode 100644 index 0000000..a602e70 --- /dev/null +++ b/Adding two numbers in Ruby @@ -0,0 +1,16 @@ +=begin +Ruby program to add two numbers. +=end + +# input the numbers and converting +# them into integer +puts "Enter first value: " +num1=gets.chomp.to_i +puts "Enter second value: " +num2=gets.chomp.to_i + +# finding sum +sum=num1+num2 + +# printing the result +puts "The sum is #{sum}"