Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Micro And Array Update problem added under Online Programming #26

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Online Programming/Micro and Array Update/MicroAndArrayUpdate.java
Original file line number Diff line number Diff line change
@@ -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--;
}
}
}

22 changes: 22 additions & 0 deletions Online Programming/Micro and Array Update/README.md
Original file line number Diff line number Diff line change
@@ -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.