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.
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element...
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element in an unsorted array of integers. please make two separate methods in java
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______________...
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
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).
Use c++ programming Construct an array of 1000 random integers within range [0, 100] An input...
Use c++ programming Construct an array of 1000 random integers within range [0, 100] An input file input.txt is provide. Each line of input.txt is a query integer that you need to check how many of that number is in your random integer array. For each query integer, fork a new child process to do the counting. The output is for each input query, output the count and child process id. For example: $> query: 13 count: 5 pid: 13342...
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