From 846f3f3c79ca43d65e174c95301f242dd85c129b Mon Sep 17 00:00:00 2001 From: Arnabhit <113552756+Arnabhit@users.noreply.github.com> Date: Wed, 5 Oct 2022 21:34:22 +0530 Subject: [PATCH 1/4] Add files via upload --- Plus One.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Plus One.cpp diff --git a/Plus One.cpp b/Plus One.cpp new file mode 100644 index 0000000..726b439 --- /dev/null +++ b/Plus One.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + vector plusOne(vector& digits) { + + int n=digits.size(); + vectorkbc; + int c=1; + + for(int i=n-1; i>=0; i--) + { + + if(digits[i]==9 && c==1) + kbc.push_back(0); + + + else kbc.push_back(digits[i]+c), + c=0; + + } + + + if(c==1) + kbc.push_back(c); + + reverse(kbc.begin(), kbc.end()); + return kbc; + + } +}; From d7c365857167a34a998f4f4ed579580cd6114dc2 Mon Sep 17 00:00:00 2001 From: Arnabhit <113552756+Arnabhit@users.noreply.github.com> Date: Thu, 6 Oct 2022 00:12:29 +0530 Subject: [PATCH 2/4] Add files via upload --- README.md | 33 +++++++++++++---- ...roduct and Sum of Digits of an Integer.cpp | 36 +++++++++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 Subtract the Product and Sum of Digits of an Integer.cpp diff --git a/README.md b/README.md index 06b8eca..b09343f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,27 @@ -We are building a dataset of all Data-Structure and algorithm questions to be accessed later via APIs for our webapp backend. - -Please help us contribute. - -This dataset would be free to use for all. - +Given an integer number n, return the difference between the product of its digits and the sum of its digits. + + + +Example 1: + +Input: n = 234 +Output: 15 +Explanation: +Product of digits = 2 * 3 * 4 = 24 +Sum of digits = 2 + 3 + 4 = 9 +Result = 24 - 9 = 15 + +Example 2: + +Input: n = 4421 +Output: 21 +Explanation: +Product of digits = 4 * 4 * 2 * 1 = 32 +Sum of digits = 4 + 4 + 2 + 1 = 11 +Result = 32 - 11 = 21 + + + +Constraints: + + 1 <= n <= 10^5 diff --git a/Subtract the Product and Sum of Digits of an Integer.cpp b/Subtract the Product and Sum of Digits of an Integer.cpp new file mode 100644 index 0000000..dbabda4 --- /dev/null +++ b/Subtract the Product and Sum of Digits of an Integer.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + int subtractProductAndSum(int n) { + + int m,s=1,z,a,s1=0; + m=n; + a=n; + while(m!=0) + { + int k=m%10; + s=s*k; + m=m/10; + + } + while(a!=0) + { + z=a%10; + s1=s1+z; + a=a/10; + } + int res=s-s1; + return res; + + } + int main() + { + int n; + cin>>n; + int a= subtractProductAndSum(n); + cout< Date: Thu, 6 Oct 2022 00:27:27 +0530 Subject: [PATCH 3/4] Add files via upload --- README1.md | 29 +++++++++++++++++++++++++++++ Running Sum of 1d Array.cpp | 16 ++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 README1.md create mode 100644 Running Sum of 1d Array.cpp diff --git a/README1.md b/README1.md new file mode 100644 index 0000000..2c4445d --- /dev/null +++ b/README1.md @@ -0,0 +1,29 @@ +Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). + +Return the running sum of nums. + + + +Example 1: + +Input: nums = [1,2,3,4] +Output: [1,3,6,10] +Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. + +Example 2: + +Input: nums = [1,1,1,1,1] +Output: [1,2,3,4,5] +Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]. + +Example 3: + +Input: nums = [3,1,2,10,1] +Output: [3,4,6,16,17] + + + +Constraints: + + 1 <= nums.length <= 1000 + -10^6 <= nums[i] <= 10^6 diff --git a/Running Sum of 1d Array.cpp b/Running Sum of 1d Array.cpp new file mode 100644 index 0000000..03d3301 --- /dev/null +++ b/Running Sum of 1d Array.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + vector runningSum(vector& nums) { + + int s=0; + + for(int i=0;i Date: Thu, 6 Oct 2022 00:34:04 +0530 Subject: [PATCH 4/4] Add files via upload --- Kth Largest Element in an Array.cpp | 10 ++++++++++ README2.md | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 Kth Largest Element in an Array.cpp create mode 100644 README2.md diff --git a/Kth Largest Element in an Array.cpp b/Kth Largest Element in an Array.cpp new file mode 100644 index 0000000..043af1a --- /dev/null +++ b/Kth Largest Element in an Array.cpp @@ -0,0 +1,10 @@ +class Solution { +public: + int findKthLargest(vector& nums, int k) { + + int z=nums.size();int c=0,i=0; + sort(nums.begin(), nums.end(), greater()); + int a=nums[k-1]; + return a; + } + }; \ No newline at end of file diff --git a/README2.md b/README2.md new file mode 100644 index 0000000..1d2d1b1 --- /dev/null +++ b/README2.md @@ -0,0 +1,25 @@ +Given an integer array nums and an integer k, return the kth largest element in the array. + +Note that it is the kth largest element in the sorted order, not the kth distinct element. + +You must solve it in O(n) time complexity. + + + +Example 1: + +Input: nums = [3,2,1,5,6,4], k = 2 +Output: 5 + +Example 2: + +Input: nums = [3,2,3,1,2,4,5,5,6], k = 4 +Output: 4 + + + +Constraints: + + 1 <= k <= nums.length <= 105 + -104 <= nums[i] <= 104 +