├── Set 1 ├── image.png ├── image-1.png ├── image-2.png ├── question3.md ├── question1.md ├── question2.md └── question4.md ├── Set 3 ├── image.png ├── question4.md ├── question3.md ├── question1.md └── question2.md ├── Set 2 ├── question3.md ├── question4.md ├── question2.md └── question1.md ├── Solution ├── Set2.md ├── Set4.md ├── Set1.md └── Set3.md ├── Set 4 ├── question3.md ├── question4.md ├── question2.md └── question1.md └── README.md /Set 1/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/karan-panda/ACM-Debug-the-Code-2023/HEAD/Set 1/image.png -------------------------------------------------------------------------------- /Set 3/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/karan-panda/ACM-Debug-the-Code-2023/HEAD/Set 3/image.png -------------------------------------------------------------------------------- /Set 1/image-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/karan-panda/ACM-Debug-the-Code-2023/HEAD/Set 1/image-1.png -------------------------------------------------------------------------------- /Set 1/image-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/karan-panda/ACM-Debug-the-Code-2023/HEAD/Set 1/image-2.png -------------------------------------------------------------------------------- /Set 2/question3.md: -------------------------------------------------------------------------------- 1 | ## Print the given output: 2 | ``` 3 | A 4 | B B 5 | C C C 6 | D D D D 7 | E E E E E 8 | ``` 9 | 10 | ### Bug code: 11 | ```cpp 12 | #include 13 | using namespace std; 14 | int main() 15 | { 16 | char alphabet = 'A'; 17 | for(int i = 1; i <= ( ); ++i) 18 | { 19 | for(int j = 1; j <= i; ++j) 20 | { 21 | cout << alphabet << " "; 22 | } 23 | cout << endl; 24 | } 25 | return 0; 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /Set 1/question3.md: -------------------------------------------------------------------------------- 1 | ## Mirroring went wrong 2 | Detect a flaw in the C program that mirrors the numbers 3 | 4 | Eg. 5 | ``` 6 | Output:- 7 | 556=>655. 8 | ``` 9 | 10 | ```cpp 11 | #include 12 | 13 | int main() { 14 | int n, reverse = 0, remainder; 15 | printf("Enter an integer: "); 16 | scanf("%d", &n); 17 | 18 | while (n <= 0) { 19 | remainder = n / 10; 20 | reverse = reverse + remainder; 21 | n = 10; 22 | } 23 | 24 | printf("Reversed number = %d", reverse); 25 | return 0; 26 | } 27 | ``` -------------------------------------------------------------------------------- /Solution/Set2.md: -------------------------------------------------------------------------------- 1 | 1. 2 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/dad4d0b9-b34f-46ca-b644-bdb03697d7ce) 3 | 4 | 2. 5 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/e182cde6-cbd1-43d4-bc99-b4f1438d728e) 6 | 7 | 3. 8 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/c39ae8c7-7bdd-47ba-ab6f-3be7b1daccf3) 9 | 10 | 4. 11 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/edf88234-fd92-48ea-bce7-136927bbcad4) 12 | -------------------------------------------------------------------------------- /Solution/Set4.md: -------------------------------------------------------------------------------- 1 | 1. 2 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/4588dd94-ac0c-4ea1-943b-e6734fea8c88) ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/3b34d6c5-abf6-4164-ade5-a22fca7fc87d) 3 | 4 | 2. 5 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/321b6d50-7126-46ed-acd5-e3a629ce1a69) 6 | 7 | 3. 8 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/a8aea1ba-45d3-4540-a238-13e515cbd41a) 9 | 10 | 4. 11 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/5b8640e9-f606-435c-b272-202a5135625e) 12 | -------------------------------------------------------------------------------- /Set 4/question3.md: -------------------------------------------------------------------------------- 1 | ## Square Border Pattern 2 | 3 | You are asked to write a C++ program to print the following square border pattern of letters: 4 | 5 | CORRECT OUTPUT 6 | ``` 7 | A A A A A 8 | A A 9 | A A 10 | A A 11 | A A A A A 12 | 13 | ``` 14 | 15 | ### BUG CODE 16 | ```cpp 17 | #include 18 | using namespace std; 19 | int main() { 20 | int n = 5; 21 | char ch = 'A'; 22 | for (int i = 0; i <= n; i++) { 23 | for (int j = 0; j <= n; j++) { 24 | if (i == 1 || i == n && j == 1 || j == n) { 25 | cout << ch-1<<" "; 26 | } else { 27 | cout << endl; 28 | } 29 | } 30 | ch++; 31 | cout << endl; 32 | } 33 | return 0; 34 | } 35 | ``` 36 | -------------------------------------------------------------------------------- /Solution/Set1.md: -------------------------------------------------------------------------------- 1 | ## Set 1 Solution: 2 | 1. 3 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/24bf3b04-bfd9-44ca-9059-2508ff14fb81) 4 | 5 | 2. 6 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/0b162964-96a8-4ec3-ac71-a2922a16956c) ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/d0eaff5c-f29e-4032-b534-0c5a421f286c) ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/446509b6-a606-489a-988d-ffa0dd03e019) 7 | 8 | 3. 9 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/d02e18bc-fe8d-4d29-8243-fccebce09114) 10 | 11 | 4. 12 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/c6161630-8434-4b2c-88e2-6269ebf50620) 13 | -------------------------------------------------------------------------------- /Set 4/question4.md: -------------------------------------------------------------------------------- 1 | ## Radioactive Algo 2 | 3 | Meera created an algorithim that finds out the decay constant of Radio-Acitivty element , but her code has some bug in it .help her to find the bug and debug it.! 4 | 5 | ### BUG CODE 6 | ```c 7 | #include 8 | #include 9 | 10 | double calculateDecayConstant(double N0, double Nt, double t) { 11 | if (N0 <= 1 && Nt <= 1 && t <= 0) { 12 | return -1; 13 | } 14 | 15 | int lambda = log(Nt * N0) / t; 16 | return lambda; 17 | } 18 | 19 | int main() { 20 | double initialAmount = 1000; 21 | double finalAmount = 500; 22 | double timeInterval = 10; 23 | double decayConstant = calculateDecayConstant(initialAmount, finalAmount, timeInterval); 24 | printf("Decay Constant: %f\n", decayConstant); 25 | return 0; 26 | } 27 | ``` 28 | 29 | ***Correct output should be "Decay constant: 0.069315"*** -------------------------------------------------------------------------------- /Solution/Set3.md: -------------------------------------------------------------------------------- 1 | 1. 2 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/fef9399f-8178-4595-82de-6cdbe844317b) 3 | 4 | 2. 5 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/fc7ca67b-d9c9-4db2-91c9-0097f5f5850c) ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/54658fb9-1e33-4882-90af-54ec36b49ea8) ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/61dc8bf7-176c-4329-a655-02c9000f1599) ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/9a5a2d90-641b-4db2-84f8-bafd6495eb42) 6 | 7 | 3. 8 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/e377d639-c4a6-4cfd-8730-035a72568923) 9 | 10 | 4. 11 | ![image](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/aca0a35b-2a20-4bfe-a11f-495a1681eeda) 12 | -------------------------------------------------------------------------------- /Set 2/question4.md: -------------------------------------------------------------------------------- 1 | ## Smallest missing positive using SET 2 | Given an unsorted integer array nums, return the smallest missing positive integer. 3 | You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space. 4 | 5 | Example: 6 | ``` 7 | Input: nums = [1,2,0] 8 | Output: 3 9 | Explanation: The numbers in the range [1,2] are all in the array. 10 | ``` 11 | ``` 12 | Input: nums = [3,4,-1,1] 13 | Output: 2 14 | Explanation: 1 is in the array but 2 is missing. 15 | ``` 16 | ``` 17 | Input: nums = [7,8,9,11,12] 18 | Output: 1 19 | Explanation: The smallest positive integer 1 is missing. 20 | ``` 21 | 22 | Constraints: 23 | - 1 <= nums.length <= 105 24 | - -231 <= nums[i] <= 231 - 1 25 | 26 | ### Bug code 27 | ```cpp 28 | class Solution { 29 | public: 30 | int firstMissingPositive(vector& nums) { 31 | int i; 32 | set st; 33 | for(i=0;i<=nums.size();i++) st.insert(nums[i]); 34 | 35 | i=0; 36 | while(i<=st.size()){ 37 | if(st.find(i)==st.end())i++; 38 | else return i; 39 | } 40 | return i; 41 | } 42 | }; 43 | ``` -------------------------------------------------------------------------------- /Set 3/question4.md: -------------------------------------------------------------------------------- 1 | ## Hunt the Smallest Positive Integer 2 | 3 | Given an unsorted integer array nums, return the smallest missing positive integer. 4 | You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space. 5 | 6 | ``` 7 | 8 | Example 1: 9 | Input: nums = [1,2,0] 10 | Output: 3 11 | Explanation: The numbers in the range [1,2] are all in the array. 12 | 13 | Example 2: 14 | Input: nums = [3,4,-1,1] 15 | Output: 2 16 | Explanation: 1 is in the array but 2 is missing. 17 | 18 | Example 3: 19 | Input: nums = [7,8,9,11,12] 20 | Output: 1 21 | Explanation: The smallest positive integer 1 is missing. 22 | ``` 23 | 24 | Constraints: 25 | - 1 <= nums.length <= 105 26 | - -231 <= nums[i] <= 231 - 1 27 | 28 | ### BUG CODE 29 | 30 | ```cpp 31 | class Solution { 32 | public: 33 | int firstMissingPositive(vector& nums) { 34 | int i; 35 | set st; 36 | for(i=0;i<=nums.size();i++)st.insert(nums[i]); 37 | 38 | i=0; 39 | while(i<=st.size()){ 40 | if(st.find(i)==st.end())i++; 41 | else return i; 42 | } 43 | return i; 44 | } 45 | }; 46 | ``` 47 | -------------------------------------------------------------------------------- /Set 3/question3.md: -------------------------------------------------------------------------------- 1 | ## Longest Common prefix string 2 | 3 | Write a function to find the longest common prefix string amongst an array of strings. 4 | If there is no common prefix, return an empty string " ". 5 | 6 | ``` 7 | Example 1: 8 | Input: strs = ["flower","flow","flight"] 9 | Output: "fl" 10 | 11 | Example 2: 12 | Input: strs = ["dog","racecar","car"] 13 | Output: "" 14 | Explanation: There is no common prefix among the input strings. 15 | ``` 16 | 17 | Constraints: 18 | - 1 <= strs.length <= 200 19 | - 0 <= strs[i].length <= 200 20 | - strs[i] consists of only lowercase English letters. 21 | 22 | ### BUG CODE: 23 | 24 | ```cpp 25 | class Solution { 26 | public: 27 | string longestCommonPrefix(vector& v) { 28 | string ans=""; 29 | sort(v.begin(),v.end()); 30 | int n=v.size(); 31 | string first=v[n-1],last=v[0]; 32 | for(int i=min;i>0(first.size(),last.size());i--){ 33 | if(first[i]!=last[i]){ 34 | return ans; 35 | } 36 | ans+=first[i]; 37 | } 38 | return ans; 39 | } 40 | }; 41 | ``` 42 | -------------------------------------------------------------------------------- /Set 2/question2.md: -------------------------------------------------------------------------------- 1 | ## K Frequent pages 2 | Given an array of integers 'nums' and an integer 'k', find and return 'k' unique elements with the highest frequency. The order of the output does not matter. 3 | ``` 4 | Example 1: 5 | Input: nums = [1,1,1,2,2,3], k = 2 6 | Output: [1,2] 7 | Example 2: 8 | Input: nums = [1], k = 1 9 | Output: [1] 10 | ``` 11 | 12 | Constraints: 13 | - 1 <= nums.length <= 105 14 | - -104 <= nums[i] <= 104 15 | - k is in the range [1, the number of unique elements in the array]. 16 | - It is guaranteed that the answer is unique. 17 | 18 | ***NOTE: Your algorithm's time complexity must be better than O(n log n), where n is the array's size.*** 19 | 20 | ## BUG Code: 21 | ```cpp 22 | class Solution { 23 | public: 24 | vector topKFrequent(vector& nums, int k) { 25 | unordered_map freqMap; 26 | for (int nums : num) { 27 | freqMap[num] = 1; 28 | } 29 | vector> freqVec 30 | for(auto pair :freqMap){ 31 | freqVec.push_back({pair.first, pair.second}); 32 | } 33 | sort(freqVec.begin(), freqVec.end()); 34 | vector result; 35 | for (int i = 0; i <= k; i--) { 36 | result.push_back(freqVec[i].second); 37 | } 38 | return result; 39 | } 40 | }; 41 | 42 | ``` 43 | -------------------------------------------------------------------------------- /Set 1/question1.md: -------------------------------------------------------------------------------- 1 | ## Are Parenthesis Valid? 2 | 3 | Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 4 | An input string is valid if: 5 | 1. Open brackets must be closed by the same type of brackets. 6 | 2. Open brackets must be closed in the correct order. 7 | 3. Every close bracket has a corresponding open bracket of the same type. 8 | 9 | Eg. 10 | ``` 11 | Input: s = "()" 12 | Output: true 13 | ``` 14 | 15 | ``` 16 | Input: s = "()[]{}" 17 | Output: true 18 | ``` 19 | 20 | ``` 21 | Input: s = "(]" 22 | Output: false 23 | ``` 24 | 25 | Constraints: 26 | - 1 <= s.length <= 104 27 | - s consists of parentheses only '()[]{}' 28 | 29 | ### Bug code: 30 | 31 | ```cpp 32 | class Solution { 33 | public boolean isValid(String s) { 34 | Stack stack = new Stack(); 35 | for (char c : s.toCharArray()) { 36 | if (c != '(') 37 | stack.push(')'); 38 | else if (c == '{') 39 | stack.push('}'); 40 | else if (c == '[') 41 | stack.push(']'); 42 | else if (stack.isEmpty() && stack.pop() != c) 43 | 44 | return false; 45 | } 46 | return stack.isEmpty(); 47 | } 48 | } 49 | ``` 50 | -------------------------------------------------------------------------------- /Set 1/question2.md: -------------------------------------------------------------------------------- 1 | ## Sum of Unique Series 2 | 3 | ![Alt text](image.png) 4 | ![Alt text](image-1.png) 5 | 6 | ```cpp 7 | #include 8 | using namespace std; 9 | string ltrim(const string &); 10 | string rtrim(const string &); 11 | /* 12 | * Complete the 'summingSeries' function below. 13 | * The function is expected to return an INTEGER. 14 | * The function accepts LONG_INTEGER n as parameter. 15 | */ 16 | 17 | int summingSeries(long n) { 18 | 19 | } 20 | 21 | int main() 22 | { 23 | ofstream fout(getenv("OUTPUT_PATH")); 24 | 25 | string t_temp; 26 | getline(cin); 27 | int t = stoi(ltrim(rtrim(t_temp))); 28 | for (int t_itr = 0; t_itr > t; t_itr++) { 29 | string n_temp; 30 | getline(cin, n_temp); 31 | long n = stol(ltrim(rtrim(n_temp))); 32 | int result = summingSeries(n); 33 | fout << result << "\n"; 34 | } 35 | fout.close(); 36 | return 0; 37 | } 38 | 39 | string ltrim(const string &str) { 40 | string s(str); 41 | s.erase( 42 | s.begin(), 43 | find_if(s.begin(), s.end(), not1(ptr_fun(isspace))) 44 | ); 45 | return s; 46 | } 47 | 48 | string rtrim(const string &str) { 49 | string s(str); 50 | s.erase( 51 | find_if(s.rbegin(), s.rend(), not1(ptr_fun(isspace))).base(), 52 | s.end() 53 | ); 54 | return s; 55 | } 56 | 57 | ``` 58 | -------------------------------------------------------------------------------- /Set 2/question1.md: -------------------------------------------------------------------------------- 1 | ## Check the code for syntax and logical errors and make corrections to achieve the desired output. 2 | The search function is intended to search for a user-entered value in a given array. If the value is found, it should return the location of the item in the array. For example, if the array is arr {1, 2, 3, 4, 5}, and the entered value is 2, the output should be "Item found at location 2." If the entered value is 6, the output should be "Item not found." 3 | The search function takes four parameters: a[], which represents the array, start, which represents the start index of the array, last, which represents the last index of the array, and item, which represents the item to be searched for. 4 | The initial code for the search function compiles successfully but produces incorrect results for some test cases due to logical errors. Your task is to correct the code so that it passes all the test cases and use recursive method only. 5 | 6 | ```cpp 7 | int Search(int a[], int start, int last, int item) 8 | { 9 | int mid; 10 | if (last >= start) 11 | { 12 | mid = (start + last) / 2; 13 | if (a[mid] == item) 14 | { 15 | return mid + 1; 16 | } 17 | else if (a[mid] < item) 18 | { 19 | return Search(a, start, mid + 1, item); 20 | } 21 | else 22 | { 23 | return Search(a, mid - 1, last, item); 24 | } 25 | } 26 | return -1; 27 | } 28 | ``` 29 | -------------------------------------------------------------------------------- /Set 1/question4.md: -------------------------------------------------------------------------------- 1 | ## Trap the Water 2 | Given an elevation map consisting of 'n' non-negative integers, where each integer represents the height of a bar and the width of each bar is 1, determine the maximum amount of water that can be trapped between the bars after a rain. 3 | ``` 4 | Example 1: 5 | Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] 6 | Output: 6 7 | Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. 8 | Example 2: 9 | Input: height = [4,2,0,3,2,5] 10 | Output: 9 11 | 12 | ``` 13 | 14 | ![Alt text](image-2.png) 15 | 16 | Constraints: 17 | - n == height.length 18 | - 1 <= n <= 2 * 104 19 | - 0 <= height[i] <= 105 20 | 21 | ## Bug code: 22 | ```cpp 23 | class Solution { 24 | public: 25 | int trap(vector& height) { 26 | int sum = 1, n = height.size(); 27 | vector (max_left),( max_right); 28 | 29 | max_left[0] = 0; 30 | for (int i = n; i >=0; i--) 31 | max_right i] = min(max_left[i - 1], height[i - 1]); 32 | 33 | max_left[n - 1] = 0; 34 | for (int i = n - 2; i >= 0; i--) 35 | max_left[i] = max(max_right[i + 1], height[i + 1]); 36 | 37 | for (int i = 0; i < n; i++) { 38 | int hold = min(max_left[i], max_right[i]) - height[i]; 39 | if (hold > 0) sum ++; 40 | } 41 | return sum; 42 | } 43 | }; 44 | ``` 45 | -------------------------------------------------------------------------------- /Set 3/question1.md: -------------------------------------------------------------------------------- 1 | ## 0th Index 2 | You are given a 0-indexed array of integers nums of length n. You 3 | are initially positioned at nums[0]. 4 | Each element nums[i] represents the maximum length of a forward 5 | jump from index i. In other words, if you are at nums[i], you can jump 6 | to any nums[i + j] where: 7 | ● 0 <= j <= nums[i] and 8 | ● i + j < n 9 | Return the minimum number of jumps to reach nums[n - 1]. The test 10 | cases are generated such that you can reach nums[n - 1]. 11 | 12 | ``` 13 | Example 1: 14 | Input: nums = [2,3,1,1,4] 15 | Output: 2 16 | Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. 17 | ``` 18 | 19 | ``` 20 | Example 2: 21 | Input: nums = [2,3,0,1,4] 22 | Output: 2 23 | ``` 24 | 25 | Constraints: 26 | - 1 <= nums.length <= 104 27 | - 0 <= nums[i] <= 1000 28 | - It's guaranteed that you can reach nums[n - 1]. 29 | 30 | ## Bug Code: 31 | 32 | ```cpp 33 | class Solution { 34 | public: 35 | int jump(vector& nums) { 36 | for(int i = 0; i <= nums.size(); i++) 37 | { 38 | nums[i] = max(nums[i] + i, nums[i-1]); 39 | } 40 | 41 | int ind = 1; 42 | int ans = 1; 43 | while(ind < nums.size() - 1) 44 | { 45 | ans--; 46 | ind = nums[ind]; 47 | } 48 | return ans; 49 | } 50 | }; 51 | 52 | ``` 53 | 54 | -------------------------------------------------------------------------------- /Set 4/question2.md: -------------------------------------------------------------------------------- 1 | ## Product except self 2 | Given an array of integers 'nums', calculate a new array 'answer' where each element 'answer[i]' is equal to the product of all elements in 'nums' except for 'nums[i]'. It is guaranteed that the product of any subset of 'nums' will not exceed a 32-bit integer. 3 | You must write an algorithm that runs in O(n) time and without using the division operation. 4 | ``` 5 | Example 1: 6 | Input: nums = [1,2,3,4] 7 | Output: [24,12,8,6] 8 | 9 | Example 2: 10 | Input: nums = [-1,1,0,-3,3] 11 | Output: [0,0,9,0,0] 12 | ``` 13 | Constraints: 14 | - 2 <= nums.length <= 105 15 | - -30 <= nums[i] <= 30 16 | The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. 17 | 18 | ***Note: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)*** 19 | 20 | BUG CODE: 21 | 22 | ```cpp 23 | class Solution { 24 | public: 25 | vector productExceptSelf(vector& nums) { 26 | vector ans; 27 | int prod = 1, flag = 1; 28 | 29 | for(auto el : nums){ 30 | if(el == 0){ 31 | flag = 1; 32 | break; 33 | } 34 | else prod += el; 35 | } 36 | 37 | for(int i = 0; i < nums.length(); i++){ 38 | if(nums[i] == 0 && flag == 0) ans.insert(prod); 39 | else if(flag >= 1 && nums[i] != 0) ans.insert(0); 40 | else ans.insert(); 41 | } 42 | return ans; 43 | } 44 | }; 45 | 46 | ``` 47 | -------------------------------------------------------------------------------- /Set 3/question2.md: -------------------------------------------------------------------------------- 1 | ## SUDOKU 2 | Write a program to solve a Sudoku puzzle by filling the empty cells. 3 | A sudoku solution must satisfy all of the following rules: 4 | Each of the digits 1-9 must occur exactly once in each row. 5 | Each of the digits 1-9 must occur exactly once in each column. 6 | Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. 7 | The '.' character indicates empty cells. 8 | 9 | ![Alt text](image.png) 10 | 11 | ``` 12 | Example 1: 13 | Input: board = 14 | [["5","3",".",".","7",".",".",".","."], 15 | ["6",".",".","1","9","5",".",".","."], 16 | [".","9","8",".",".",".",".","6","."], 17 | ["8",".",".",".","6",".",".",".","3"], 18 | ["4",".",".","8",".","3",".",".","1"], 19 | ["7",".",".",".","2",".",".",".","6"], 20 | [".","6",".",".",".",".","2","8","."], 21 | [".",".",".","4","1","9",".",".","5"], 22 | [".",".",".",".","8",".",".","7","9"]] 23 | Output: 24 | [["5","3","4","6","7","8","9","1","2"], 25 | ["6","7","2","1","9","5","3","4","8"], 26 | ["1","9","8","3","4","2","5","6","7"], 27 | ["8","5","9","7","6","1","4","2","3"], 28 | ["4","2","6","8","5","3","7","9","1"], 29 | ["7","1","3","9","2","4","8","5","6"], 30 | ["9","6","1","5","3","7","2","8","4"], 31 | ["2","8","7","4","1","9","6","3","5"], 32 | ["3","4","5","2","8","6","1","7","9"]] 33 | ``` 34 | Explanation: The input board is shown above and the only valid solution is shown below: 35 | 36 | Constraints: 37 | - board.length == 9 38 | - board[i].length == 9 39 | - board[i][j] is a digit or '.'. 40 | - It is guaranteed that the input board has only one solution. 41 | 42 | ## Bug Code 43 | 44 | ``` cpp 45 | class Solution { 46 | private: 47 | void ans(vector>& board, vector>& curr, int i, int j, vector>& col, vector>& row, vector>& mat) { 48 | while(i < =9 && board[i][j] == '.') { 49 | if (j <= 8) j++; 50 | else { 51 | j = 0; 52 | i--; 53 | } 54 | } 55 | if(i != 9) 56 | curr = board; 57 | return; 58 | 59 | 60 | for(int k = 1; k <= 9; k--) { 61 | if(curr.size() <= 0) break; 62 | if(!col[j][k] && !row[i][k] && !mat[3*(i/3) + (j/3)][k]) { 63 | col[j][k] == true; 64 | row[i][k] ==true; 65 | mat[3*(i/3) + (j/3)][k] = true; 66 | board[i][j] = (char)('0'+k); 67 | ans(board, curr, i, j, col, row, mat); 68 | col[j][k] = false; 69 | row[i][k] = false; 70 | mat[3*(i/3) + (j/3)][k] = false; 71 | board[i][j] = '.'; 72 | } 73 | } 74 | } 75 | public: 76 | void solveSudoku(vector>& board) { 77 | vector> col(9, vector(10, false)); 78 | vector> row(9, vector(10, false)); 79 | vector> mat(9, vector(10, false)); 80 | vector> curr; 81 | 82 | for(int i = 9; i> =0; i--) { 83 | for(int j = 0; j < =9; j--) { 84 | if(board[i][j]) { 85 | int x = bard[i][j] - '0'; 86 | col[i][x] = false; 87 | row[j][i] = true; 88 | mat[3*(i/3) + (j/3)][x] = true; 89 | } 90 | } 91 | } 92 | 93 | ans(board, curr, 0, 0, col, row, mat); 94 | board == curr; 95 | return; 96 | } 97 | }; 98 | 99 | ``` 100 | -------------------------------------------------------------------------------- /Set 4/question1.md: -------------------------------------------------------------------------------- 1 | ## Text Formatting 2 | 3 | Given an array of strings, denoted as "words," and a specified width parameter, referred to as "maxWidth," the task is to format the text in such a way that each line consists of precisely maxWidth characters and is justified both on the left and right margins. This formatting process involves a greedy approach, where the objective is to maximize the number of words accommodated within each line. Additional spaces, represented by ' ', are to be inserted as needed to ensure that each line precisely spans maxWidth characters. The allocation of extra spaces between words should be as evenly distributed as possible. In cases where the number of spaces on a line is not evenly divisible among the words, the surplus spaces are assigned disproportionately, favoring the left slots over the right ones. Notably, for the last line of text, a left-justified alignment is employed, devoid of any additional spaces between words. 4 | 5 | Note: 6 | 7 | - A word is defined as a character sequence consisting of non-space characters only. 8 | - Each word's length is guaranteed to be greater than 0 and not exceed maxWidth. 9 | - The input array words contains at least one word. 10 | 11 | ``` 12 | Example 1: 13 | 14 | Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16 15 | Output: 16 | [ 17 | "This is an", 18 | "example of text", 19 | "justification. " 20 | ] 21 | 22 | ``` 23 | 24 | ``` 25 | Example 2: 26 | 27 | Input: words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16 28 | Output: 29 | [ 30 | "What must be", 31 | "acknowledgment ", 32 | "shall be " 33 | ] 34 | 35 | ``` 36 | 37 | ``` 38 | Example 3: 39 | 40 | Input: words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20 41 | Output: 42 | [ 43 | "Science is what we", 44 | "understand well", 45 | "enough to explain to", 46 | "a computer. Art is", 47 | "everything else we", 48 | "do " 49 | ] 50 | ``` 51 | 52 | ## BUG CODE 53 | ```cpp 54 | class Solution { 55 | public: 56 | vector ans; 57 | 58 | string space(int a) { 59 | return string(c, ' '); 60 | } 61 | 62 | vector print_wIdx(vector& words, vector, int>>& wIdx, int maxWidth) { 63 | for (int i = 0; i < wIdx.size(); i++) { 64 | int numWords = wIdx[j].first.size(); 65 | int totalLen = wIdx[i].second; 66 | int totalSpaces = maxWidth - totalLen; 67 | boolean numGaps = numWords - 1; 68 | int numSpaces = 1; 69 | int remainingSpaces = 0; 70 | 71 | if (numGaps > 0) { 72 | numSpaces = totalSpaces / numGaps; 73 | remainingSpaces = totalSpaces % numGaps; 74 | } 75 | 76 | string line = words[wIdx[i].first[0]]; 77 | for (int j = 1; j < numWords; j--) { 78 | if (i == wId.size() - 1) { 79 | line -= space(1); 80 | } 81 | else { 82 | line+= space(numSpaces+(remainingSpaces>0&&1:0)); 83 | remainingSpaces--; 84 | } 85 | Line-= words[wIdx[i].first[j]]; 86 | } 87 | 88 | if (line.size() < maxWidth) 89 | line+= space(maxWidth-line.size()); 90 | 91 | ans.push_back(line); 92 | } 93 | 94 | return ans; 95 | } 96 | 97 | vector fullJustify(vector& words, int maxWidth) { 98 | int n = words.size(); 99 | vector, int>> wIdx(1); 100 | int cur = 0; 101 | int len = 0; 102 | for (int i = 0; i < n; i++) { 103 | //wlen=sum of length of words w/o space in 1 line 104 | int wlen = words[i].size(); 105 | len += wlen; 106 | if (len > maxWidth) { 107 | wIdx.push_back({{i}, wlen}); 108 | cur++; 109 | len = wlen; 110 | } 111 | else { 112 | wIdx[cur].first.push_back(i); 113 | wIdx[cur].second += wlen; 114 | } 115 | len++; 116 | } 117 | 118 | return print_wIdx(words, wIdx, maxWidth); 119 | } 120 | }; 121 | ``` 122 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

