Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 2197 of 3366
467 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
202 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
178 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
346 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
261 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
317 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
840 Views
Problem statementGiven an array of N integers. The bitwise OR of all the elements of the array has to be maximized by performing one task. The task is to multiply any element of the array at-most k times with a given integer xIf input array is {4, 3, 6, 1}, k = 2 and x = 3 then maximum value can be obtained is 55Algorithm1. multiply an array element with (x^k) and do bitwise OR it with the bitwise OR of all previous elements 2. Multiply an array element with bitwise OR of all next elements 3. Return the maximum ... Read More
781 Views
Problem statementGiven a binary array, find the maximum number of zeros in an array with one flip of a subarray allowed. A flip operation switches all 0s to 1s and 1s to 0sIf arr1= {1, 1, 0, 0, 0, 0, 0}If we flip first 2 1’s to 0’s, then we can get subarray of size 7 as follows −{0, 0, 0, 0, 0, 0, 0}Algorithm1. Consider all subarrays and find a subarray with maximum value of (count of 1s) – (count of 0s) 2. Considers this value be maxDiff. Finally return count of zeros in original array + maxDiff.Example Live Demo#include ... Read More
935 Views
Problem statementGiven two arrays with size n, maximize the first array by using the elements from the second array such that the new array formed contains n greatest but unique elements of both the arrays giving the second array priority i.e. all elements of the second array appear before the first array. The order of appearance of elements should be kept the same in output as in inputIf arr1[] = {12, 15, 10} and arr2[] = {16, 17, 5} then {16, 17, 15} are the maximum elements from both array by maintaining order.Algorithm1. Create temporary array of size 2 * ... Read More
378 Views
Problem statementGiven an array of size n and a number k. We have to modify an array k number of times.Modify array means in each operation we can replace any array element arr[i] by negating it i.e. arr[i] = -arr[i]. The task is to perform this operation in such a way that after k operations, the sum of an array must be maximum.If input arr[] = {7, -3, 5, 4, -1} then maximum sum will be 20First negate -3. Now array becomes {7, 3, 5, 4, -1}Negate -1. Now array becomes {7, 3, 5, 4, 1}Algorithm1. Replace the minimum element ... Read More