Maximize median after doing K addition operation on the Array

Given an array arr[] of N elements and an integer **K, **the task is to perform at most K operation on the array. In the one operation increment any element by one of the array. Find maximize median after doing K such operation.

Example:

Input:_ arr[] = {1, 3, 4, 5}, K = 3_

_Output: _5

Explanation:_ Here we add two in the second element and one in the third element then we will get a maximum median. After k operation the array can become {1, 5, 5, 5}. So the maximum median we can make is ( 5 + 5 ) / 2 = 5, because here N is even._

_Input: _arr[]_ = _{1, 3, 6, 4, 2}, K = 10

_Output: _7

Approach:

  1. Sort the array in increasing order.
  2. Since the median is the middle element of the array doing the operation in the left half then it will be worthless because it will not increase the median.
  3. Perform the operation in the second half and start performing the operations from the n/2th element to the end.
  4. If N is even then start doing the operation from the n/2 element to the end.
  5. Using Binary Search we will check for any number is possible as a median or not after doing K operation.
  6. If the median is possible then we will check for the next number which is greater than the current median calculated. Otherwise, the last possible value of the median is the required result.

Below is the implementation of the above approach:

  • C++

// C++ program for the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Function to check operation can be

// perform or not

**bool** possible(``**int** arr[], **int** N,

**int** mid, **int** K)

{

**int** add = 0;

**for** (``**int** i = N / 2 - (N + 1) % 2;

i < N; ++i) {

**if** (mid - arr[i] > 0) {

// Number of operation to

// perform s.t. mid is median

add += (mid - arr[i]);

**if** (add > K)

**return** **false**``;

}

}

// If mid is median of the array

**if** (add <= K)

**return** **true**``;

**else**

**return** **false**``;

}

// Function to find max median

// of the array

**int** findMaxMedian(``**int** arr[], **int** N,

**int** K)

{

// Lowest possible median

**int** low = 1;

**int** mx = 0;

**for** (``**int** i = 0; i < N; ++i) {

mx = max(mx, arr[i]);

}

// Highest possible median

**long** **long** **int** high = K + mx;

**while** (low <= high) {

**int** mid = (high + low) / 2;

// Checking for mid is possible

// for the median of array after

// doing at most k operation

**if** (possible(arr, N, mid, K)) {

low = mid + 1;

}

**else** {

high = mid - 1;

}

}

**if** (N % 2 == 0) {

**if** (low - 1 < arr[N / 2]) {

**return** (arr[N / 2] + low - 1) / 2;

}

}

// Return the max possible ans

**return** low - 1;

}

// Driver Code

**int** main()

{

// Given array

**int** arr[] = { 1, 3, 6 };

// Given number of operation

**int** K = 10;

// Size of array

**int** N = **sizeof**``(arr) / **sizeof**``(arr[0]);

// Sort the array

sort(arr, arr + N);

// Function call

cout << findMaxMedian(arr, N, K);

**return** 0;

}

Output:

9

Time Complexity: O(N*log(K + M)), where M is the maximum element of the given array.

**Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #greedy #searching #sorting #binary search #median-finding

What is GEEK

Buddha Community

Maximize median after doing K addition operation on the Array

Maximize median after doing K addition operation on the Array

Given an array arr[] of N elements and an integer **K, **the task is to perform at most K operation on the array. In the one operation increment any element by one of the array. Find maximize median after doing K such operation.

Example:

Input:_ arr[] = {1, 3, 4, 5}, K = 3_

_Output: _5

Explanation:_ Here we add two in the second element and one in the third element then we will get a maximum median. After k operation the array can become {1, 5, 5, 5}. So the maximum median we can make is ( 5 + 5 ) / 2 = 5, because here N is even._

_Input: _arr[]_ = _{1, 3, 6, 4, 2}, K = 10

_Output: _7

Approach:

  1. Sort the array in increasing order.
  2. Since the median is the middle element of the array doing the operation in the left half then it will be worthless because it will not increase the median.
  3. Perform the operation in the second half and start performing the operations from the n/2th element to the end.
  4. If N is even then start doing the operation from the n/2 element to the end.
  5. Using Binary Search we will check for any number is possible as a median or not after doing K operation.
  6. If the median is possible then we will check for the next number which is greater than the current median calculated. Otherwise, the last possible value of the median is the required result.

Below is the implementation of the above approach:

  • C++

// C++ program for the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Function to check operation can be

// perform or not

**bool** possible(``**int** arr[], **int** N,

**int** mid, **int** K)

{

**int** add = 0;

**for** (``**int** i = N / 2 - (N + 1) % 2;

i < N; ++i) {

**if** (mid - arr[i] > 0) {

// Number of operation to

// perform s.t. mid is median

add += (mid - arr[i]);

**if** (add > K)

**return** **false**``;

}

}

// If mid is median of the array

**if** (add <= K)

**return** **true**``;

**else**

**return** **false**``;

}

// Function to find max median

// of the array

