Question

In: Computer Science

Use C Programming - Given an array of integers and a number K, find the smallest...

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:

  • First line represents the number of elements in the array.
  • Second line represents the elements in the array.
  • Third line represents the value of K.

Output:

    7

Explanation: Smallest integer in the array greater than or equal to K=5 is 7.

Solutions

Expert Solution

#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;
}


Related Solutions

Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
This is C++ programming Given an int variable k, an int array incompletes that has been...
This is C++ programming Given an int variable k, an int array incompletes that has been declared and initialized, an int variable nIncompletes that contains the number of elements in the array, an int variable studentID that has been initialized, and an int variable numberOfIncompletes, Write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes. I tried this code...
Problem5: (5 pts) using c programming Given an array of integers named “simple” of length 8...
Problem5: (5 pts) using c programming Given an array of integers named “simple” of length 8 and filled as follows: 5 20 13 8 0 16 55 2 int x = 5; int *p = &simple[3] ; Write the values of x after each of the following executes (write unknown if cannot be determined): (hint: pointer moves to new location after each line) x = *(p++);                                                   x = ___8_______________ p->simple[4]            x = *( p + 1) ;                                               x = _____16______________...
Given an array of integers, and given a specific value k (not equal to 0), produce...
Given an array of integers, and given a specific value k (not equal to 0), produce all unique pairs of values in the array which differ by k. For example, if the array has [1,4,9,12, 6, 15, 5, 13,17] and k=3, the answer would be (1,4 ) ( 9,12), ( 9,6), (12,15). If k=4, the answer would be (1,5), (9,5), (13,17), (9.13). Notice that you do not print the same answer twice, for instance (9,13), and (13,9).
Develop a C++ function to find the number of even and odd integers in a given...
Develop a C++ function to find the number of even and odd integers in a given array of integers Pass in an array of integers Pass in the size of the array of integers Pass in a reference parameter for the number of even values Pass in a reference parameter for the number of odd values The function doesn't return anything Develop a C++ program to test your function
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT