diff --git a/Week1/Count Negative Nos/CharanjeevKaur.txt b/Week1/Count Negative Nos/CharanjeevKaur.txt new file mode 100644 index 0000000..aa537cf --- /dev/null +++ b/Week1/Count Negative Nos/CharanjeevKaur.txt @@ -0,0 +1,22 @@ +class Solution { +public: + int countNegatives(vector>& grid) + { + int m = grid.size(); + int n = 0, count = 0; + + for(int i = m-1;i >= 0;i--) + { + n = grid[i].size(); + for(int j = n-1;j >= 0;j--) + + if(grid[i][j] >= 0) + break; + else + count++; + + } + + return count; + } +}; \ No newline at end of file diff --git a/Week1/Missing no/CharanjeevKaur.txt b/Week1/Missing no/CharanjeevKaur.txt new file mode 100644 index 0000000..b78b704 --- /dev/null +++ b/Week1/Missing no/CharanjeevKaur.txt @@ -0,0 +1,15 @@ +class Solution +{ + public: + int missingNumber(vector& nums) + { + int n=nums.size(); + + int sum = 0; + for(int i = 0;i < n; i++) + sum += nums[i]; + + int x= n*(n+1)/2; + return x-sum; + } +}; \ No newline at end of file diff --git a/Week1/Monotonic Array/CharanjeevKaur.txt b/Week1/Monotonic Array/CharanjeevKaur.txt new file mode 100644 index 0000000..ebf3f0c --- /dev/null +++ b/Week1/Monotonic Array/CharanjeevKaur.txt @@ -0,0 +1,25 @@ +class Solution { +public: + bool isMonotonic(vector& A) + { + int n = A.size(); + int count_inc = 1, count_dec = 1; + + if(n == 1 || n == 2) + return true; + + for(int i = 0; i < n-1; i++) + { + if(A[i] <= A[i+1]) + count_inc++; + + if(A[i] >= A[i+1]) + count_dec++; + } + + if(count_inc == n || count_dec == n) + return true; + else + return false; + } +}; \ No newline at end of file diff --git a/Week1/Pivot Index/CharanjeevKaur.txt b/Week1/Pivot Index/CharanjeevKaur.txt new file mode 100644 index 0000000..c06af5b --- /dev/null +++ b/Week1/Pivot Index/CharanjeevKaur.txt @@ -0,0 +1,21 @@ +class Solution { +public: + + int pivotIndex(vector& nums) + { + int lsum = 0, rsum = 0, n =nums.size(); + for(int i = 0;i < n; i++) + rsum += nums[i]; + + for(int i = 0;i < n; i++) + { + rsum -= nums[i]; + if(rsum == lsum) + return i; + lsum += nums[i]; + } + + return -1; + + } +}; \ No newline at end of file diff --git a/Week1/Remove Duplicates/CharanjeevKaur.txt b/Week1/Remove Duplicates/CharanjeevKaur.txt new file mode 100644 index 0000000..e912973 --- /dev/null +++ b/Week1/Remove Duplicates/CharanjeevKaur.txt @@ -0,0 +1,19 @@ +class Solution +{ + public: + int removeDuplicates(vector& nums) + { + int j = 1, n = nums.size(); + if(n == 0) + return 0; + + for(int i = 0; i < n-1; i++) + if(nums[i] != nums[i+1]) + { + nums[j] = nums[i + 1]; + j++; + } + + return j; + } +}; \ No newline at end of file diff --git a/Week1/Rotate Array/CharanjeevKaur.txt b/Week1/Rotate Array/CharanjeevKaur.txt new file mode 100644 index 0000000..620c9c5 --- /dev/null +++ b/Week1/Rotate Array/CharanjeevKaur.txt @@ -0,0 +1,37 @@ +#include +#include + +using namespace std; + +class Solution { + +public: + void rotate(vector& nums, int k) + { + k=k%nums.size(); + reverse(nums, 0, nums.size()-1); + reverse(nums, 0, k-1); + reverse(nums, k, nums.size()-1); + } + + void reverse(vector& nums1, int i, int j) + { + + while(i& nums2,int x,int y) + { + int temp; + temp=nums2[x]; + nums2[x]=nums2[y]; + nums2[y]=temp; + } + +}s; diff --git a/Week1/Xor operation/CharanjeevKaur.txt b/Week1/Xor operation/CharanjeevKaur.txt new file mode 100644 index 0000000..12cbd82 --- /dev/null +++ b/Week1/Xor operation/CharanjeevKaur.txt @@ -0,0 +1,13 @@ +class Solution { +public: + int xorOperation(int n, int start) + { + int result = start; + while(--n) + { + result = result ^ (start + 2 * n); + } + + return result; + } +}; \ No newline at end of file