From 4b143e95730f571437b696dc7b736a51d53f19f5 Mon Sep 17 00:00:00 2001 From: Rohan Roy Chowdhury Date: Mon, 18 Apr 2022 10:56:40 +0530 Subject: [PATCH] up --- .../codeStudio/.vscode/c_cpp_properties.json | 20 ++++ contests/codeStudio/c10/addEdges.cpp | 93 +++++++++++++++ contests/codeStudio/c10/countPeep.cpp | 110 ++++++++++++++++++ contests/codeStudio/c10/miniBill.cpp | 96 +++++++++++++++ .../leetCode/.vscode/c_cpp_properties.json | 20 ++++ contests/leetCode/bc72/cntEqDivAr.cpp | 58 +++++++++ contests/leetCode/bc72/find3consInt.cpp | 52 +++++++++ contests/leetCode/bc72/maxSplitPosEven.cpp | 81 +++++++++++++ contests/leetCode/wc280/maxAndSumAr.cpp | 71 +++++++++++ contests/leetCode/wc280/minOptArAlt.cpp | 106 +++++++++++++++++ contests/leetCode/wc280/opCntzro.cpp | 64 ++++++++++ contests/leetCode/wc280/remMinMagBeans.cpp | 70 +++++++++++ vmware/.vscode/settings.json | 1 + vmware/insertDelGetR.cpp | 38 ++++++ vmware/medianDataStream.cpp | 44 +++++++ vmware/revLL2.cpp | 58 +++++++++ vmware/sortList.cpp | 53 +++++++++ 17 files changed, 1035 insertions(+) create mode 100644 contests/codeStudio/.vscode/c_cpp_properties.json create mode 100644 contests/codeStudio/c10/addEdges.cpp create mode 100644 contests/codeStudio/c10/countPeep.cpp create mode 100644 contests/codeStudio/c10/miniBill.cpp create mode 100644 contests/leetCode/.vscode/c_cpp_properties.json create mode 100644 contests/leetCode/bc72/cntEqDivAr.cpp create mode 100644 contests/leetCode/bc72/find3consInt.cpp create mode 100644 contests/leetCode/bc72/maxSplitPosEven.cpp create mode 100644 contests/leetCode/wc280/maxAndSumAr.cpp create mode 100644 contests/leetCode/wc280/minOptArAlt.cpp create mode 100644 contests/leetCode/wc280/opCntzro.cpp create mode 100644 contests/leetCode/wc280/remMinMagBeans.cpp create mode 100644 vmware/.vscode/settings.json create mode 100644 vmware/insertDelGetR.cpp create mode 100644 vmware/medianDataStream.cpp create mode 100644 vmware/revLL2.cpp create mode 100644 vmware/sortList.cpp diff --git a/contests/codeStudio/.vscode/c_cpp_properties.json b/contests/codeStudio/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..d060767 --- /dev/null +++ b/contests/codeStudio/.vscode/c_cpp_properties.json @@ -0,0 +1,20 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "compilerPath": "C:/MinGW/bin/g++.exe", + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "windows-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/contests/codeStudio/c10/addEdges.cpp b/contests/codeStudio/c10/addEdges.cpp new file mode 100644 index 0000000..838b25e --- /dev/null +++ b/contests/codeStudio/c10/addEdges.cpp @@ -0,0 +1,93 @@ +/* +Add Edges +Difficulty: MEDIUM +Contributed By +Vishal R S +| +Level 2 +Avg. time to solve +15 min +Success Rate +85% +Problem Statement +A bipartite graph is a graph where each node is either coloured black or white and that for any edge, the endpoints of the edge are in different colours. +You are given an undirected tree of ‘N’ nodes. You can keep adding edges to it as long as the graph remains bipartite. What is the maximum number of edges you can add? +Note - If there already exists an edge between two nodes, you cannot add another. You also cannot add an edge from any node to itself. However, cycles are permitted. +For example: +If there is a tree of 4 nodes, with edges (1,2), (2,3) and (1,4), you can add an edge between nodes 3 and 4 and the graph still remains bipartite. +Input Format: +The first line contains 'T', denoting the number of test cases. +For each Test : +The first line contains a single integer ‘N’, representing the number of elements. +The next N-1 lines contain two integers ‘X’ and ‘Y’, indicating that there is an edge between node ‘X’ and ‘Y’. +Output Format: +For each test case, print one integer, the maximum number of edges you can add. +Note: +You are not required to print the expected output. It has already been taken care of. Just implement the function. +Constraints - +1 <= ‘T’ <= 10 +2 <= ‘N’ <= 10^5 +1 <= ‘X’,’Y’ <= N, X != Y +Note: It is guaranteed that the sum of N across all test cases will be at most 10^5. +Time Limit: 1 sec +Sample Input 1: +1 +5 +1 2 +1 3 +2 4 +2 5 +Sample Output 1 +2 +Explanation For Sample Input 1: +For test case 1: + We can add two edges (3,4) and (3,5) and the graph still remains bipartite. +Sample Input 2: +1 +3 +1 2 +1 3 +Sample Output 2: +0 + + +*/ + +#include +using namespace std; + +int dfs(int node, int color, int p, vector>&adj){//will count no. of blacks + int black= color;//initially 1 black + //for particular node + for(auto it: adj[node]){//traversing neigbours of node 1 + if(it!=p){//p is parent + black+=dfs(it, !color,node,adj);//oppo colo and new parent is node itself + } + } + return black; +} +long long maxEdges(int n, vector> edges) +{ + // Write your code here. + //any graph having odd length cycle can't be bipartite + + //creating a graph + vector>adj(n+1); + for(auto it:edges){//traversing edges vector + adj[it.first].push_back(it.second); + adj[it.second].push_back(it.first); + } + //color the graph into b and w + //assuming 1 to be black, 0 to be white + //1st node is black and parent in -1 in func call + int b = dfs(1,1,-1,adj);//no of blk nodes + int w = n-b;//whites + //b*w - (n-1) + //3 b and 2 w so 3*2=6 {6-4}=2 + long long ans = 1LL * (b*w)-(n-1); + return ans; + +} +int main() { + return 0; +} \ No newline at end of file diff --git a/contests/codeStudio/c10/countPeep.cpp b/contests/codeStudio/c10/countPeep.cpp new file mode 100644 index 0000000..a3af9bc --- /dev/null +++ b/contests/codeStudio/c10/countPeep.cpp @@ -0,0 +1,110 @@ +/* +Count The People +Difficulty: MEDIUM +Contributed By +Nihal Srivastava +| +Level 5 +Avg. time to solve +17 min +Success Rate +82% +Problem Statement +You and your friend recently watched a Mr Beast’s video; now you want to give away food just like him. There are infinite people arranged in a 2-D grid of an infinite length and breadth, each cell having one person. You and your friend are starting at the same cell of the grid. You also have a string ‘S’ of length ‘N’ consisting of the instructions you and your friend have to follow. You will follow the first instruction; your friend will follow the second instruction; you will follow the third instruction, and so on. +The instructions are as follow: +1) ‘U’ means to move one cell UP. +2) ‘D’ means to move one cell DOWN. +3) ‘L’ means to move one cell LEFT. +4) ‘R’ means to move one cell RIGHT. +Once you or your friend reach a cell (including the starting position), the person present in the cell will receive the food. Now you wonder how many distinct people got the food. +For example: +If the instruction is 'RU', you move one cell right and then you friend moves one cell up. Hence, in total 3 cells are visited. +Input Format- +First-line contains ‘T’, denoting the number of Test cases. +For each Test case: +The first line contains an integer, ‘N’, denoting the length of the string ‘S’. +The following contains the string ‘S’ consisting only of the character {‘U’, ‘D’, ‘R’, ‘L’}. +Output Format- +For each test case, you have to print an integer denoting the number of distinct people that got the food. +Note : +You don’t need to print anything. It has already been taken care of. Just implement the given function. +Constraints - +1 <= ‘T’ <= 10 +1 <= ‘N’ <= 10^5 + +Note- Sum of ‘N’ over all test cases does not exceed 10^5. + +Time Limit: 1 sec +Sample Input-1 +2 +2 +UL +4 +ULUL +Sample Output-1 +3 +5 +Explanation For Sample Input 1: +For test case 1: + You and your friend gave food to the person at the starting position. Then you went up by one cell and gave food to the person in that cell. Your friend went left by one cell and gave food to the person in that cell. Hence a total of three distinct people got the food. +Sample Input -2 +2 +4 +ULRD +6 +DRDRDR +Sample Output -2 +5 +7 + +*/ + +#include +using namespace std; + +int count(int n, string s) +{ + // Write your code here. + int x1=0, y1=0;//my coordinates, x is for rows and y is for cols + int x2=0, y2=0;//friend's coordinates + unordered_setst;//pos is visited or not + st.insert(0*1e6+0);//at initial + for(int i=0; i +using namespace std; + +long long totalBill(int n, vector& a) +{ + // Write your code here. + + int sum =0; + int idx; + int maxi = INT_MIN; + for(int i=0; imaxi){ + maxi=a[i]; + idx=i; + } + } + a[idx]*=-1; + for(int i=0; i +using namespace std; + +class Solution { +public: + int countPairs(vector& nums, int k) { + int n = nums.size(); + int cnt = 0; + for(int i=0; i +using namespace std; + +class Solution { +public: + vector sumOfThree(long long n) { + vectorans; + + if(n%3==0){ + ans.emplace_back(n/3-1); + ans.emplace_back(n/3); + ans.emplace_back(n/3+1); + + } + + return ans; + } +}; +int main() { + return 0; +} \ No newline at end of file diff --git a/contests/leetCode/bc72/maxSplitPosEven.cpp b/contests/leetCode/bc72/maxSplitPosEven.cpp new file mode 100644 index 0000000..41846ab --- /dev/null +++ b/contests/leetCode/bc72/maxSplitPosEven.cpp @@ -0,0 +1,81 @@ +/* +2178. Maximum Split of Positive Even Integers +User Accepted:4998 +User Tried:6305 +Total Accepted:5152 +Total Submissions:11744 +Difficulty:Medium +You are given an integer finalSum. Split it into a sum of a maximum number of unique positive even integers. + +For example, given finalSum = 12, the following splits are valid (unique positive even integers summing up to finalSum): (2 + 10), (2 + 4 + 6), and (4 + 8). Among them, (2 + 4 + 6) contains the maximum number of integers. Note that finalSum cannot be split into (2 + 2 + 4 + 4) as all the numbers should be unique. +Return a list of integers that represent a valid split containing a maximum number of integers. If no valid split exists for finalSum, return an empty list. You may return the integers in any order. + + + +Example 1: + +Input: finalSum = 12 +Output: [2,4,6] +Explanation: The following are some valid splits: (2 + 10), (2 + 4 + 6), and (4 + 8). +(2 + 4 + 6) has the maximum number of integers, which is 3. Thus, we return [2,4,6]. +Note that [2,6,4], [6,2,4], etc. are also accepted. +Example 2: + +Input: finalSum = 7 +Output: [] +Explanation: There are no valid splits for the given finalSum. +Thus, we return an empty array. +Example 3: + +Input: finalSum = 28 +Output: [6,8,2,12] +Explanation: The following are some valid splits: (2 + 26), (6 + 8 + 2 + 12), and (4 + 24). +(6 + 8 + 2 + 12) has the maximum number of integers, which is 4. Thus, we return [6,8,2,12]. +Note that [10,2,4,12], [6,2,4,16], etc. are also accepted. + + +Constraints: + +1 <= finalSum <= 1010 + + +*/ + + +#include +using namespace std; + +class Solution { +public: + vector maximumEvenSplit(long long fs) { + vectorans; + long long curr = 2; + long long sum =0; + if(fs&1)return ans;//if fs is odd, we will have empty array + while(1){//infinite loop + if(sum+curr==fs){ + ans.emplace_back(curr); + break; + } + if(sum+curr= n. There are numSlots slots numbered from 1 to numSlots. + +You have to place all n integers into the slots such that each slot contains at most two numbers. The AND sum of a given placement is the sum of the bitwise AND of every number with its respective slot number. + +For example, the AND sum of placing the numbers [1, 3] into slot 1 and [4, 6] into slot 2 is equal to (1 AND 1) + (3 AND 1) + (4 AND 2) + (6 AND 2) = 1 + 1 + 0 + 2 = 4. +Return the maximum possible AND sum of nums given numSlots slots. + + + +Example 1: + +Input: nums = [1,2,3,4,5,6], numSlots = 3 +Output: 9 +Explanation: One possible placement is [1, 4] into slot 1, [2, 6] into slot 2, and [3, 5] into slot 3. +This gives the maximum AND sum of (1 AND 1) + (4 AND 1) + (2 AND 2) + (6 AND 2) + (3 AND 3) + (5 AND 3) = 1 + 0 + 2 + 2 + 3 + 1 = 9. +Example 2: + +Input: nums = [1,3,10,4,7,1], numSlots = 9 +Output: 24 +Explanation: One possible placement is [1, 1] into slot 1, [3] into slot 3, [4] into slot 4, [7] into slot 7, and [10] into slot 9. +This gives the maximum AND sum of (1 AND 1) + (1 AND 1) + (3 AND 3) + (4 AND 4) + (7 AND 7) + (10 AND 9) = 1 + 1 + 3 + 4 + 7 + 8 = 24. +Note that slots 2, 5, 6, and 8 are empty which is permitted. + + +Constraints: + +n == nums.length +1 <= numSlots <= 9 +1 <= n <= 2 * numSlots +1 <= nums[i] <= 15 +*/ + +#include +using namespace std; + +class Solution { +private: + int findMax(int idx, vector&slotW, vector&nums, int numSlots, map>,int>&dp){ + if(idx>=nums.size())return 0;//filled up all the digits + int maxi = INT_MIN; + if(dp.find({idx,slotW})!=dp.end())return dp[{idx,slotW}]; + //putting all arr[idx] into every slot from 1 to num slots + for(int i=1; i<=numSlots; ++i){ + //we can put if it is not full yet + if(slotW[i]<2) { + slotW[i]++;//weight of first slot will go up + int ans = (nums[idx]&i)+ findMax(idx+1, slotW, nums, numSlots,dp); + slotW[i]--; + maxi=max(maxi,ans); + } + } + return dp[{idx,slotW}]=maxi; + } + +public: + int maximumANDSum(vector& nums, int numSlots) { + vectorslots(numSlots+1,0); + map>,int>dp; + return findMax(0,slots,nums,numSlots,dp); + } +}; +int main() { + return 0; +} \ No newline at end of file diff --git a/contests/leetCode/wc280/minOptArAlt.cpp b/contests/leetCode/wc280/minOptArAlt.cpp new file mode 100644 index 0000000..fb4cf2b --- /dev/null +++ b/contests/leetCode/wc280/minOptArAlt.cpp @@ -0,0 +1,106 @@ +/* +2170. Minimum Operations to Make the Array Alternating +User Accepted:2819 +User Tried:7511 +Total Accepted:2884 +Total Submissions:19403 +Difficulty:Medium +You are given a 0-indexed array nums consisting of n positive integers. + +The array nums is called alternating if: + +nums[i - 2] == nums[i], where 2 <= i <= n - 1. +nums[i - 1] != nums[i], where 1 <= i <= n - 1. +In one operation, you can choose an index i and change nums[i] into any positive integer. + +Return the minimum number of operations required to make the array alternating. + + + +Example 1: + +Input: nums = [3,1,3,2,4,3] +Output: 3 +Explanation: +One way to make the array alternating is by converting it to [3,1,3,1,3,1]. +The number of operations required in this case is 3. +It can be proven that it is not possible to make the array alternating in less than 3 operations. +Example 2: + +Input: nums = [1,2,2,2,2] +Output: 2 +Explanation: +One way to make the array alternating is by converting it to [1,2,1,2,1]. +The number of operations required in this case is 2. +Note that the array cannot be converted to [2,2,2,2,2] because in this case nums[0] == nums[1] which violates the conditions of an alternating array. + + +Constraints: + +1 <= nums.length <= 105 +1 <= nums[i] <= 105 +*/ + +#include +using namespace std; + +class Solution { +public: + int minimumOperations(vector& nums) { + int freqEven[100001]={0};//frq array, 1->10^5 constraint (of even elements) + int freqOdd[100001]={0};//frq array, 1->10^5 constraint (of odd elements) + int n = nums.size(); + int ans = 0; + int maxi=nums[0]; + //frquency array filling + for(int i=0;ifreqFirstMaxEven){ + secondMaxEven=firstMaxEven; + freqSecondMaxEven=freqFirstMaxEven; + freqFirstMaxEven=freqEven[i]; + firstMaxEven=i; + } + else if(freqEven[i]>freqSecondMaxEven){ + freqSecondMaxEven=freqEven[i]; + secondMaxEven=i; + } + if(freqOdd[i]>freqFirstMaxOdd){ + secondMaxOdd=firstMaxOdd; + freqSecondMaxOdd=freqFirstMaxOdd; + freqFirstMaxOdd=freqOdd[i]; + firstMaxOdd=i; + } + else if(freqOdd[i]>freqSecondMaxOdd){ + freqSecondMaxOdd=freqOdd[i]; + secondMaxOdd=i; + } + } + if(firstMaxEven != firstMaxOdd){//when p!=q + return n - freqFirstMaxEven - freqFirstMaxOdd; + } + else{//when p=q + return min(n-freqFirstMaxEven-freqSecondMaxOdd, n-freqFirstMaxOdd-freqSecondMaxEven); + } + } +}; +int main() { + return 0; +} \ No newline at end of file diff --git a/contests/leetCode/wc280/opCntzro.cpp b/contests/leetCode/wc280/opCntzro.cpp new file mode 100644 index 0000000..921b13b --- /dev/null +++ b/contests/leetCode/wc280/opCntzro.cpp @@ -0,0 +1,64 @@ +/* +2169. Count Operations to Obtain Zero +User Accepted:9910 +User Tried:10138 +Total Accepted:10194 +Total Submissions:14135 +Difficulty:Easy +You are given two non-negative integers num1 and num2. + +In one operation, if num1 >= num2, you must subtract num2 from num1, otherwise subtract num1 from num2. + +For example, if num1 = 5 and num2 = 4, subtract num2 from num1, thus obtaining num1 = 1 and num2 = 4. However, if num1 = 4 and num2 = 5, after one operation, num1 = 4 and num2 = 1. +Return the number of operations required to make either num1 = 0 or num2 = 0. + + + +Example 1: + +Input: num1 = 2, num2 = 3 +Output: 3 +Explanation: +- Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1. +- Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1. +- Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1. +Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations. +So the total number of operations required is 3. +Example 2: + +Input: num1 = 10, num2 = 10 +Output: 1 +Explanation: +- Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0. +Now num1 = 0 and num2 = 10. Since num1 == 0, we are done. +So the total number of operations required is 1. + + +Constraints: + +0 <= num1, num2 <= 105 +*/ + +#include +using namespace std; + +class Solution { +public: + int countOperations(int num1, int num2) { + int cnt=0; + while(num1 and num2){ + if(num1>=num2){ + num1-=num2; + cnt++; + } + else{ + num2-=num1; + cnt++; + } + } + return cnt; + } +}; +int main() { + return 0; +} \ No newline at end of file diff --git a/contests/leetCode/wc280/remMinMagBeans.cpp b/contests/leetCode/wc280/remMinMagBeans.cpp new file mode 100644 index 0000000..d493e64 --- /dev/null +++ b/contests/leetCode/wc280/remMinMagBeans.cpp @@ -0,0 +1,70 @@ +/* +2171. Removing Minimum Number of Magic Beans +User Accepted:3061 +User Tried:4726 +Total Accepted:3138 +Total Submissions:10046 +Difficulty:Medium +You are given an array of positive integers beans, where each integer represents the number of magic beans found in a particular magic bag. + +Remove any number of beans (possibly none) from each bag such that the number of beans in each remaining non-empty bag (still containing at least one bean) is equal. Once a bean has been removed from a bag, you are not allowed to return it to any of the bags. + +Return the minimum number of magic beans that you have to remove. + + + +Example 1: + +Input: beans = [4,1,6,5] +Output: 4 +Explanation: +- We remove 1 bean from the bag with only 1 bean. + This results in the remaining bags: [4,0,6,5] +- Then we remove 2 beans from the bag with 6 beans. + This results in the remaining bags: [4,0,4,5] +- Then we remove 1 bean from the bag with 5 beans. + This results in the remaining bags: [4,0,4,4] +We removed a total of 1 + 2 + 1 = 4 beans to make the remaining non-empty bags have an equal number of beans. +There are no other solutions that remove 4 beans or fewer. +Example 2: + +Input: beans = [2,10,3,2] +Output: 7 +Explanation: +- We remove 2 beans from one of the bags with 2 beans. + This results in the remaining bags: [0,10,3,2] +- Then we remove 2 beans from the other bag with 2 beans. + This results in the remaining bags: [0,10,3,0] +- Then we remove 3 beans from the bag with 3 beans. + This results in the remaining bags: [0,10,0,0] +We removed a total of 2 + 2 + 3 = 7 beans to make the remaining non-empty bags have an equal number of beans. +There are no other solutions that removes 7 beans or fewer. + + +Constraints: + +1 <= beans.length <= 105 +1 <= beans[i] <= 105 +*/ + +#include +using namespace std; + +class Solution { +public: + long long minimumRemoval(vector& beans) { + long long n = beans.size(); + long long step = 1e18; + sort(beans.begin(),beans.end()); + long long sum =0; + for(long long i=0; i +using namespace std; + + +class RandomizedSet { + vectorv; + unordered_mapm;//val and idx +public: + RandomizedSet() { + //empty cons + } + //set can't have dupli eles + bool insert(int x) { + if(m.find(x)!=m.end())return false;//ele already present in set + v.push_back(x);//insert + m[x]=v.size()-1;//insert in map with pos + return true; + } + + bool remove(int x) { + if(m.find(x)==m.end())return false;//ele not present in set + auto i = m.find(x);//accessing ele in map, (i->first is key and i->sec is idx) + v[i->second]=v.back();//replacing with last ele in vector + v.pop_back();//removing last ele + m[v[i->second]]=i->second;//storing in map with curr pos, last ele + m.erase(x);//deleting record in map for x + return true; + } + + int getRandom() { + return v[rand()%v.size()]; + } +}; +int main(int argc, char const *argv[]) +{ + /* code */ + return 0; +} diff --git a/vmware/medianDataStream.cpp b/vmware/medianDataStream.cpp new file mode 100644 index 0000000..44046cc --- /dev/null +++ b/vmware/medianDataStream.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + +class MedianFinder { +public: + priority_queue maxpq; //maxHeap + priority_queue, greater> minpq; //minHeap + + void addNum(int num) { + //if both heaps are empty push to max heap + //we push nums which are smaller than top ele of max heap + if(maxpq.empty() or maxpq.top() > num) maxpq.push(num); + else minpq.push(num);//otherwise we push to min heap + + + //whichever's size diff become greater than 1 we pop + //ele from that and push to other one + if(maxpq.size() > minpq.size() + 1){ + minpq.push(maxpq.top()); + maxpq.pop(); + } + else if(minpq.size() > maxpq.size() + 1){ + maxpq.push(minpq.top()); + minpq.pop(); + } + + } + + double findMedian() { + if(maxpq.size() == minpq.size()) return (maxpq.top() + minpq.top()) / 2.0; //even elements + else{ //odd elements, return top ele of heap which is larger iin size + if(maxpq.size() > minpq.size()) return maxpq.top(); + else return minpq.top(); + + } + } +}; + + +int main(int argc, char const *argv[]) +{ + /* code */ + return 0; +} diff --git a/vmware/revLL2.cpp b/vmware/revLL2.cpp new file mode 100644 index 0000000..dce5001 --- /dev/null +++ b/vmware/revLL2.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + + + struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} + }; + +class Solution { +public: + ListNode* reverseBetween(ListNode* head, int l, int r) { + if(!head)return NULL; + ListNode* cur = head; + ListNode* prev = NULL; + //left is 2 and right is 4 + while(l>1){//itr ll till we reach idx left + prev=cur; + cur=cur->next; + l--; + r--; + } + //right becomes 2 and left 0 + //prev will be at 1 and curr will be at 2 + //so start is 1 and tail is 2 + ListNode* start = prev; + ListNode* tail = cur;//to indicate connections at end + ListNode* tmp = NULL; + //rev ll till we reach right idx + while(r>0){//itr loop till r goes 0 + tmp=cur->next; + cur->next=prev; + prev=cur; + cur=tmp; + r--; + } + //in between part is reversed + + //establish connection with rest of ll + if(start!=NULL){ + start->next=prev;//when start node is there we connect it with prev + } + else{ + head=prev;//if not there then head is prev + } + tail->next=cur; + return head; + } +}; + +int main(int argc, char const *argv[]) +{ + /* code */ + return 0; +} diff --git a/vmware/sortList.cpp b/vmware/sortList.cpp new file mode 100644 index 0000000..6cec0c1 --- /dev/null +++ b/vmware/sortList.cpp @@ -0,0 +1,53 @@ +#include +using namespace std; + + struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} + }; + +class Solution { + ListNode* merge(ListNode *a, ListNode *b){ + if(!b){ //when we dont have the 2nd ll + return a; + } + if(!a){//when we dont have the 1st ll + return b; + } + if(a->val < b->val){ //then first node will be a + a->next = merge(a->next,b);//2nd node will be smaller between the next to a , and b + return a; + } + else{ + b->next = merge(a, b->next); + return b; + } + } +public: + ListNode* sortList(ListNode* head) { + if(!head or !head->next){ // base case , when 0 node or 1 node + return head; + } + //divide linkedlist (equally or unequally, does not matter) + ListNode * slowPtr = head; + ListNode * fastPtr = head->next; + while(fastPtr and fastPtr->next){ + slowPtr= slowPtr->next; + fastPtr= fastPtr->next->next; + } + //breaking linkedlist + ListNode* newNode = slowPtr->next;//reference, for new head for the other half of ll + slowPtr->next = NULL;//breaking links + + return merge(sortList(head), sortList(newNode));//brealing into further pieces + } +}; + +int main(int argc, char const *argv[]) +{ + /* code */ + return 0; +}