In: Computer Science
Use C Programming -
Given an array of integers and a number K, find the smallest element in array greater than or equal to K. If such element exists in the array, display it otherwise display "-1".
Example:
Input:
8
1 3 4 7 8 9 9 10
5
where:
Output:
7
Explanation: Smallest integer in the array greater than or equal to K=5 is 7.
#include <stdio.h> int main(void) { int n, i,k,res=-1; int arr[200]; scanf("%d",&n); for(i = 0;i<n;i++){ scanf("%d",&arr[i]); } scanf("%d",&k); for(i = 0;i<n;i++){ if(arr[i]>k){ if(res==-1){ res = arr[i]; } else if(res-k > arr[i]-k){ res = arr[i]; } } } printf("%d\n",res); return 0; }