# ASIGNMENT DSA 1 MC21143 sem 2

```TASK : 1
CODE : #include&lt;bits/stdc++.h&gt;
using namespace std;
int search(int a[] , int b , int n)
{
int count = 0;
for(int i=0 ; i&lt;=n;i++)
{
for(int j=0;j&lt;=n;j++)
{
if(i != j &amp;&amp; ((a[i]+a[j]) % b )==0 )
{
count += 1;
} }
}
if(count%2==0)
count /=2;
else count = (count-1)/2 +1;
return count;
}
int main()
{
int a[100];
int n,b;
cout&lt;&lt;&quot;enter how many number you have to enter in the arrray : &quot;&lt;&lt;endl;
cin&gt;&gt;n;
cout&lt;&lt;&quot;enter the numbers : &quot;&lt;&lt;endl;
for(int i=0;i&lt;n;i++)
{
cin&gt;&gt;a[i];
}
cout&lt;&lt;&quot;enter the number : &quot;&lt;&lt;endl;
cin&gt;&gt;b;
cout&lt;&lt;&quot;the number of the pairs possible is &quot;&lt;&lt;search(a , b, n);
return 0;
}
OUTPUT: enter how many number you have to enter in the arrray :
6
enter the numbers :
2
2
1
7
5
3
enter the number :
4
the number of the pairs possible is 5
--------------------------------
Process exited after 22.49 seconds with return value 0
Press any key to continue . . .
CODE:
#include &lt;bits/stdc++.h&gt;
using namespace std;
int main()
{
int i,n,SUM=0;
cout&lt;&lt;&quot;Enter size of the array :&quot;;
cin&gt;&gt;n;
int a[n];
cout&lt;&lt;&quot;ENTER MESSAGE BITS : &quot; ;
for(i=0;i&lt;n;i++)
cin&gt;&gt;a[i];
for(i=0;i&lt;n;i++){
if(a[i]==1)
SUM++;
}
cout&lt;&lt;SUM%2&lt;&lt;&quot; &quot;;
for(i=0;i&lt;n;i++)
cout&lt;&lt;a[i]&lt;&lt;&quot; &quot;;
return 0;
}
OUTPUT:
Enter size of the array :6
ENTER MESSAGE BITS : 1 0 1 0 1 1
0 1 0 1 0 1 1
--------------------------------
Process exited after 6.717 seconds with return value 0
Press any key to continue . . .
CODE:
#include &lt;bits/stdc++.h&gt;
using namespace std;
int main()
{
int i,j,a,b,c,d,e;
for(i=0;i&lt;10;i++)
{
a=i;
b=(a+1)/2;
c=10-b;
d=b+1;
e=14-c;
if((a+b+c+d+e)==30)
{
cout&lt;&lt;a&lt;&lt;b&lt;&lt;c&lt;&lt;d&lt;&lt;e;
}
}
return 0;
}
OUTPUT: 74658
--------------------------------
Process exited after 0.08334 seconds with return value 0
Press any key to continue . . .
CODE:
#include&lt;bits/stdc++.h&gt;
using namespace std;
int sorting(int a[],int n ,int l,int s)
{
int count[20];
int k;
for(int i=0;i&lt;n;i++)
{
for(int j=i;j&lt;n;j++)
{
if(a[i]&lt;a[j])
{
k = a[i];
a[i] = a[j];
a[j] = k;
}
}
}
cout&lt;&lt;&quot;The ith largest element is : &quot;&lt;&lt;endl;
cout&lt;&lt;a[l-1]&lt;&lt;endl;
cout&lt;&lt;&quot;The jth smallest element is : &quot;&lt;&lt;endl;
cout&lt;&lt;a[n-s]&lt;&lt;endl;
}
int main()
{
int a[20];
int n;
int i,j;
cout&lt;&lt;&quot;Enter the number of element you want to enter in the array : &quot;&lt;&lt;endl;
cin&gt;&gt;n;
cout&lt;&lt;&quot;Enter the array elements : &quot;&lt;&lt;endl;
for(int k=0;k&lt;n;k++)
{
cin&gt;&gt;a[k];
}
cout&lt;&lt;&quot;Enter the value of i : &quot;&lt;&lt;endl;
cin&gt;&gt;i;
cout&lt;&lt;&quot;Enter the value of j : &quot;&lt;&lt;endl;
cin&gt;&gt;j;
sorting(a,n,i,j);
return 0; }OUTPUT: Enter the number of element you want to enter in the array :
6
Enter the array elements :
5
7
9
10
2
3
Enter the value of i :
2
Enter the value of j :
3
The ith largest element is :
9
The jth smallest element is :
5
--------------------------------
Process exited after 26.48 seconds with return value 0
Press any key to continue . . .
CODE:
#include &lt;bits/stdc++.h&gt;
using namespace std;
int main()
{
int n,i,j,num;
cout&lt;&lt;&quot;Enter the size of array &quot; ;
cin&gt;&gt;n;
int a[n],f[10];
for(i=0;i&lt;n;i++)
{
cout&lt;&lt;&quot;element - &quot;&lt;&lt; i &lt;&lt;&quot; &quot;;
cin&gt;&gt;a[i];
cout&lt;&lt;endl;
}
for(i=0;i&lt;n;i++)
{
num=0;
for(j=0;j&lt;n;j++)
{
if(a[i]==a[j])
{
num++;
}
}
f[i]=num;
}
for (int i=0; i&lt;n; i++)
{
int j;
for (j=0; j&lt;i; j++)
if (a[i] == a[j])
break;
if (i == j)
cout &lt;&lt; a[i] &lt;&lt;&quot; occurs &quot;&lt;&lt; f[i]&lt;&lt; &quot; times.&quot;&lt;&lt;endl;
}
return 0;
}
OUTPUT: Enter the size of array 6
element - 0 25
element - 1 12
element - 2 43
element - 3 25
element - 4 43
element - 5 25
25 occurs 3 times.
12 occurs 1 times.
43 occurs 2 times.
--------------------------------
Process exited after 40.45 seconds with re
CODE:
#include&lt;bits/stdc++.h&gt;
using namespace std;
int search (int a[],int n)
{
for(int i=0;i&lt;n;i++)
{
for(int j=i;j&lt;n;j++)
{
if(a[j]&gt;a[i])
{
a[i]=a[j];
}
}
}
cout&lt;&lt;&quot;The elements of the array are : &quot;&lt;&lt;endl;
for(int i=0 ; i&lt;=(n-1);i++)
{
if(i ==(n-1))
{
cout&lt;&lt;&quot; 0&quot;&lt;&lt;endl;
break;
}
cout&lt;&lt;&quot; &quot;&lt;&lt;a[i];
}
}
int main()
{
int n;
int a[20];
cout&lt;&lt;&quot;Enter number of element you want to enter in the array : &quot;&lt;&lt;endl;
cin&gt;&gt;n;
cout&lt;&lt;&quot;Enter the array element : &quot;&lt;&lt;endl;
for(int i= 0 ;i&lt;n;i++)
{
cin&gt;&gt;a[i];
}
search(a,n);
return 0;
}
OUTPUT: Enter number of element you want to enter in the array :
10
Enter the array element :
7
5
8
9
6
8
5
7
4
6
The elements of the array are :
9 9 9 9 8 8 7 7 6 0
--------------------------------
Process exited after 19.3 seconds with return value 0
Press any key to continue . . .
CODE:
#include &lt;bits/stdc++.h&gt;
using namespace std;
int main()
{
int a[100],i,n,j,num=0;
cout&lt;&lt;&quot;Enter the no. of element in Array &quot; ;
cin&gt;&gt;n ;
cout&lt;&lt;&quot;Enter Array Elememt &quot; ;
for(i=0;i&lt;n;i++)
{
cin&gt;&gt;a[i] ;
}
for(i=0;i&lt;n;i++)
{
for(j=0;j&lt;n;j++)
{
if(a[i]&gt;a[j]&amp;&amp;a[i]-a[j]==5)
{
cout&lt;&lt;&quot;(&quot;&lt;&lt;a[i]&lt;&lt;&quot;,&quot;&lt;&lt;a[j]&lt;&lt;&quot;) &quot; ;
num++;
}
}
}
cout&lt;&lt;endl&lt;&lt;&quot;Number of distinct pairs for difference 5 are : &quot;&lt;&lt;num ;
return 0 ;
}
OUTPUT: Enter the no. of element in Array 8
Enter Array Elememt 5 2 3 7 6 4 8 9
(7,2) (8,3) (9,4)
Number of distinct pairs for difference 5 are : 3
--------------------------------
Process exited after 24.98 seconds with return value 0
Press any key to continue . . .
CODE:
#include&lt;bits/stdc++.h&gt;
using namespace std;
int peak(int a[], int n )
{
int j=0;
int count[20];
for(int i=0;i&lt;n;i++)
{
if(a[i]&gt;a[i-1] &amp;&amp; a[i]&gt;a[i+1])
{
count[j]=a[i];
j +=1;
}
}
cout&lt;&lt;&quot;Peak elements of the array are : &quot;&lt;&lt;endl;
for(int i=0;i&lt;j;i++)
{
cout&lt;&lt;count[i]&lt;&lt;endl;
}
}
int main()
{
int a[20];
int n;
cout&lt;&lt;&quot;Enter the number of element you want to enter in the array : &quot;&lt;&lt;endl;
cin&gt;&gt;n;
cout&lt;&lt;&quot;Enter the array element : &quot;&lt;&lt;endl;
for(int i=0;i&lt;n;i++)
{
cin&gt;&gt;a[i];
}
peak(a,n);
}
OUTPUT: Enter the number of element you want to enter in the array :
7
Enter the array element :
10
20
15
2
23
90
68
Peak elements of the array are :
20
90
--------------------------------
Process exited after 18.97 seconds with return value 0
Press any key to continue . . .
CODE:
#include &lt;bits/stdc++.h&gt;
using namespace std;
class Term {
public:
int x, y;
Term(int x, int y)
: x(x), y(y)
{
}
};
bool cmp(Term a, Term b)
{
return a.x * b.y &lt; b.x * a.y;
}
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void farey(int n)
{
vector&lt;Term&gt; v;
for (int i = 1; i &lt;= n; ++i) {
for (int j = i + 1; j &lt;= n; ++j)
if (gcd(i, j) == 1)
v.push_back(Term(i, j));
}
sort(v.begin(), v.end(), cmp);
cout &lt;&lt; &quot;0/1 &quot;;
for (int i = 0; i &lt; v.size(); ++i)
cout &lt;&lt; v[i].x &lt;&lt; &quot;/&quot; &lt;&lt; v[i].y &lt;&lt; &quot; &quot;;
cout &lt;&lt; &quot;1/1&quot;;
}
int main()
{
int n ;
cout&lt;&lt;&quot;Enter the number : &quot;&lt;&lt;endl;
cin&gt;&gt;n;
cout &lt;&lt; &quot;Farey Sequence of order &quot; &lt;&lt; n &lt;&lt; &quot; is\n&quot;;
farey(n);
return 0;
}
OUTPUT: Enter the number :
7
Farey Sequence of order 7 is
0/1 1/7 1/6 1/5 1/4 2/7 1/3 2/5 3/7 1/2 4/7 3/5 2/3 5/7 3/4 4/5 5/6 6/7 1/1
--------------------------------
Process exited after 0.5851 seconds with return value 0
Press any key to continue . . TASK : 10
CODE:
#include&lt;bits/stdc++.h&gt;
using namespace std;
int absolute(int a[],int n,int d)
{
int num;
int s,m;
for(int i=0;i&lt;n;i++)
{
for(int j=i;j&lt;n;j++)
{
s = (a[i]-d);
if(s&lt;0)
{
s=s*(-1);
}
m = (a[j]-d);
if(m&lt;0)
{
m *=(-1);
}
if(s&gt;m)
{
num = a[i];
a[i]= a[j];
a[j]= num;
}
}
}
cout&lt;&lt;&quot;The sorted array is : &quot;;
for(int i=0;i&lt;n;i++)
{
cout&lt;&lt;a[i]&lt;&lt;&quot; &quot;;;
}
}
int main()
{
int a[20];
int n;
int d;
cout&lt;&lt;&quot;Enter the size of array : &quot;&lt;&lt;endl;
cin&gt;&gt;n;
cout&lt;&lt;&quot;Enter the array element : &quot;&lt;&lt;endl;
for(int i=0;i&lt;n;i++)
{
cin&gt;&gt;a[i];
}
cout&lt;&lt;&quot;Enter the number you want to sort according : &quot;&lt;&lt;endl;
cin&gt;&gt;d;
absolute(a,n,d);
return 0;
}
OUTPUT: Enter the size of array :
5
Enter the array element :
9 1 12 4 2
Enter the number you want to sort according :
6
The sorted array is : 4 9 2 1 12
--------------------------------
Process exited after 17.39 seconds with return value 0
Press any key to continue . . . ```