Math, asked by sujithgalam16, 1 month ago


Array Operations: Sum of Nearest Smaller and
Greater Number

Answers

Answered by yuvi13y
1

Answer:

#include <bits/stdc++.h>

using namespace std;

int small(int *a,int n,int x)

{

int just_smaller=INT_MIN;

for(int i=0; i<n; i++)

{

if(a[i] < x and just_smaller < a[i])

just_smaller = a[i];

}

if(just_smaller !=INT_MIN)

return just_smaller;

return 0;

}

int large(int *a,int n,int x)

{

int just_greater=INT_MAX;

for(int i=0; i<n; i++)

{

if(a[i] > x and a[i] < just_greater)

just_greater = a[i];

}

if(just_greater != INT_MAX)

return just_greater;

return 0;

}

void sumNum(int *a,int n)

{

for(int i=0;i<n;i++)

{

int sm=small(a,n,a[i]);

int la=large(a,n,a[i]);

cout<<(sm+la)<<" ";

}

}

int main()

{

int n,x;

cin>>n;//>>x;

int a[n];

for(int i=0;i<n;i++)

{

cin>>a[i];

}

sumNum(a,n);

return 0;

}

Similar questions