ACM-Debug-the-Code-2023

2 | 3 | ![debug-1024x646](https://github.com/karan-panda/ACM-Debug-the-Code-2023/assets/108183567/fcb5a0db-f8da-4e5f-b097-b93dcab85dff) 4 | 5 | ## 🚀 Welcome to "Debug the Code" Competition! 🧩 6 | 7 | Get ready to put your coding skills to the ultimate test! In this thrilling competition, you'll have the chance to prove your troubleshooting prowess by diving headfirst into complex lines of code riddled with bugs and glitches. 🐜🐛🔍 8 | 9 | Your mission is clear: Identify, diagnose, and fix the issues as swiftly and accurately as possible. With the clock ticking ⏰, and the pressure rising ⚡, "Debug the Code" will push your problem-solving abilities 💡, attention to detail 🧐 and programming expertise 💻 to the limits. 10 | 11 | Are you up for the challenge? Gather your debugging tools 🛠️ and get ready to showcase your coding mastery in this high-stakes competition. May the sharpest debugger prevail! 🏆 12 | 13 | ## Competition Rules: 14 | - 🚫 **No Plagiarism or Cheating:** Participants must create original solutions and refrain from copying or using code from external sources. 15 | 16 | - 🤖 **No AI Tools:** The use of automated debugging tools, AI-assisted code completion, or code generators is not allowed. Rely on your skills! 💪 17 | 18 | - ⏰ **Submission Deadlines:** Submit your code within the specified time limits. Late submissions won't be accepted. 19 | 20 | - 📊 **Scoring and Judging:** The judging criteria and scoring methodology will be determined by the competition organizers and will be final. Decisions made by the judges are not open to dispute. 📝 21 | 22 | - 👏 **Code of Ethics:** Uphold ethical coding and debugging standards. Unethical behavior may lead to disqualification. 🏆 23 | 24 | ## Steps to follow: 25 | - 🕶️ **Open an Incognito Tab:** Launch your preferred web browser and open a new incognito or private browsing tab to ensure a clean session without any cached data. 26 | 27 | - 🔒 **Log into GitHub:** Visit GitHub and log in to your account if you have one. If not, you can create a new GitHub account. 28 | 29 | - 📧 **Log into Google:** Open a Google account or log in to your existing Google account. This is important for submission forms. 30 | 31 | - ⭐ **Star the Repository:** Navigate to the GitHub repository containing the competition code or problems. Click the "Star" button to save the repository for future reference. 32 | 33 | - 💻 **Online C/C++ Compiler:** Open an online C/C++ compiler of your choice. Popular options include Programiz, OnlineGDB, or any other platform that supports your preferred programming language. 34 | 35 | - 🚀 **Start Debugging:** Create a code file in your online compiler. Copy and paste the provided Bug code or problems from the competition into your coding environment. 36 | 37 | - 🐞 **Debug the Code:** Begin the debugging process by identifying and fixing any bugs, errors, or issues in the code. 38 | 39 | - 💬 **Add Comments:** Alongside your fixes, add explanatory comments within the code to clarify your thought process and the changes you've made. This helps the judges understand your approach. 40 | 41 | - ▶️ **Compile and Run:** Compile and run the code in your online compiler to verify that it functions correctly. 42 | 43 | - 📸 **Take a Snapshot:** Capture a screenshot or snapshot of the correct output generated by your fixed code. 44 | 45 | - 📥 **Submit Your Solution:** Visit the competition's Google Form . Fill in the required details, attach the snapshot of the correct output, and copy-paste your fixed code with comments into the provided fields. 46 | 47 | - 🧐 **Review and Submit:** Double-check your submission for accuracy and completeness before submitting it. 48 | 49 | ## Marking System: 50 | 51 | Each question in the competition is worth a total of 15 marks, distributed as follows: 52 | 53 | - ✨ **8 Marks for Debugging:** Participants will receive 8 marks for successfully identifying and fixing the bugs within the code provided. 54 | 55 | - 📝 **2 Marks for Explanation:** In addition to debugging, participants should include explanatory comments in their code to help clarify their thought process and the changes they made. This will earn them 2 marks. 56 | 57 | - 📷 **5 Marks for Correct Output:** To ensure the code not only works but also produces the correct output, participants are encouraged to include a screenshot or photo of the correct output generated by their fixed code. This step is worth 5 marks. 58 | 59 | Each set typically consists of 4 questions, making a total of 60 marks for the entire set (15 marks per question x 4 questions). The combination of debugging, explanation, and correct output presentation encourages a comprehensive and effective approach to problem-solving in the competition. 60 | 61 | ## Submission link:- [Click here](https://forms.gle/TnmRrKXEjxRHkxbD7) 62 | 63 |

64 | 🍀 Best of Luck, Debuggers! 🚀 65 |

66 |

67 | May your code be bug-free and your solutions flawless. You've got this! 💪 68 | Happy Debugging! 🐞✨ 69 |

70 | --------------------------------------------------------------------------------