From 84adb1917cb419c79cd262fb0b99fd39fba5f067 Mon Sep 17 00:00:00 2001 From: thesuaveguy Date: Sat, 29 Oct 2022 14:42:22 +0530 Subject: [PATCH] added leetcode weekly contest 315 cpp solution --- Leetcode_weeklyContest315/2441.cpp | 40 +++++++++++++++++++ Leetcode_weeklyContest315/2442.cpp | 40 +++++++++++++++++++ Leetcode_weeklyContest315/2443.cpp | 41 +++++++++++++++++++ Leetcode_weeklyContest315/2444.cpp | 64 ++++++++++++++++++++++++++++++ 4 files changed, 185 insertions(+) create mode 100644 Leetcode_weeklyContest315/2441.cpp create mode 100644 Leetcode_weeklyContest315/2442.cpp create mode 100644 Leetcode_weeklyContest315/2443.cpp create mode 100644 Leetcode_weeklyContest315/2444.cpp diff --git a/Leetcode_weeklyContest315/2441.cpp b/Leetcode_weeklyContest315/2441.cpp new file mode 100644 index 00000000..12e9a4a8 --- /dev/null +++ b/Leetcode_weeklyContest315/2441.cpp @@ -0,0 +1,40 @@ +/*2441. Largest Positive Integer That Exists With Its Negative */ + +/*Given an integer array nums that does not contain any zeros, find the largest positive integer k such that -k also exists in the array. +Return the positive integer k. If there is no such integer, return -1.*/ + +// Test cases +/* +1.) +Input: nums = [-1,2,-3,3] +Output: 3 +2.) +Input: nums = [-1,10,6,7,-7,1] +Output: 7 +3.) +Input: nums = [-10,8,6,7,-2,-3] +Output: -1 +*/ + +class Solution +{ +public: + int findMaxK(vector &nums) + { + set s; + int ans = -1; + for (int i = 0; i < nums.size(); i++) + s.insert(nums[i]); + sort(nums.begin(), nums.end()); + for (int i = nums.size() - 1; i >= 0; i--) + { + if (s.find(-1 * nums[i]) != s.end()) + { + + ans = nums[i]; + break; + } + } + return ans; + } +}; \ No newline at end of file diff --git a/Leetcode_weeklyContest315/2442.cpp b/Leetcode_weeklyContest315/2442.cpp new file mode 100644 index 00000000..0cada172 --- /dev/null +++ b/Leetcode_weeklyContest315/2442.cpp @@ -0,0 +1,40 @@ +/*2442. Count Number of Distinct Integers After Reverse Operations*/ + +/*You are given an array nums consisting of positive integers. +You have to take each integer in the array, reverse its digits, and add it to the end of the array. You should apply this operation to the original integers in nums. +Return the number of distinct integers in the final array.*/ + +// Test cases +/* +1.) +Input: nums = [1,13,10,12,31] +Output: 6 +2.) +Input: nums = [2,2,2] +Output: 1 +*/ + +class Solution +{ +public: + int countDistinctIntegers(vector &nums) + { + set s; + for (int i = 0; i < nums.size(); i++) + s.insert(nums[i]); + int n = nums.size(); + for (int i = 0; i < n; i++) + { + int c = 0; + int d = nums[i]; + while (d != 0) + { + int l = d % 10; + c = c * 10 + l; + d /= 10; + } + s.insert(c); + } + return s.size(); + } +}; \ No newline at end of file diff --git a/Leetcode_weeklyContest315/2443.cpp b/Leetcode_weeklyContest315/2443.cpp new file mode 100644 index 00000000..41b3b55a --- /dev/null +++ b/Leetcode_weeklyContest315/2443.cpp @@ -0,0 +1,41 @@ +/*2443. Sum of Number and Its Reverse*/ + +/*Given a non-negative integer num, return true if num can be expressed as the sum of any non-negative integer and its reverse, or false otherwise. + */ + +// Test cases +/* +1.) +Input: num = 443 +Output: true +2.) +Input: num = 63 +Output: false +3.) +Input: num = 181 +Output: true +*/ +class Solution +{ +public: + int rev(int d) + { + int c = 0; + while (d != 0) + { + int l = d % 10; + c = c * 10 + l; + d /= 10; + } + return c; + } + bool sumOfNumberAndReverse(int num) + { + for (int i = 0; i <= num; i++) + { + if (i + rev(i) == num) + return true; + } + return false; + } +}; \ No newline at end of file diff --git a/Leetcode_weeklyContest315/2444.cpp b/Leetcode_weeklyContest315/2444.cpp new file mode 100644 index 00000000..ac428b5e --- /dev/null +++ b/Leetcode_weeklyContest315/2444.cpp @@ -0,0 +1,64 @@ +/*2444. Count Subarrays With Fixed Bounds*/ + +/*You are given an integer array nums and two integers minK and maxK. +A fixed-bound subarray of nums is a subarray that satisfies the following conditions: + *The minimum value in the subarray is equal to minK. + *The maximum value in the subarray is equal to maxK. + +Return the number of fixed-bound subarrays. + +A subarray is a contiguous part of an array.*/ + +// Test cases +/* +1.) +Input: nums = [1,3,5,2,7,5], minK = 1, maxK = 5 +Output: 2 +2.) +Input: nums = [1,1,1,1], minK = 1, maxK = 1 +Output: 10 +*/ + +class Solution +{ +public: + long long countSubarrays(vector &nums, int minK, int maxK) + { + vector newvec; + int size = nums.size(); + vector v2; + vector v1; + for (int i = 0; i < nums.size(); i++) + { + if (nums[i] == minK) + newvec.push_back(i); + } + newvec.push_back(size); + for (int i = 0; i < nums.size(); i++) + { + if (nums[i] == maxK) + v1.push_back(i); + } + v1.push_back(size); + for (int i = 0; i < nums.size(); i++) + { + if (nums[i] < minK || nums[i] > maxK) + v2.push_back(i); + } + v2.push_back(size); + long long int ans = 0; + for (int i = 0; i < size; i++) + { + int os2 = *lower_bound(newvec.begin(), newvec.end(), i); + int os3 = *lower_bound(v1.begin(), v1.end(), i); + int os1 = *lower_bound(v2.begin(), v2.end(), i); + int ann = max(os2, os3); + int temp = 0; + if (ann < os1) + temp = os1 - ann; + + ans += temp; + } + return ans; + } +}; \ No newline at end of file