├── CPP ├── FP_Problem01.cpp ├── SWP_Problem01.cpp ├── SWP_Problem02.cpp ├── SWP_Problem03.cpp ├── SWP_Problem04.cpp ├── SWP_Problem05.cpp └── TP_Problem02.cpp ├── Python ├── FP_Problem01.py ├── SWP_Problem01.py ├── SWP_Problem02.py ├── SWP_Problem03.py ├── SWP_Problem04.py ├── SWP_Problem05.py └── TP_Problem02.py └── SWP_Problem04.jpg /CPP/FP_Problem01.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @author Aakash.Verma 3 | 4 | Problem: Given an array of sorted numbers and a target sum, find a pair in the array whose 5 | sum is equal to the given target. 6 | Write a function to return the indices of the two numbers (i.e. the pair) such that 7 | they add up to the given target. 8 | 9 | Example 1: 10 | Input: [1, 2, 3, 4, 6], target=6 11 | Output: [1, 3] 12 | Explanation: The numbers at index 1 and 3 add up to 6: 2+4=6 13 | 14 | Example 2: 15 | Input: [2, 5, 9, 11], target=11 16 | Output: [0, 2] 17 | Explanation: The numbers at index 0 and 2 add up to 11: 2+9=11 18 | 19 | Approach: 20 | 21 | We'll use two pointers, left and right. 22 | The left pointer will point to 0th index and right pointer will point to the last index. 23 | We do sum = arr[left] + arr[right], if sum is greater than target sum, we'll decrement right. 24 | If sum is less than target sum then we'll increment left. 25 | We'll keep on doing this, till left < right. 26 | If left becomes greater than right and we do not found any pair which gives us as target sum. 27 | Once we find the pair, we'll return the indices as an array. 28 | Otherwise, we return [-1. -1]. 29 | Time Complexity: 30 | The time complexity of the above algorithm will be O(N), where ‘N’ is the total number of elements in the given array. 31 | Space Complexity: 32 | The space complexity will also be O(N), as, in the worst case, we will be pushing ‘N’ numbers in the HashTable. 33 | */ 34 | 35 | #include 36 | #include 37 | using namespace std; 38 | 39 | class FP_Problem01 { 40 | public: 41 | static pair search(const vector& arr, int targetSum) { 42 | int left = 0, right = arr.size() - 1; 43 | while(left < right) { 44 | int currentSum = arr[left] + arr[right]; 45 | if(targetSum == currentSum) { 46 | return make_pair(left, right); 47 | } 48 | 49 | if(targetSum > currentSum) 50 | left++; 51 | else 52 | right--; 53 | } 54 | return make_pair(-1, -1); 55 | } 56 | }; 57 | 58 | int main(int argc, char * argv[]) { 59 | auto result = FP_Problem01::search(vector{1, 2, 3, 4, 6}, 6); 60 | cout << << "[" << result.first << ", " << result.second << "]" << endl; 61 | } -------------------------------------------------------------------------------- /CPP/SWP_Problem01.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author 4 | * Aakash Verma 5 | * 6 | * Problem: Given an array, find the average of all contiguous subarrays of size ‘K’ in it. 7 | * Sliding Window Pattern 8 | * 9 | * Output: 2.2 2.8 2.4 3.6 2.8 10 | * Time Complexity: O(N) 11 | * 12 | */ 13 | 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | class SWP_Problem01 { 19 | public: 20 | static vector findAveragesOfSubarrays(const vector& arr, int k) { 21 | vector result(arr.size() - k + 1); 22 | double windowSum = 0; 23 | int windowStart = 0; 24 | for(int windowEnd = 0; windowEnd < arr.size(); windowEnd++) { 25 | windowSum += arr[windowEnd]; 26 | if(windowEnd >= k - 1) { 27 | result[windowStart] = windowSum/k; 28 | windowSum -= arr[windowStart]; 29 | windowStart++; 30 | } 31 | } 32 | return result; 33 | } 34 | }; 35 | 36 | int main(int argc, char * argv[]) { 37 | int k = 5; 38 | vector result = SWP_Problem01::findAveragesOfSubarrays(vector{1, 3, 2, 6, -1, 4, 1, 8, 2}, k); 39 | for (double r : result) { 40 | cout << r << " "; 41 | } 42 | cout << endl; 43 | } -------------------------------------------------------------------------------- /CPP/SWP_Problem02.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author 4 | * Aakash Verma 5 | * 6 | * Problem: Given an array of positive numbers and a positive number ‘k’, 7 | * find the maximum sum of any contiguous subarray of size ‘k’. 8 | * 9 | * Sliding Window Pattern 10 | * 11 | * Output: 9 12 | * 13 | */ 14 | 15 | #include 16 | #include 17 | using namespace std; 18 | 19 | class SWP_Problem02 { 20 | public: 21 | static int contiguousSubArrayOfSizeKWithMaxSum(const vector& arr, int k) { 22 | int maxSum = 0; 23 | int windowSum = 0; 24 | int windowStart = 0; 25 | for(int windowEnd = 0; windowEnd < arr.size(); windowEnd++) { 26 | windowSum += arr[windowEnd]; 27 | if(windowEnd >= k - 1) { 28 | maxSum = max(maxSum, windowSum); 29 | windowSum -= arr[windowStart]; 30 | windowStart++; 31 | } 32 | } 33 | return maxSum; 34 | } 35 | }; 36 | 37 | int main(int argc, char * argv[]) { 38 | int k = 3; 39 | int sum = SWP_Problem02::contiguousSubArrayOfSizeKWithMaxSum(vector{2, 1, 5, 1, 3, 2}, k); 40 | cout << sum << endl; 41 | } -------------------------------------------------------------------------------- /CPP/SWP_Problem03.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author 4 | * Aakash Verma 5 | * 6 | * Problem: Given an array of positive numbers and a positive number ‘S’, 7 | * find the length of the smallest contiguous subarray whose sum is greater than 8 | * or equal to ‘S’. Return 0, if no such subarray exists. 9 | * 10 | * Sliding Window Pattern 11 | * 12 | * Output: 2 13 | * 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | using namespace std; 20 | 21 | class SWP_Problem03 { 22 | public: 23 | static int smallestSubarrayWithGivenSum(const vector& arr, int givenSum) { 24 | int minLength = numeric_limits::max(); 25 | int windowSum = 0; 26 | int windowStart = 0; 27 | for(int windowEnd = 0; windowEnd < arr.size(); windowEnd++) { 28 | windowSum += arr[windowEnd]; 29 | while (windowSum >= givenSum) { 30 | minLength = min(minLength, windowEnd - windowStart + 1); 31 | windowSum -= arr[windowStart]; 32 | windowStart++; 33 | } 34 | } 35 | 36 | if(minLength == numeric_limits::max()) { 37 | return 0; 38 | } 39 | 40 | return minLength; 41 | } 42 | }; 43 | 44 | int main(int argc, char * argv[]) { 45 | int length = SWP_Problem03::smallestSubarrayWithGivenSum(vector{2, 1, 5, 2, 3, 2}, 7); 46 | cout << length << endl; 47 | } -------------------------------------------------------------------------------- /CPP/SWP_Problem04.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @author Aakash.Verma 3 | 4 | Problem: Given a string, find the length of the longest substring in it with no more than 5 | K distinct characters. 6 | 7 | Example 1: 8 | Input: string = "araaci", k = 2 9 | Output: 4 10 | Explanation: The longest substring with no more than '2' distinct characters is "araa". 11 | 12 | Example 2: 13 | Input: string = "cbbebi", k = 3 14 | Ouput: 5 15 | Explanation: The longest substrings with no more than '3' distinct characters are "cbbeb" & "bbebi". 16 | 17 | Approach: 18 | 19 | We've taken a map to keep count of distinct character that we'll encounter during the process. 20 | Until the size of map becomes greater than k, we'll keep adding the characters into map with their 21 | frequency & we'll keep finding the maxLength. 22 | As soon as size becomes greater than k, we will try to shrink the window from the beginning 23 | if the count of distinct characters in the map is larger than ‘k’. We will shrink the window until 24 | we have no more than ‘k’ distinct characters in the map. This is needed as we intend to find the longest window. 25 | While shrinking, we’ll decrement the frequency of the character going out of the window and 26 | remove it from the map if its frequency becomes zero. 27 | At the end of each step, we’ll check if the current window length is the longest so far, 28 | and if so, remember its length. 29 | */ 30 | 31 | 32 | #include 33 | #include 34 | #include 35 | using namespace std; 36 | 37 | class SWP_Problem04 { 38 | public: 39 | static int findLength(const string &s, int k) { 40 | if(s.length() == 0 || s.length() < k) { 41 | return -1; 42 | } 43 | 44 | int windowStart = 0; 45 | int maxLength = 0; 46 | unordered_map map; 47 | 48 | for(int windowEnd = 0; windowEnd < s.length(); windowEnd++) { 49 | char rightCharacter = s[windowEnd]; 50 | map[rightCharacter]++; 51 | 52 | while((int)map.size() > k) { 53 | char leftCharacter = s[windowStart]; 54 | map[leftCharacter]--; 55 | if(map[leftCharacter] == 0) { 56 | map.erase(leftCharacter); 57 | } 58 | windowStart++; 59 | } 60 | maxLength = max(maxLength, windowEnd - windowStart + 1); 61 | } 62 | return maxLength; 63 | } 64 | }; 65 | 66 | int main(int argc, char *argv[]) { 67 | int length = SWP_Problem04::findLength("araaci", 2); 68 | cout << length << endl; 69 | } -------------------------------------------------------------------------------- /CPP/SWP_Problem05.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @author Aakash.Verma 3 | 4 | Problem: Given a string, find the length of the longest substring which has no repeating characters. 5 | 6 | Example 1: 7 | Input: String="aabccbb" 8 | Output: 3 9 | Explanation: The longest substring without any repeating characters is "abc". 10 | 11 | Example 2: 12 | Input: String="abccde" 13 | Output: 3 14 | Explanation: Longest substrings without any repeating characters are "abc" & "cde". 15 | 16 | Approach: 17 | 18 | We can use a Map to remember the last index of each character we have processed. 19 | Whenever we get a repeating character we will shrink our sliding window to ensure that we 20 | always have distinct characters in the sliding window. 21 | i.e windowStart to windowEnd will always have unique characters. 22 | So, whenever I get any character which is already processed, I'll make my windowStart as 23 | the last index of the character + 1. 24 | */ 25 | 26 | 27 | #include 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | class SWP_Problem05 { 33 | public: 34 | static int findLength(const string &s) { 35 | if(s.length() == 0) { 36 | return -1; 37 | } 38 | 39 | int windowStart = 0; 40 | int maxLength = 0; 41 | unordered_map map; 42 | 43 | for(int windowEnd = 0; windowEnd < s.length(); windowEnd++) { 44 | char rightCharacter = s[windowEnd]; 45 | 46 | if(map.find(rightCharacter) != map.end()) { 47 | windowStart = max(windowStart, map[rightCharacter] + 1); 48 | } 49 | 50 | map[rightCharacter] = windowEnd; 51 | maxLength = max(maxLength, windowEnd - windowStart + 1); 52 | } 53 | return maxLength; 54 | } 55 | }; 56 | 57 | int main(int argc, char *argv[]) { 58 | int length = SWP_Problem05::findLength("aabccbb"); 59 | cout << length << endl; 60 | } -------------------------------------------------------------------------------- /CPP/TP_Problem02.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @author Aakash.Verma 3 | 4 | Problem: Given an array containing 0s, 1s and 2s, sort the array in-place. You should treat numbers of the array as objects, 5 | hence, we can’t count 0s, 1s, and 2s to recreate the array. 6 | The flag of the Netherlands consists of three colors: red, white and blue; and since our input array also consists of three 7 | different numbers that is why it is called Dutch National Flag problem. 8 | 9 | Example 1: 10 | Input: [1, 0, 2, 1, 0] 11 | Output: [0, 0, 1, 1, 2] 12 | 13 | Example 2: 14 | Input: [2, 2, 0, 1, 2, 0] 15 | Output: [0 0 1 2 2 2 ] 16 | 17 | Approach: 18 | 19 | One of the solution is that count the number of 0s, 1s, and 2s in the array and finally start putting into the array. 20 | But this method of solving the problem fails as it is asking for some in-place solution. 21 | 22 | We can use a Two Pointers approach while iterating through the array. Let’s say the two pointers are called low and high which 23 | are pointing to the first and the last element of the array respectively. So while iterating, we will move all 0s before low and all 2s after high 24 | so that in the end, all 1s will be between low and high. 25 | */ 26 | 27 | #include 28 | #include 29 | using namespace std; 30 | 31 | class TP_Problem02 { 32 | public: 33 | static void sort(vector& arr) { 34 | int low = 0, high = arr.size() - 1; 35 | 36 | for(int i = 0; i <= high;) { 37 | if(arr[i] == 0) { 38 | swap(arr, i, low); 39 | i++; 40 | low++; 41 | } 42 | else if(arr[i] == 1) { 43 | i++; 44 | } 45 | else { 46 | swap(arr, i, high); 47 | high--; 48 | } 49 | } 50 | } 51 | private: 52 | static void swap(vector& arr, int i, int j) { 53 | int temp = arr[i]; 54 | arr[i] = arr[j]; 55 | arr[j] = temp; 56 | } 57 | }; 58 | 59 | int main(int argc, char * argv[]) { 60 | vector arr = {2, 2, 0, 1, 2, 0}; 61 | TP_Problem02::sort(arr); 62 | for (auto num: arr) { 63 | cout << num << " "; 64 | } 65 | } -------------------------------------------------------------------------------- /Python/FP_Problem01.py: -------------------------------------------------------------------------------- 1 | 2 | # @author Aakash.Verma 3 | 4 | # Problem: Given an array of sorted numbers and a target sum, find a pair in the array whose 5 | # sum is equal to the given target. 6 | # Write a function to return the indices of the two numbers (i.e. the pair) such that 7 | # they add up to the given target. 8 | 9 | # Example 1: 10 | # Input: [1, 2, 3, 4, 6], target=6 11 | # Output: [1, 3] 12 | # Explanation: The numbers at index 1 and 3 add up to 6: 2+4=6 13 | 14 | # Example 2: 15 | # Input: [2, 5, 9, 11], target=11 16 | # Output: [0, 2] 17 | # Explanation: The numbers at index 0 and 2 add up to 11: 2+9=11 18 | 19 | # Approach: 20 | 21 | # We'll use two pointers, left and right. 22 | # The left pointer will point to 0th index and right pointer will point to the last index. 23 | # We do sum = arr[left] + arr[right], if sum is greater than target sum, we'll decrement right. 24 | # If sum is less than target sum then we'll increment left. 25 | # We'll keep on doing this, till left < right. 26 | # If left becomes greater than right and we do not found any pair which gives us as target sum. 27 | # Once we find the pair, we'll return the indices as an array. 28 | # Otherwise, we return [-1. -1]. 29 | # Time Complexity: 30 | # The time complexity of the above algorithm will be O(N), where ‘N’ is the total number of elements in the given array. 31 | # Space Complexity: 32 | # The space complexity will also be O(N), as, in the worst case, we will be pushing ‘N’ numbers in the HashTable. 33 | 34 | 35 | def search(arr, targetSum): 36 | left = 0 37 | right = len(arr) - 1 38 | 39 | while left < right: 40 | currentSum = arr[left] + arr[right] 41 | if(currentSum == targetSum): 42 | return [left, right] 43 | 44 | if targetSum > currentSum: 45 | left += 1 46 | else: 47 | right -= 1 48 | 49 | return [-1, -1] 50 | 51 | def main(): 52 | ans = search([1, 2, 3, 4, 6], 6) 53 | print(ans) 54 | 55 | main() -------------------------------------------------------------------------------- /Python/SWP_Problem01.py: -------------------------------------------------------------------------------- 1 | 2 | # @author 3 | # Aakash Verma 4 | 5 | # Problem: Given an array, find the average of all contiguous subarrays of size ‘K’ in it. 6 | # Sliding Window Pattern 7 | 8 | # Output: 2.2 2.8 2.4 3.6 2.8 9 | 10 | # Time Complexity: O(N) 11 | 12 | 13 | def findAveragesOfSubarrays(arr, k): 14 | result = [] 15 | windowSum = 0 16 | windowStart = 0 17 | 18 | for windowEnd in range(len(arr)): 19 | windowSum += arr[windowEnd] 20 | if windowEnd >= k - 1: 21 | result.append(windowSum/k) 22 | windowSum -= arr[windowStart] 23 | windowStart += 1 24 | 25 | return result 26 | 27 | def main(): 28 | arr = [1, 3, 2, 6, -1, 4, 1, 8, 2] 29 | k = 5 30 | ans = findAveragesOfSubarrays(arr, k) 31 | print(str(ans)) 32 | 33 | main() -------------------------------------------------------------------------------- /Python/SWP_Problem02.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | # @author 4 | # Aakash Verma 5 | 6 | # Problem: Given an array of positive numbers and a positive number ‘k’, 7 | # find the maximum sum of any contiguous subarray of size ‘k’. 8 | 9 | # Sliding Window Pattern 10 | 11 | # Output: 9 12 | 13 | 14 | 15 | 16 | def contiguousSubArrayOfSizeKWithMaxSum(arr, k): 17 | maxSum = 0 18 | windowSum = 0 19 | windowStart = 0 20 | 21 | for windowEnd in range(len(arr)): 22 | windowSum += arr[windowEnd] 23 | if windowEnd >= k - 1: 24 | maxSum = max(maxSum, windowSum) 25 | windowSum -= arr[windowStart] 26 | windowStart += 1 27 | 28 | return maxSum 29 | 30 | def main(): 31 | arr = [2, 1, 5, 1, 3, 2] 32 | k = 3 33 | ans = contiguousSubArrayOfSizeKWithMaxSum(arr, k) 34 | print(ans) 35 | 36 | main() -------------------------------------------------------------------------------- /Python/SWP_Problem03.py: -------------------------------------------------------------------------------- 1 | # @author 2 | # Aakash Verma 3 | 4 | # Problem: Given an array of positive numbers and a positive number ‘S’, 5 | # find the length of the smallest contiguous subarray whose sum is greater than 6 | # or equal to ‘S’. Return 0, if no such subarray exists. 7 | 8 | # Sliding Window Pattern 9 | 10 | # Output: 2 11 | 12 | 13 | import math 14 | 15 | def smallestSubarrayWithGivenSum(arr, givenSum): 16 | minLength = math.inf 17 | windowSum = 0 18 | windowStart = 0 19 | 20 | for windowEnd in range(len(arr)): 21 | windowSum += arr[windowEnd] 22 | while windowSum >= givenSum: 23 | minLength = min(minLength, windowEnd - windowStart + 1) 24 | windowSum -= arr[windowStart] 25 | windowStart += 1 26 | 27 | if minLength == math.inf: 28 | return 0 29 | 30 | return minLength 31 | 32 | def main(): 33 | length = smallestSubarrayWithGivenSum([2, 1, 5, 2, 3, 2], 7) 34 | print(length) 35 | 36 | main() -------------------------------------------------------------------------------- /Python/SWP_Problem04.py: -------------------------------------------------------------------------------- 1 | 2 | # @author Aakash.Verma 3 | # Problem: Given a string, find the length of the longest substring in it with no more than 4 | # K distinct characters. 5 | 6 | # Example 1: 7 | # Input: string = "araaci", k = 2 8 | # Output: 4 9 | # Explanation: The longest substring with no more than '2' distinct characters is "araa". 10 | 11 | # Example 2: 12 | # Input: string = "cbbebi", k = 3 13 | # Ouput: 5 14 | # Explanation: The longest substrings with no more than '3' distinct characters are "cbbeb" & "bbebi". 15 | 16 | # Approach: 17 | 18 | # We've taken a map to keep count of distinct character that we'll encounter during the process. 19 | # Until the size of map becomes greater than k, we'll keep adding the characters into map with their 20 | # frequency & we'll keep finding the maxLength. 21 | # As soon as size becomes greater than k, we will try to shrink the window from the beginning 22 | # if the count of distinct characters in the map is larger than ‘k’. We will shrink the window until 23 | # we have no more than ‘k’ distinct characters in the map. This is needed as we intend to find the longest window. 24 | # While shrinking, we’ll decrement the frequency of the character going out of the window and 25 | # remove it from the map if its frequency becomes zero. 26 | # At the end of each step, we’ll check if the current window length is the longest so far, 27 | # and if so, remember its length. 28 | 29 | 30 | def findLength(s, k): 31 | 32 | if(s == None or len(s) < k or len(s) == 0): 33 | return -1 34 | 35 | windowStart = 0 36 | maxLength = 0 37 | dictionary = {} 38 | 39 | for windowEnd in range(len(s)): 40 | rightCharacter = s[windowEnd] 41 | if rightCharacter not in dictionary: 42 | dictionary[rightCharacter] = 0 43 | dictionary[rightCharacter] += 1 44 | 45 | while len(dictionary) > k: 46 | leftCharacter = s[windowStart] 47 | dictionary[leftCharacter] -= 1 48 | if dictionary[leftCharacter] == 0: 49 | del dictionary[leftCharacter] 50 | windowStart += 1 51 | 52 | maxLength = max(maxLength, windowEnd - windowStart + 1) 53 | 54 | return maxLength 55 | 56 | def main(): 57 | s = "araaci" 58 | k = 2 59 | length = findLength(s, k) 60 | print(length) 61 | 62 | main() -------------------------------------------------------------------------------- /Python/SWP_Problem05.py: -------------------------------------------------------------------------------- 1 | # @author Aakash.Verma 2 | # Problem: Given a string, find the length of the longest substring which has no repeating characters. 3 | 4 | # Example 1: 5 | # Input: String="aabccbb" 6 | # Output: 3 7 | # Explanation: The longest substring without any repeating characters is "abc". 8 | 9 | # Example 2: 10 | # Input: String="abccde" 11 | # Output: 3 12 | # Explanation: Longest substrings without any repeating characters are "abc" & "cde". 13 | 14 | # Approach: 15 | 16 | # We can use a Map to remember the last index of each character we have processed. 17 | # Whenever we get a repeating character we will shrink our sliding window to ensure that we 18 | # always have distinct characters in the sliding window. 19 | # i.e windowStart to windowEnd will always have unique characters. 20 | # So, whenever I get any character which is already processed, I'll make my windowStart as 21 | # the last index of the character + 1. 22 | 23 | 24 | def findLength(s): 25 | 26 | if(s == None or len(s) == 0): 27 | return -1 28 | 29 | windowStart = 0 30 | maxLength = 0 31 | dictionary = {} 32 | 33 | for windowEnd in range(len(s)): 34 | rightCharacter = s[windowEnd] 35 | 36 | if rightCharacter in dictionary: 37 | windowStart = max(windowStart, dictionary[rightCharacter] + 1) 38 | 39 | dictionary[rightCharacter] = windowEnd 40 | maxLength = max(maxLength, windowEnd - windowStart + 1) 41 | 42 | return maxLength 43 | 44 | def main(): 45 | s = "aabccbb" 46 | length = findLength(s) 47 | print(length) 48 | 49 | main() -------------------------------------------------------------------------------- /Python/TP_Problem02.py: -------------------------------------------------------------------------------- 1 | 2 | # @author Aakash.Verma 3 | 4 | # Problem: Given an array containing 0s, 1s and 2s, sort the array in-place. You should treat numbers of the array as objects, 5 | # hence, we can’t count 0s, 1s, and 2s to recreate the array. 6 | # The flag of the Netherlands consists of three colors: red, white and blue; and since our input array also consists of three 7 | # different numbers that is why it is called Dutch National Flag problem. 8 | 9 | # Example 1: 10 | # Input: [1, 0, 2, 1, 0] 11 | # Output: [0, 0, 1, 1, 2] 12 | 13 | # Example 2: 14 | # Input: [2, 2, 0, 1, 2, 0] 15 | # Output: [0 0 1 2 2 2 ] 16 | 17 | # Approach: 18 | 19 | # One of the solution is that count the number of 0s, 1s, and 2s in the array and finally start putting into the array. 20 | # But this method of solving the problem fails as it is asking for some in-place solution. 21 | 22 | # We can use a Two Pointers approach while iterating through the array. Let’s say the two pointers are called low and high which 23 | # are pointing to the first and the last element of the array respectively. So while iterating, we will move all 0s before low and all 2s after high 24 | # so that in the end, all 1s will be between low and high. 25 | 26 | def sort(arr): 27 | low, high = 0, len(arr) - 1 28 | i = 0 29 | while i <= high: 30 | if arr[i] == 0: 31 | arr[i], arr[low] = arr[low], arr[i] 32 | i += 1 33 | low += 1 34 | elif arr[i] == 1: 35 | i += 1 36 | else: 37 | arr[i], arr[high] = arr[high], arr[i] 38 | high -= 1 39 | 40 | def main(): 41 | arr = [2, 2, 0, 1, 2, 0] 42 | sort(arr) 43 | print(arr) 44 | 45 | main() 46 | -------------------------------------------------------------------------------- /SWP_Problem04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aakashverma1124/Cracking-The-Coding-Interview-CPP-Python/2995235f683a3bfae0d0f89064aebee550f939aa/SWP_Problem04.jpg --------------------------------------------------------------------------------