**int** findMaxMedian(``**int** arr[], **int** N,

**int** K)

{

// Lowest possible median

**int** low = 1;

**int** mx = 0;

**for** (``**int** i = 0; i < N; ++i) {

mx = max(mx, arr[i]);

}

// Highest possible median

**long** **long** **int** high = K + mx;

**while** (low <= high) {

**int** mid = (high + low) / 2;

// Checking for mid is possible

// for the median of array after

// doing at most k operation

**if** (possible(arr, N, mid, K)) {

low = mid + 1;

}

**else** {

high = mid - 1;

}

}

**if** (N % 2 == 0) {

**if** (low - 1 < arr[N / 2]) {

**return** (arr[N / 2] + low - 1) / 2;

}

}

// Return the max possible ans

**return** low - 1;

}

// Driver Code

**int** main()

{

// Given array

**int** arr[] = { 1, 3, 6 };

// Given number of operation

**int** K = 10;

// Size of array

**int** N = **sizeof**``(arr) / **sizeof**``(arr[0]);

// Sort the array

sort(arr, arr + N);

// Function call

cout << findMaxMedian(arr, N, K);

**return** 0;

}

Output:

9

Time Complexity: O(N*log(K + M)), where M is the maximum element of the given array.

**Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #greedy #searching #sorting #binary search #median-finding

Ray  Patel

Ray Patel

1619565060

Ternary operator in Python?

  1. Ternary Operator in Python

What is a ternary operator: The ternary operator is a conditional expression that means this is a comparison operator and results come on a true or false condition and it is the shortest way to writing an if-else statement. It is a condition in a single line replacing the multiline if-else code.

syntax : condition ? value_if_true : value_if_false

condition: A boolean expression evaluates true or false

value_if_true: a value to be assigned if the expression is evaluated to true.

value_if_false: A value to be assigned if the expression is evaluated to false.

How to use ternary operator in python here are some examples of Python ternary operator if-else.

Brief description of examples we have to take two variables a and b. The value of a is 10 and b is 20. find the minimum number using a ternary operator with one line of code. ( **min = a if a < b else b ) **. if a less than b then print a otherwise print b and second examples are the same as first and the third example is check number is even or odd.

#python #python ternary operator #ternary operator #ternary operator in if-else #ternary operator in python #ternary operator with dict #ternary operator with lambda

Joseph  Murray

Joseph Murray

1623911281

How to Print an Array in Java

Introduction

Printing an array is a quick way to give us visibility on the values of the contents inside. Sometimes the array values are the desired output of the program.

In this article, we’ll take a look at how to print an array in Java using four different ways.

While the “best way” depends on what your program needs to do, we begin with the simplest method for printing and then show more verbose ways to do it.

#java #array #how to print an array in java #array in java #print an array in java #print

Abdullah  Kozey

Abdullah Kozey

1617738420

Unformatted input/output operations In C++

In this article, we will discuss the unformatted Input/Output operations In C++. Using objects cin and cout for the input and the output of data of various types is possible because of overloading of operator >> and << to recognize all the basic C++ types. The operator >> is overloaded in the istream class and operator << is overloaded in the ostream class.

The general format for reading data from the keyboard:

cin >> var1 >> var2 >> …. >> var_n;

  • Here, var1var2, ……, varn are the variable names that are declared already.
  • The input data must be separated by white space characters and the data type of user input must be similar to the data types of the variables which are declared in the program.
  • The operator >> reads the data character by character and assigns it to the indicated location.
  • Reading of variables terminates when white space occurs or character type occurs that does not match the destination type.

#c++ #c++ programs #c++-operator overloading #cpp-input-output #cpp-operator #cpp-operator-overloading #operators

Luna  Hermann

Luna Hermann

1595467140

NumPy Array Tutorial - Python NumPy Array Operations and Methods

The most important feature of NumPy is the homogeneous high-performance n-dimensional array object. Data manipulation in Python is nearly equivalent to the manipulation of NumPy arrays. NumPy array manipulation is basically related to accessing data and sub-arrays. It also includes array splitting, reshaping, and joining of arrays. Even the other external libraries in Python relate to NumPy arrays.

_Keeping you updated with latest technology trends, _Join DataFlair on Telegram

Numpy Array Basics

Arrays in NumPy are synonymous with lists in Python with a homogenous nature. The homogeneity helps to perform smoother mathematical operations. These arrays are mutable. NumPy is useful to perform basic operations like finding the dimensions, the bite-size, and also the data types of elements of the array.

NumPy Array Creation

Array Creation in NumPy

1. Using the NumPy functions

NumPy has a variety of built-in functions to create an array.

a. Creating one-dimensional array in NumPy

For 1-D arrays the most common function is np.arange(…), passing any value create an array from 0 to that number.

  1. import numpy as np
  2. array=np.arange(20)
  3. array

Output

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12, 13, 14, 15, 16, 17, 18, 19])

We can check the dimensions by using array.shape.

#numpy tutorials #array in numpy #numpy array #python numpy array