diff --git a/Online Programming/Micro and Array Update/MicroAndArrayUpdate.java b/Online Programming/Micro and Array Update/MicroAndArrayUpdate.java new file mode 100644 index 0000000..034081b --- /dev/null +++ b/Online Programming/Micro and Array Update/MicroAndArrayUpdate.java @@ -0,0 +1,32 @@ +import java.util.Scanner; + +class MicroAndArrayUpdate { + public static void main(String[] args) throws Exception { + Scanner scanner = new Scanner(System.in); + int testCases = scanner.nextInt(); + + while (testCases > 0) { + int arrSize = scanner.nextInt(); + int K = scanner.nextInt(); + + int[] arr = new int[arrSize]; + for (int i = 0; i < arrSize; i++) { + arr[i] = scanner.nextInt(); + } + + int min = arr[0]; + for (int i = 1; i < arrSize; i++) { + if (arr[i] < min) + min = arr[i]; + } + + if (min < K) + System.out.println(K - min); + else + System.out.println(0); + + testCases--; + } + } +} + diff --git a/Online Programming/Micro and Array Update/README.md b/Online Programming/Micro and Array Update/README.md new file mode 100644 index 0000000..84b5b31 --- /dev/null +++ b/Online Programming/Micro and Array Update/README.md @@ -0,0 +1,22 @@ +# Micro and Array Update +[https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/micro-and-array-update/](https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/micro-and-array-update/) +## This is a solution to HackerEarth problem. Following steps are followed: +###### For each test case +- First, we take the size of the array as input (N). + +- Then, we take value of K as input. + +- The array is declared to be of integer type. + +- The space separated array elements are taken as input from user. + +- After that, we find the minimum element in the array. + +- If minimum element is greater than K, we print 0 else we print value of + (K - minimum element) as the answer. + +### Logic + We are supposed to find out the minimum time it takes to for each array element's value to become greater than or equal to K. + For this, we find the minimum element from the array and compare it with K. + If value is greater than or equal to K , it means all elements are already greater than or equal to K. Hence, we get 0 as answer. + If value is lesser than K, performing (K-minimum element) gives us least amount of time required.