Found 26504 Articles for Server Side Programming

‘AND’ vs ‘&&’ operators in PHP

AmitDiwan
Updated on 24-Dec-2019 12:08:08

2K+ Views

The difference is the precedence when we compare AND with && operator. The precedence of AND operator is lower than the operator = when the evaluation is performed, therefore even if both the operators do the same work, the result is different.ExampleLet us first see an example of the AND operator− Live DemoOutputThis will produce the following output−Result = TrueExampleLet us now see an example of the && operator− Live DemoOutputThis will produce the following output:Result = TrueExampleLet us now see the difference in a single example − Live DemoOutputThis will produce the following output−true false

Comparing two dates in PHP

AmitDiwan
Updated on 02-Jan-2020 06:09:34

701 Views

To compare two dates in PHP, the code is as follows. Here, we have used the equality operator to compare dates −Example Live DemoOutputThis will produce the following output−Date1 = 2019-10-30 Date2 = 2019-10-30 Both the dates are equal!ExampleLet us now see another example− Live DemoOutputThis will produce the following output−Date1 = 2019-11-08 Date2 = 2018-08-10 DateOne is the latest date!

Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++

Narendra Kumar
Updated on 24-Dec-2019 07:21:26

232 Views

Problem statementGiven an array, arr[] find the maximum value of (arr[i] – i) – (arr[j] – j) where i is not equal to j. Where i and j vary from 0 to n-1 and n is the size of input array arr[].If the input array is {7, 5, 10, 2, 3} then we can obtain 9 maximum value as follows−(element 10 – index 2) - (element 2 – index 3) (10 – 2) – (2 – 3) = 8 – (-1) = 9Algorithm1. Find maximum value of (arr[i] – i) in whole array. 2. Find minimum value of (arr[i] – ... Read More

Maximize the value of the given expression in C++

Narendra Kumar
Updated on 24-Dec-2019 07:17:59

316 Views

Problem statementGiven three non-zero integers a, b and c. The task is to find the maximum value possible by putting addition and multiplication signs between them in any order.Please note that rearrangement of integers is allowed but addition and multiplication sign must be used once.If a = 1, b = 3 and c = 5 then maximum value will be 20 as follows−(1 + 3) * 5 = 20Algorithm1. If all numbers are positive, then add two small numbers and multiply result with larger one 2. If only two numbers are positive, then multiply 2 positive numbers and add remaining ... Read More

Maximize the sum of arr[i]*i in C++

Narendra Kumar
Updated on 24-Dec-2019 07:15:21

452 Views

Problem statementGiven an array of N integers. You are allowed to rearrange the elements of the array. The task is to find the maximum value of Σarr[i]*i, where i = 0, 1, 2, .. n – 1.If input array = {4, 1, 6, 2} then the maximum sum will be 28 if we rearrange elements in sorted order−{1, 2, 4, 6} = (1 * 0) + (2 * 1) + (4 * 2) + (6 * 3) = 28Algorithm1. Sort array in ascending order 2. Iterate over array and multiply each array element by 1 where i = 0, 1, ... Read More

Maximize the number of segments of length p, q and r in C++

Narendra Kumar
Updated on 24-Dec-2019 07:12:10

191 Views

Problem statementGiven a rod of length L, the task is to cut the rod in such a way that the total number of segments of length p, q and r is maximized. The segments can only be of length p, q, and rIf l = 15, p = 2, q = 3 and r = 5 then we can make 7 segments as follows −{2, 2, 2, 2, 2, 2, 3}AlgorithmWe can solve this problem using dynamic programming1. Initialize dp[] array to 0 2. Iterate till the length of the rod. For every i, a cut of p, q and ... Read More

Maximize the number by rearranging bits in C++

Narendra Kumar
Updated on 24-Dec-2019 07:09:42

170 Views

Problem statementGiven an unsigned number, find the maximum number that could be formed by using the bits of the given unsigned numberIf the input number is 8 then its binary representation is−00000000000000000000000000001000To maximize it set MSB to 1. Then number becomes 2147483648 whose binary representation is−10000000000000000000000000000000Algorithms1. Count number of set bits in the binary representation of a given number 2. Find a number with n least significant set bits 3. shift the number left by (32 – n)Example Live Demo#include using namespace std; unsigned getMaxNumber(unsigned num){    int n = __builtin_popcount(num);    if (n == 32) {       return num;    }    unsigned result = (1

Maximize the median of the given array after adding K elements to the same array in C++

Narendra Kumar
Updated on 24-Dec-2019 07:07:25

340 Views

Problem statementGiven an array arr[] of N elements and an integer K where K < N. The task is to insert K integer elements to the same array such that the median of the resultant array is maximizedIf input array is {1, 3, 2, 5} and k = 3 then −Sorted array becomes {1, 2, 3, 5}Insert 3 elements that are greater than 5. After this operation array becomes {1, 2, 3, 5, 6, 6, 6}Median of the new array is 5Algorithm1. In order to maximize the median of the resultant array, all the elements that need to be inserted ... Read More

Maximize the median of an array in C++

Narendra Kumar
Updated on 24-Dec-2019 07:05:41

257 Views

Problem statementGiven an array arr[] of N elements and an integer K where K < N. The task is to insert K integer elements to the same array such that the median of the resultant array is maximizedIf input array is {1, 3, 2, 5} and k = 3 then−Sorted array becomes {1, 2, 3, 5}Insert 3 elements that are greater than 5. After this operation array becomes {1, 2, 3, 5, 6, 6, 6}Median of the new array is 5Algorithm1. In order to maximize the median of the resultant array, all the elements that need to be inserted must ... Read More

Maximize the maximum subarray sum after removing at most one element in C++

Narendra Kumar
Updated on 24-Dec-2019 07:03:04

308 Views

Problem statementGiven an array arr[] of N integers. The task is to first find the maximum sub-array sum and then remove at most one element from the sub-array. Remove at most a single element such that the maximum sum after removal is maximized.If given input array is {1, 2, 3, -2, 3} then maximum sub-array sum is {2, 3, -2, 3}. Then we can remove -2. After removing the remaining array becomes−{1, 2, 3, 3} with sum 9 which is maximum.Algorithm1. Use Kadane’s algorithm to find the maximum subarray sum. 2. Once the sum has beens find, re-apply Kadane’s algorithm ... Read More

Advertisements