# Programming Examples

## C++ Program for sum of arithmetic series

#include<bits/stdc++.h>
using namespace std;
// Function to find sum of series.
float sum(float a, float d, int n) {
float sum = 0;
for (int i=0;i<n;i++) {
sum = sum + a;
a = a + d;
}
return sum;
}
int main() {
int n = 10;
float a = 1.5, d = 0.5;
cout<<"sum of series A.P is : "<<sum(a, d, n);
return 0;
}

## Find Corners of Rectangle using mid points in C++

#include &lt;iostream&gt;\r\n#include &lt;cmath&gt;\r\nusing namespace std;\r\nclass Point {\r\npublic:\r\nfloat x, y;\r\nPoint(float a = 0.0f, float b = 0.0f) {\r\nx = a, y = b;\r\n}\r\n};\r\nvoid printCorners(Point p, Point q, float l) {\r\nPoint a, b, c, d;\r\nif (p.x == q.x) {\r\na.x = p.x - (l/2.0);\r\nd.x = p.x + (l/2.0);\r\na.y = d.y = p.y;\r\nb.x = q.x - (l/2.0);\r\nc.x = q.x + (l/2.0);\r\nb.y = c.y = q.y;\r\n}else if (p.y == q.y) {\r\na.y = p.y - (l/2.0);\r\nd.y = p.y + (l/2.0);\r\na.x = d.x = p.x;\r\nb.y = q.y - (l/2.0);\r\nc.y = q.y + (l/2.0);\r\nb.x = c.x = q.x;\r\n}else{\r\nfloat m = (p.x-q.x)/float(q.y-p.y);\r\nfloat dx = (l /sqrt(1+(m*m))) *0.5 ;\r\nfloat dy = m*dx;\r\na.x = p.x - dx;\r\na.y = p.y - dy;\r\nd.x = p.x + dx;\r\nd.y = p.y + dy;\r\nb.x = q.x - dx;\r\nb.y = q.y - dy;\r\nc.x = q.x + dx;\r\nc.y = q.y + dy;\r\n}\r\ncout &lt;&lt; &quot;A (&quot; &lt;&lt; a.x &lt;&lt; &quot;, &quot; &lt;&lt; a.y &lt;&lt; &quot;)\\\\n&quot;\r\n&lt;&lt; &quot;B (&quot; &lt;&lt; b.x &lt;&lt; &quot;, &quot; &lt;&lt; b.y &lt;&lt; &quot;)\\\\n&quot;\r\n&lt;&lt; &quot;C (&quot; &lt;&lt; c.x &lt;&lt; &quot;, &quot; &lt;&lt; c.y &lt;&lt; &quot;)\\\\n&quot;\r\n&lt;&lt; &quot;D (&quot; &lt;&lt; d.x &lt;&lt; &quot;, &quot; &lt;&lt; d.y &lt;&lt; &quot;)\\\\n&quot;;\r\n}\r\nint main() {\r\nPoint p(1, 1), q(-1, -1);\r\nprintCorners(p, q, 2*sqrt(2));\r\n}

## Minimum Index Sum for Common Elements of Two Lists in C++

#include &lt;iostream&gt;\r\n#include &lt;vector&gt;\r\n#include &lt;algorithm&gt;\r\nusing namespace std;\r\nvector&lt;string&gt; commonInterest(string set1[], int n1, string set2[],\r\nint n2) {\r\nvector&lt;string&gt; v(min(n1, n2));\r\nvector&lt;string&gt;::iterator it;\r\n// Sorting both the list\r\nsort(set1, set1 + n1);\r\nsort(set2, set2 + n2);\r\nit = set_intersection(set1, set1 + n1, set2, set2 + n2,\r\nv.begin());\r\nreturn v;\r\n}\r\nint main() {\r\nstring first[] = { &quot;Kolkata&quot;, &quot;Hyderabad&quot;, &quot;Chennai&quot;,\r\n&quot;Delhi&quot; };\r\nint n1 = sizeof(first) / sizeof(first[0]);\r\nstring second[] = { &quot;Mumbai&quot;, &quot;Kolkata&quot;, &quot;Durgapur&quot;, &quot;Delhi&quot; };\r\nint n2 = sizeof(second) / sizeof(second[0]);\r\nvector&lt;string&gt; v = commonInterest(first, n1, second, n2);\r\ncout &lt;&lt; &quot;Common cities: &quot;;\r\nfor (int i = 0; i &lt; v.size(); i++)\r\ncout &lt;&lt; \\\' \\\' &lt;&lt; v[i];\r\ncout &lt;&lt; endl;\r\n}

