1598443200

Given an array **A[]**, the task is to find the minimum number of operations required in which two adjacent elements are removed from the array and replaced by their sum, such that the array is converted to a non-increasing array.

**Note:** An array with a single element is considered as non-increasing.

**Examples:**

_ A[] = {1, 5, 3, 9, 1}_Input:

_ 2_Output:

Explanation:

Replacing {1, 5} by {6} modifies the array to {6, 3, 9, 1}

Replacing {6, 3} by {9} modifies the array to {9, 9, 1}

_ A[] = {0, 1, 2}_Input:

_ 2_Output:

**Approach: **The idea is to use Dynamic Programming. A memoization table is used to store the minimum count of operations required to make subarrays non-increasing from right to left of the given array. Follow the steps below to solve the problem:

- Initialize an array
**dp[]**where**dp[i]**stores the minimum number of operations required to make the subarray {A[i], …, A[N]} non-increasing. Therefore, the target is to compute dp[0]. - Find a minimal subarray
**{A[i] … A[j]}**such that**sum({A[i] … A[j]}) > val[j+1]**, where, val[j + 1] is the merged sum obtained for the subarray**{A[j + 1], … A[N]}**. - Update
**dp[i]**to **j – i + dp[j+1] **and**vals[i]**to**sum({A[i] … A[j]})**.

Below is the implementation of the above approach:

- C++

filter_none

edit

play_arrow

brightness_4

`// C++ program to implement`

`// the above approach`

`#include <bits/stdc++.h>`

`**using**`

`**namespace**`

`std;`

`// Function to find the minimum operations`

`// to make the array Non-increasing`

`**int**`

`solve(vector<``**int**``>& a)`

`{`

`// Size of the array`

`**int**`

`n = a.size();`

`// Dp table initialization`

`vector<``**int**``> dp(n + 1, 0), val(n + 1, 0);`

`// dp[i]: Stores minimum number of `

`// operations required to make `

`// subarray {A[i], ..., A[N]} non-increasing`

`**for**`

`(``**int**`

`i = n - 1; i >= 0; i--) {`

`**long**`

`**long**`

`sum = a[i];`

`**int**`

`j = i;`

`**while**`

`(j + 1 < n and sum < val[j + 1]) {`

`// Increment the value of j`

`j++;`

`// Add current value to sum`

`sum += a[j];`

`}`

`// Update the dp tables`

`dp[i] = (j - i) + dp[j + 1];`

`val[i] = sum;`

`}`

`// Return the answer`

`**return**`

`dp[0];`

`}`

`// Driver code`

`**int**`

`main()`

`{`

`vector<``**int**``> arr = { 1, 5, 3, 9, 1 };`

`cout << solve(arr);`

`}`

**Output:**

```
2
```

** Time Complexity:**_ O(N2)_

** Auxiliary Space:**_ O(N)_

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 #dynamic programming #mathematical #memoization

1598443200

Given an array **A[]**, the task is to find the minimum number of operations required in which two adjacent elements are removed from the array and replaced by their sum, such that the array is converted to a non-increasing array.

**Note:** An array with a single element is considered as non-increasing.

**Examples:**

_ A[] = {1, 5, 3, 9, 1}_Input:

_ 2_Output:

Explanation:

Replacing {1, 5} by {6} modifies the array to {6, 3, 9, 1}

Replacing {6, 3} by {9} modifies the array to {9, 9, 1}

_ A[] = {0, 1, 2}_Input:

_ 2_Output:

**Approach: **The idea is to use Dynamic Programming. A memoization table is used to store the minimum count of operations required to make subarrays non-increasing from right to left of the given array. Follow the steps below to solve the problem:

- Initialize an array
**dp[]**where**dp[i]**stores the minimum number of operations required to make the subarray {A[i], …, A[N]} non-increasing. Therefore, the target is to compute dp[0]. - Find a minimal subarray
**{A[i] … A[j]}**such that**sum({A[i] … A[j]}) > val[j+1]**, where, val[j + 1] is the merged sum obtained for the subarray**{A[j + 1], … A[N]}**. - Update
**dp[i]**to **j – i + dp[j+1] **and**vals[i]**to**sum({A[i] … A[j]})**.

Below is the implementation of the above approach:

- C++

filter_none

edit

play_arrow

brightness_4

`// C++ program to implement`

`// the above approach`

`#include <bits/stdc++.h>`

`**using**`

`**namespace**`

`std;`

`// Function to find the minimum operations`

`// to make the array Non-increasing`

`**int**`

`solve(vector<``**int**``>& a)`

`{`

`// Size of the array`

`**int**`

`n = a.size();`

`// Dp table initialization`

`vector<``**int**``> dp(n + 1, 0), val(n + 1, 0);`

`// dp[i]: Stores minimum number of `

`// operations required to make `

`// subarray {A[i], ..., A[N]} non-increasing`

`**for**`

`(``**int**`

`i = n - 1; i >= 0; i--) {`

`**long**`

`**long**`

`sum = a[i];`

`**int**`

`j = i;`

`**while**`

`(j + 1 < n and sum < val[j + 1]) {`

`// Increment the value of j`

`j++;`

`// Add current value to sum`

`sum += a[j];`

`}`

`// Update the dp tables`

`dp[i] = (j - i) + dp[j + 1];`

`val[i] = sum;`

`}`

`// Return the answer`

`**return**`

`dp[0];`

`}`

`// Driver code`

`**int**`

`main()`

`{`

`vector<``**int**``> arr = { 1, 5, 3, 9, 1 };`

`cout << solve(arr);`

`}`

**Output:**

```
2
```

** Time Complexity:**_ O(N2)_

** Auxiliary Space:**_ O(N)_

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 #dynamic programming #mathematical #memoization

1619565060

**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

1623911281

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.

- Print an Array Using Arrays.toString() and Arrays.deepToString()
- Print an Array Using Java 8 Streams
- Print an Array by Converting to List
- Print an Array Using For Loops
- Converting To List and Using an Iterator

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

1579945937

Essential information regarding how do dating apps make money and how does tinder make money. Moreover, we present unique ways to make money through dating apps.

#how does tinder make money #how does bumble make money #how much money do dating apps make #how dating apps make money #how do dating apps make money

1617738420

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,
**var1**,**var2**, ……,**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