Posts

Showing posts from October, 2020

Largest Sum Contiguous Subarray

  #include <iostream> #include <vector> using namespace std; int partial_sum(vector<int>& sum, int x, int y) { return sum[y] - sum[x - 1]; } void sum_array(vector<int>& v, vector<int>& sum) { sum.push_back(v[0]); for (auto i = 1; i < v.size(); i++) { sum.push_back(sum[i - 1] + v[i]); } } int max_sum_subarray(vector<int>& sum) { int minSum = 0; int result = INT_MIN; for (int r = 0; r < sum.size(); r++) { if (sum[r] - minSum > result) { result = sum[r] - minSum; } if (sum[r] < minSum) { minSum = sum[r]; } } return result; } int main() { vector<int> v = { -2, -3, 4, -1, -2, 1, 5, -3 }; vector <int> sum; sum_array(v, sum); cout << max_sum_subarray(sum); return 0; }

Partial Sum

  #include <iostream> #include <vector> using namespace std; int partial_sum(vector<int>& sum, int x, int y) { return sum[y] - sum[x - 1]; } void sum_array(vector<int>& v, vector<int>& sum) { sum.push_back(v[0]); for (auto i = 1; i < v.size(); i++) { sum.push_back(sum[i - 1] + v[i]); } } int main() { vector<int> v = { 7, 3, 5, 6, 7, 9, 3, 5, 11 }; vector <int> sum; sum_array(v, sum); // for (auto i = sum.begin(); i != sum.end(); i++) // cout << *i << " "; cout << partial_sum(sum, 3, 7); return 0; }

Advancing through an Array

  #include <iostream> #include <vector> using namespace std; bool canReachEnd(vector<int>& v) { int last_index = v.size()-1; int furthest_index = 0; for (int i = 0; i <= furthest_index && furthest_index < last_index; i++) { furthest_index = max(furthest_index, i + v[i]); } return (furthest_index >= last_index); } int main() { vector<int> v = { 3,2,0,0,2,0,1 }; bool result = canReachEnd(v); if (result) { cout << "We can reach the end of array.\n"; } else { cout << "We can't reach the end of array.\n"; } }

Dutch Flag Problem

  #include <iostream> #include <vector> using namespace std; void dutchFlag(vector<int>& v, int pivot) { int smaller = 0; int equal = 0; int larger = v.size() - 1; while (equal < larger) { if (v[equal] < pivot) { swap(v[smaller++], v[equal++]); } else if (v[equal] == pivot) { equal++; } else { swap(v[larger--], v[equal]); } } } /*void dutchFlag(vector<int>& v, int pivot) { int smaller = 0; for (int i = 0; i < v.size(); i++) { if (v[i] < pivot) { swap(v[i], v[smaller++]); } } int larger = v.size() - 1; for (int i = v.size() - 1; i >= 0 && v[i] >= pivot; i--) { if (v[i] > pivot) { swap(v[i], v[larger--]); } } }*/ /* void dutchFlag(vector<int>& v, int pivot) { for (int i = 0; i < v.size(); i++) { for (int j = i + 1; j ...

Even Odd problem

#include <iostream> #include <vector> using namespace std; void evenOdd(vector<int> &v) { int next_even = 0; int next_odd = v.size() - 1; while (next_even < next_odd) { if (v[next_even] % 2 == 1) { swap(v[next_even], v[next_odd--]); } else { next_even++; } } } int main() { vector<int> v = { 1, 2, 3 , 4, 5, 6, 7}; evenOdd(v); for (auto i = v.begin(); i != v.end(); i++) cout << *i << " "; return 0; }

Swap bits at ith and jth position of integer n

  #include <iostream> using namespace std; //function to swap bits of an integer n unsigned swapBit(unsigned int n, unsigned int i, unsigned int j) { if(( n & (1 << (i-1))) != (n & (1 << (j-1)))){ n ^= (1 << (i-1)) ^ (1 << (j-1)); } return n; } int main() { unsigned int n = 7, i = 1, j = 4 ; unsigned int result = swapBit(n,i,j); cout << "After swapping the bit at position "<< i <<" and "<< j << " of integer "<< n << ", we get "<< result<<"." << endl; return 0; }

Calculate number of set bits in integer n

  #include <iostream> using namespace std; //function to calculate number of set bit of an integer n unsigned isolateRightMostSetBit(unsigned int n) { unsigned int count = 0; while(n){ n &= n-1; count ++; } return count; } int main() { unsigned int n = 7; unsigned int result = isolateRightMostSetBit(n); cout << "Number of set bits in integer "<< n << ", we get "<< result<<"." << endl; return 0; }

Isolate the right most set bit of integer n

  #include <iostream> using namespace std; //function to isolate rightmost set bit of integer n unsigned isolateRightMostSetBit(unsigned int n) { int result = n & ~(n - 1); return result; } int main() { unsigned int n = 10; unsigned int result = isolateRightMostSetBit(n); cout << "After isolating right most set bit of integer "<< n << ", we get "<< result<<"." << endl; return 0; }

Clear right most set bit of an integer n

  #include <iostream> using namespace std; //function to clear rightmost set bit of integer n unsigned clearRightMostSetBit(unsigned int n) { int result = n & (n - 1); return result; } int main() { unsigned int n = 8; unsigned int result = clearRightMostSetBit(n); cout << "After clearing right most set bit of integer "<< n << ", we get "<< result<<"." << endl; return 0; }

Toggle the bit at Kth position of integer n

Image
  #include <iostream> using namespace std; //function to toggle Kth bit of integer n unsigned toggleBit(unsigned int n, unsigned int k) { int result = n ^ (1 <<(k - 1)); return result; } int main() { unsigned int n = 10, k = 1; unsigned int result = toggleBit(n, k); cout << "After toggling bit of position " << k << " integer "<< n << ", we get "<< result<<"." << endl; return 0; }

Set the bit at Kth position of integer n

Image
#include <iostream> using namespace std; //function to set Kth bit of integer n unsigned setBit(unsigned int n, unsigned int k) { int result = n | (1 <<(k - 1)); return result; } int main() { unsigned int n = 10, k = 1; unsigned int result = setBit(n, k); cout << "After setting bit of position " << k << " integer "<< n << ", we get "<< result<<"." << endl; return 0; }

Clear the bit at Kth position of integer n

  #include <iostream> using namespace std; //function to clear Kth bit of integer n unsigned clearBit(unsigned int n, unsigned int k) { int result = n & ~(1 <<(k - 1)); return result; } int main() { unsigned int n = 10, k = 2; unsigned int result = clearBit(n, k); cout << "After clearing bit of position " << k << " integer "<< n << ", we get "<< result<<"." << endl; return 0; }

Checking the bit at Kth position of integer n

Image
      Hello Viewers, Our problem for the video is How to check the Kth bit of any integer n. I request you to take some time and think of the solution yourself,  Before solving the problem we need to understand how ‘and’ operator works. The bitwise and operator returns one only if both the bits are set to 1. In all the other cases it returns 0. Here is the table of bitwise and operator:- In the table you can clearly see the bitwise and operator returns one only if both flag  and value are 1. We can make a simple observation from the table. Whenever the flag is 1, the result  returned is same as the value. So if we left shift 1 to kth position and do bitwise and operation with the number  we will get our result. If our result is 0 the kth bit is 0. Otherwise, it is 1.  Let’s look at an example, Here n is 113. And k is 5. So when we shift 1 to 5th position and do bitwise and operation our result will get a  non zero value. That means 5th bit is...