## Minimum Cost To Make Two Strings Identical in C++

#include &lt;iostream&gt;\r\nusing namespace std;\r\nbool isRepresentedInDDigits(int num, int d, int base) {\r\nif (d==1 &amp;&amp; num &lt; base)\r\nreturn true;\r\nif (d &gt; 1 &amp;&amp; num &gt;= base)\r\nreturn isRepresentedInDDigits(num/base, --d, base);\r\nreturn false;\r\n}\r\nbool checkNumber(int num, int d) {\r\n// Check for all bases one by one\r\nfor (int base=2; base&lt;=32; base++)\r\nif (isRepresentedInDDigits(num, d, base))\r\nreturn true;\r\nreturn false;\r\n}\r\nint main() {\r\nint num = 8;\r\nint dig = 2;\r\nif(checkNumber(num, dig))\r\ncout &lt;&lt; &quot;Can be represented&quot;;\r\nelse\r\ncout &lt;&lt; &quot;Can not be represented&quot;;\r\n}

## Minimum Cost to make two Numeric Strings Identical in C++

#include &lt;iostream&gt;\r\nusing namespace std;\r\nint longest_common_subsequence(int dp[101][101], string a, string b, int a_len,\r\nint b_len) {\r\nfor (int i = 0; i &lt; 100; i++)\r\nfor (int j = 0; j &lt; 100; j++)\r\ndp[i][j] = -1;\r\nif (a_len &lt; 0 || b_len &lt; 0) {\r\nreturn 0;\r\n}\r\nif (dp[a_len][b_len] != -1)\r\nreturn dp[a_len][b_len];\r\nint res = 0;\r\nif (a[a_len] == b[b_len]) {\r\nres = int(a[a_len] - 48) + longest_common_subsequence(dp, a, b, a_len - 1,\r\nb_len - 1);\r\n} else\r\nres = max(longest_common_subsequence(dp, a, b, a_len - 1, b_len),\r\nlongest_common_subsequence(dp, a, b, a_len, b_len - 1));\r\ndp[a_len][b_len] = res;\r\nreturn res;\r\n}\r\nint minCost(string str) {\r\nint cost = 0;\r\nfor (int i = 0; i &lt; str.length(); i++)\r\ncost += int(str[i] - 48);\r\nreturn cost;\r\n}\r\nint main() {\r\nstring a, b;\r\na = &quot;6789&quot;;\r\nb = &quot;7859&quot;;\r\nint dp[101][101];\r\ncout &lt;&lt; &quot;Minimum Cost to make these two numbers identical: &quot; &lt;&lt;\r\n(minCost(a) + minCost(b) - 2 * longest_common_subsequence(dp, a, b, a.length()\r\n- 1, b.length() - 1));\r\nreturn 0;\r\n}

## Median and Mode using Counting Sort in C++

#include &lt;iostream&gt;\r\nusing namespace std;\r\nbool isRepresentedInDDigits(int num, int d, int base) {\r\nif (d==1 &amp;&amp; num &lt; base)\r\nreturn true;\r\nif (d &gt; 1 &amp;&amp; num &gt;= base)\r\nreturn isRepresentedInDDigits(num/base, --d, base);\r\nreturn false;\r\n}\r\nbool checkNumber(int num, int d) {\r\n// Check for all bases one by one\r\nfor (int base=2; base&lt;=32; base++)\r\nif (isRepresentedInDDigits(num, d, base))\r\nreturn true;\r\nreturn false;\r\n}\r\nint main() {\r\nint num = 8;\r\nint dig = 2;\r\nif(checkNumber(num, dig))\r\ncout &lt;&lt; &quot;Can be represented&quot;;\r\nelse\r\ncout &lt;&lt; &quot;Can not be represented&quot;;\r\n}

## Memory allocation in C++

#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\nint main() {\r\nunion my_union {\r\nint i;\r\nfloat f;\r\nchar c;\r\n};\r\nunion my_union* u;\r\nu = (union my_union*)malloc(sizeof(union my_union));\r\nu-&gt;f = 20.60f;\r\nprintf(&quot;%f&quot;, u-&gt;f);\r\n}

## Array using pair STL in C++

