Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 950 of 2547
Maximum absolute difference of value and index sums in C
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 MoreDifferent ways to declare variable as constant in C and C++
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 MoreMaximum distance between two occurrences of same element in array in C
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 MoreMaximum number of characters between any two same character in a string in C
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 MoreMaximum number of chocolates to be distributed equally among k students in C
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 MoreMaximum money that can be withdrawn in two steps in C
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 MoreMaximum difference between the group of k-elements and rest of the array in C
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 MoreMaximum difference between two elements such that larger element appears after the smaller number in C
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 MoreMaximum difference between two subsets of m elements in C
The task is to find the maximum difference between the sum of two subsets, each containing m elements from an array. We need to find the subset with the highest sum and the subset with the lowest sum, then calculate their difference. Syntax int find_diff(int arr[], int length, int m); Algorithm The approach to solve this problem is − Sort the array in ascending order Sum the first m elements (lowest subset) Sum the last m elements (highest subset) Return the difference between highest and lowest sums Example 1: Basic ...
Read MoreMaximum difference of sum of elements in two rows in a matrix in C
We are given a matrix and the task is to find the greatest difference between the sum of elements in two rows of a matrix. Suppose we have a matrix M[i,j] with i rows and j columns. Let the rows be R0 to Ri-1. The difference will be calculated by subtracting the (sum of elements of Ry) - (sum of elements of Rx), where x
Read More