Server Side Programming Articles

Page 931 of 2109

Maximize the difference between two subsets of a set with negatives in C

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 337 Views

We are given an array of positive and negative integers. The task is to find the maximum difference between two subsets where one contains positive numbers and the other contains negative numbers. The maximum difference is achieved by taking all positive numbers in one subset and all negative numbers in the other subset, then calculating (sum of positives) − (sum of negatives). Since subtracting negatives effectively adds them, we can convert all negatives to positives and sum all elements. Syntax int maximizeSubsetDifference(int arr[], int n); Example Input and Output Input − Arr[] = { ...

Read More

Maximum and minimum of an array using minimum number of comparisons in C

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 13K+ Views

We are given an array of integers. The task is to find the minimum and maximum elements of the array using the minimum number of comparisons possible. Syntax void findMinMax(int arr[], int n, int *min, int *max); Algorithm To minimize comparisons, we use an optimized approach − If array has only one element, both min and max are that element For arrays with multiple elements, compare first two elements to initialize min and max Then traverse remaining elements, comparing each with current min and max This approach requires approximately 1.5n comparisons instead ...

Read More

Maximum absolute difference of value and index sums in C

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 1K+ Views

We are given an array of integers. The task is to calculate the maximum absolute difference of value and index sums. That is for each pair of indexes (i, j) in an array, we have to calculate |Arr[i] - Arr[j]| + |i - j| and find the maximum such sum possible. Here |A| means absolute value of A. Syntax int maxAbsDiff(int arr[], int n); // Returns maximum value of |arr[i] - arr[j]| + |i - j| for all pairs (i, j) Example 1: Finding Maximum Absolute Difference Let's implement the brute force approach to ...

Read More

Different ways to declare variable as constant in C and C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 1K+ Views

In C programming, constants are fixed values that cannot be changed during program execution. There are multiple ways to declare variables as constants, each with its own characteristics and use cases. Syntax const data_type variable_name = value; #define MACRO_NAME value enum { constant1, constant2, ... }; Method 1: Using const Keyword The const keyword is the most common way to create read-only variables. Once declared, attempting to modify the value results in a compilation error − #include int main() { const int value = 5; ...

Read More

Maximum distance between two occurrences of same element in array in C

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 2K+ Views

In this problem, we need to find the maximum distance between two occurrences of the same element in an array. The distance is calculated as the number of elements between the first and last occurrence of any repeating element. Syntax int maxDistance(int arr[], int n); Algorithm Traverse each element of the array For each element, find its last occurrence in the remaining array Calculate the distance between first and last occurrence (j - i - 1) Keep track of the maximum distance found Return -1 if no repeating elements exist Example ...

Read More

Maximum number of characters between any two same character in a string in C

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 1K+ Views

We are given a string of alphabets. The array can have at least two occurrences of the same character. The task here is to find the maximum number of characters between any two occurrences of a character. If there is no repetition of any character then return -1. Input − string str = "abcdba" Output − Maximum number of characters between any two same character in a string − 4 Explanation − The repeating characters are 'a' and 'b' only with indexes − 1. 'a' first index 0 last 5, characters in between 5-0-1=4 2. 'b' ...

Read More

Maximum number of chocolates to be distributed equally among k students in C

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 1K+ Views

We are given chocolates in consecutive boxes represented as an array and k students among which these chocolates will be distributed. The task is to find consecutive boxes such that the sum of chocolates can be equally distributed among k students with maximum chocolates per student. This problem requires finding the maximum sum subarray whose sum is divisible by k. We traverse all possible subarrays, check if their sum is divisible by k, and track the maximum such sum. Syntax int maxChocolates(int arr[], int n, int k); Algorithm Iterate through all possible ...

Read More

Maximum money that can be withdrawn in two steps in C

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 314 Views

We are given two lockers, say L1 and L2 that have some amount of money in the form of coins. L1 has A coins and L2 has B number of coins in it. We have to withdraw money or coins from the lockers such that the money drawn out is maximum. Each time the coins are drawn from any locker, it is replaced by coins 1 less than its previous count. If we draw A coins from L1 then it will be replaced by A-1 coins and if we draw B coins from L2 then it will be replaced by ...

Read More

Maximum difference between the group of k-elements and rest of the array in C

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 663 Views

We are given an array of integers of size N and a number k. The array consists of integers in random order. The task is to find the maximum difference between the group of k-elements and rest of the array. The array will be divided into two parts. The first part is a group of k-elements taken out and the second part is the rest of the elements of the array. We have to select k elements such that the difference between the sum of elements in both groups is maximum. If k is smaller ( half of array ...

Read More

Maximum difference between two elements such that larger element appears after the smaller number in C

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 2K+ Views

We are given an array of integers of size N. The task is to find the maximum difference between two elements such that the larger element appears after the smaller number. That is, Arr[j] − Arr[i] is maximum such that j > i. Input: Arr[] = {2, 1, 3, 8, 3, 19, 21} Output: The maximum difference is 20 Explanation: The maximum difference is between 21 and 1, and 21 appears after 1 in the array. Syntax int maxDifference(int arr[], int n); Approach The optimal approach uses a single ...

Read More
Showing 9301–9310 of 21,090 articles
« Prev 1 929 930 931 932 933 2109 Next »
Advertisements