#include &lt;iostream&gt;\r\n#include &lt;algorithm&gt;\r\n#include &lt;map&gt;\r\nusing namespace std;\r\ntemplate &lt;class T&gt;\r\nvoid display(T arr[], int n) {\r\nfor (int i = 0; i &lt; n; i++)\r\ncout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;\r\n}\r\nvoid sortUsingSecondArr(int A1[], char A2[], int n){\r\npair&lt;int, char&gt; pair_arr[n];\r\nfor (int i = 0; i &lt; n; i++) {\r\npair_arr[i].first = A1[i];\r\npair_arr[i].second = A2[i];\r\n}\r\nsort(pair_arr, pair_arr + n);\r\nfor (int i = 0; i &lt; n; i++) {\r\nA1[i] = pair_arr[i].first;\r\nA2[i] = pair_arr[i].second;\r\n}\r\n}\r\nint main() {\r\nint n = 10;\r\nint A1[] = {2, 1, 5, 4, 9, 3, 6, 7, 10, 8};\r\nchar A2[] = {\\\'A\\\', \\\'B\\\', \\\'C\\\', \\\'D\\\', \\\'E\\\', \\\'F\\\', \\\'G\\\', \\\'H\\\', \\\'I\\\', \\\'J\\\'};\r\ncout &lt;&lt; &quot;Before Sorting: &quot;&lt;&lt;endl;\r\ncout &lt;&lt; &quot;First Array : &quot;; display(A1, n);\r\ncout &lt;&lt; &quot;\\\\nSecond Array: &quot;; display(A2, n);\r\nsortUsingSecondArr(A1, A2, n);\r\ncout &lt;&lt; &quot;\\\\n\\\\nAfter Sorting: &quot;&lt;&lt;endl;\r\ncout &lt;&lt; &quot;First Array : &quot;; display(A1, n);\r\ncout &lt;&lt; &quot;\\\\nSecond Array: &quot;; display(A2, n);\r\n}

## Sort the array of strings in C++

#include &lt;iostream&gt;\r\n#include &lt;algorithm&gt;\r\n#include &lt;unordered_map&gt;\r\n#include &lt;vector&gt;\r\nusing namespace std;\r\nunordered_map&lt;char, int&gt; char_map;\r\nbool compare(string c1, string c2) {\r\nfor (int i = 0; i &lt; min(c1.size(), c2.size()); i++) {\r\nif (char_map[c1[i]] == char_map[c2[i]])\r\ncontinue;\r\nreturn char_map[c1[i]] &lt; char_map[c2[i]];\r\n}\r\nreturn c1.size() &lt; c2.size();\r\n}\r\nint main() {\r\nstring str = &quot;pigvxbskyhqzelutoacfjrndmw&quot;;\r\nvector&lt;string&gt; v{ &quot;hello&quot;, &quot;programming&quot;, &quot;science&quot;, &quot;computer&quot;,\r\n&quot;india&quot; };\r\nchar_map.clear();\r\nfor (int i = 0; i &lt; str.size(); i++)\r\nchar_map[str[i]] = i;\r\nsort(v.begin(), v.end(), compare);\r\n// Print the strings after sorting\r\nfor (auto x : v)\r\ncout &lt;&lt; x &lt;&lt; &quot; &quot;;\r\n}

## Sort elements of the array that occurs in between multiples of K in C++

#include &lt;iostream&gt;\r\n#include &lt;algorithm&gt;\r\nusing namespace std;\r\nvoid display(int arr[], int n) {\r\nfor (int i = 0; i &lt; n; i++)\r\ncout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;\r\n}\r\nvoid sortBetweenMultipleOfK(int arr[], int n, int k) {\r\nint prev_index = -1;\r\nfor (int i = 0; i &lt; n; i++) {\r\nif (arr[i] % k == 0) {\r\nif (prev_index != -1) //check whether that is not the first multiple of k\r\nsort(arr + prev_index + 1, arr + i);\r\nprev_index = i;\r\n}\r\n}\r\n}\r\nint main() {\r\nint arr[] = {2, 13, 3, 1, 21, 7, 8, 13, 12};\r\nint n = sizeof(arr) / sizeof(arr[0]);\r\nint k = 2;\r\ncout &lt;&lt; &quot;Before Sort: &quot;; display(arr, n);\r\nsortBetweenMultipleOfK(arr, n, k);\r\ncout &lt;&lt; &quot;\\\\nAfter Sort : &quot;; display(arr, n);\r\n}