diff --git a/C++/h1.cpp b/C++/h1.cpp index 2d23603..743299a 100644 --- a/C++/h1.cpp +++ b/C++/h1.cpp @@ -3,7 +3,7 @@ // ie. if the string given is 'djhfacdadcaop' the longest palindrom is 'acdadca' // which should be the return value for the function longest pallindrome(char* s). -#include +#include//librar using namespace std; void printSubStr( @@ -31,15 +31,15 @@ int longestPalSubstr(string str) if (str[i] == str[i + 1]) { table[i][i + 1] = true; start = i; - maxLength = 2; + maxLength =2; } } for (int k = 3; k <= n; ++k) { - for (int i = 0; i < n - k; ++i) { + for (int i = 0; i < n - k+1; ++i) {// n-k to n-k+1 int j = i + k - 1; - if (table[i + 1][j] && str[i] == str[j]) { + if (table[i + 1][j-1] && str[i] == str[j]) {//j-1 table[i][j] = true; if (k > maxLength) { @@ -61,7 +61,7 @@ int longestPalSubstr(string str) // Driver Code int main() { - string str = "forgeeksskeegfor"; + string str = "forgeeksskeegrof"; cout << longestPalSubstr(str) << endl; return 0; } diff --git a/C++/h1.exe b/C++/h1.exe new file mode 100644 index 0000000..4d878ff Binary files /dev/null and b/C++/h1.exe differ diff --git a/C++/h2.cpp b/C++/h2.cpp index d29572a..56433ea 100644 --- a/C++/h2.cpp +++ b/C++/h2.cpp @@ -18,16 +18,19 @@ int count(int coins[], int n, int sum) for (i = 1; i < sum + 1; i++) { for (j = 0; j < n; j++) { - x = (i - coins[j] >= 0) ? table[j][i - coins[j]] : 1; + //x = (i - coins[j] >= 0) ? table[j][i - coins[j]] : 1; + x = (i - coins[j] >= 0) ? table[i - coins[j]][j]: 0; + - y = (j > 1) ? table[j - 1][i] : 1; + //y = (j > 1) ? table[j - 1][i] : 1; + y = (j >= 1) ? table[i][j - 1] : 0; table[i][j] = x + y; } } - return table[sum-1][n - 1]; + return table[sum][n-1]; //table[sum-1][n - 1]; table[sum][n-1] } // Driver Code diff --git a/C++/h2.exe b/C++/h2.exe new file mode 100644 index 0000000..06308a6 Binary files /dev/null and b/C++/h2.exe differ diff --git a/C++/m1.cpp b/C++/m1.cpp index caa5f1c..5507b4d 100644 --- a/C++/m1.cpp +++ b/C++/m1.cpp @@ -8,7 +8,7 @@ class Solution public: bool isPalindrome(string s) { - int i = 0, j = s.size(); + int i = 0, j = s.size()-1; while (i < j) { @@ -34,7 +34,7 @@ class Solution { if (isPalindrome(s.substr(i, j))) { - if (result.size() < j) + if (result.size() < s.substr(i,j).size()) result = s.substr(i, j + 1); } } diff --git a/C++/m2.cpp b/C++/m2.cpp index c42208e..eb0af00 100644 --- a/C++/m2.cpp +++ b/C++/m2.cpp @@ -15,7 +15,7 @@ class Solution { vector> output; for (int i = 0; i < nums.size(); i++){ int j = i + 1; - int k = i+j; + int k = nums.size()-1; while (j < k) { int sum = nums[i] + nums[j] + nums[k]; if (sum == target) { @@ -28,8 +28,15 @@ class Solution { } } } - for(auto triplets : s) - output.push_back(triplets); - return output; + for(auto triplets : s){ + cout<nums={1,2,-3,1,-2,1}; + Solution s; + s.threeSum(nums); +} diff --git a/C++/m2.exe b/C++/m2.exe new file mode 100644 index 0000000..4a661be Binary files /dev/null and b/C++/m2.exe differ diff --git a/C++/m3.cpp b/C++/m3.cpp index d6995fd..e35a78d 100644 --- a/C++/m3.cpp +++ b/C++/m3.cpp @@ -6,7 +6,8 @@ using namespace std; char *initialize() { char string[80]; // string = "hello" - char* ptr = string; + char* ptr ; + ptr = string; return ptr; } @@ -16,8 +17,10 @@ void do_something_with(char* myval){ main() { cout << "enter value for myval" << endl; - char *myval = initialize(); + char *myval ; + myval = initialize(); cin >> *myval; + cout<<*myval; cout << "Variable intialized" << endl; do_something_with(myval); } diff --git a/C++/m3.exe b/C++/m3.exe new file mode 100644 index 0000000..5045bb1 Binary files /dev/null and b/C++/m3.exe differ diff --git a/C++/m5.cpp b/C++/m5.cpp index 39456b1..ecade9c 100644 --- a/C++/m5.cpp +++ b/C++/m5.cpp @@ -29,13 +29,13 @@ vector spiralOrder(vector >& matrix) int newX = x + dr[di]; int newY = y + dc[di]; - if (0 <= newX || newX < m && 0 <= newY || newY < n + if (0 <= newX && newX < m && 0 <= newY && newY < n && !seen[newX][newY]) { x = newX; y = newY; } else { - di = (di + 1) / 4; + di = (di + 1) % 4; x += dr[di]; y += dc[di]; } diff --git a/C++/m6.cpp b/C++/m6.cpp index 5524bce..b6716bb 100644 --- a/C++/m6.cpp +++ b/C++/m6.cpp @@ -4,7 +4,7 @@ #include int main(){ - std :: string s = "Hello World"; + std :: string s = NULL; for(int i=0;i < s.length(); i++){ s[i] = toupper(s[i]); diff --git a/C++/m6.exe b/C++/m6.exe new file mode 100644 index 0000000..74e3cc9 Binary files /dev/null and b/C++/m6.exe differ diff --git a/C++/s1.cpp b/C++/s1.cpp index 95629da..2e0b066 100644 --- a/C++/s1.cpp +++ b/C++/s1.cpp @@ -6,7 +6,7 @@ using namespace std; int reverse(int x) { long r=0; while(x){ - r=r*10+x/10; + r=r*10+x%10; x=x/10; } return r; diff --git a/C++/s1.exe b/C++/s1.exe new file mode 100644 index 0000000..7242658 Binary files /dev/null and b/C++/s1.exe differ diff --git a/C++/s2.cpp b/C++/s2.cpp index d995ee5..9dd7b18 100644 --- a/C++/s2.cpp +++ b/C++/s2.cpp @@ -12,7 +12,7 @@ int main(){ for(int i=0; i < myVec.size(); i++){ std :: cout << myVec[i] << std :: endl; } - std :: cout << "Element at index 3: "<< myVec.at(3) << std::endl; + std :: cout << "Element at index 3: "<< myVec.at(2) << std::endl; return 0; } diff --git a/C++/s2.exe b/C++/s2.exe new file mode 100644 index 0000000..f0ce2fc Binary files /dev/null and b/C++/s2.exe differ diff --git a/C++/s3.cpp b/C++/s3.cpp index 854ed28..f30592a 100644 --- a/C++/s3.cpp +++ b/C++/s3.cpp @@ -18,7 +18,7 @@ void ineedj(void) { } main() { - int j; + //int j; j = foo(i); ineedj(); } diff --git a/C++/s4.cpp b/C++/s4.cpp index 727e620..eeb2bcd 100644 --- a/C++/s4.cpp +++ b/C++/s4.cpp @@ -4,16 +4,16 @@ #include #include using namespace std; -#define Random(n) random()%n +#define Random(n) random(n)%n -int random(){ +int random(int n){ srand(time(0)); - return rand(); + return rand()%n; } int main(void){ - int i=6, j=7; + int i=6, j=9; int val = Random(j-i+1); cout << val << endl; diff --git a/C++/s4.exe b/C++/s4.exe new file mode 100644 index 0000000..6c64f07 Binary files /dev/null and b/C++/s4.exe differ diff --git a/C++/s5.exe b/C++/s5.exe new file mode 100644 index 0000000..dad99c9 Binary files /dev/null and b/C++/s5.exe differ diff --git a/C++/s6.cpp b/C++/s6.cpp index 736760a..9e4ee7a 100644 --- a/C++/s6.cpp +++ b/C++/s6.cpp @@ -1,10 +1,11 @@ // to find the largest sum of subarray #include +#include using namespace std; int maxSubArraySum(int a[], int size) { - int max_so_far = INT_MIN, max_ending_here; + int max_so_far = 0, max_ending_here=0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; @@ -16,3 +17,8 @@ int maxSubArraySum(int a[], int size) } return max_so_far; } +int main(){ + int arr[]={-5,4,3,-2,1}; + cout<