Found 1401 Articles for C

Maximum absolute difference of value and index sums in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:28:33

724 Views

We are given with 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] - A[j] | + |i-j| and find the maximum such sum possible. Here |A| means absolute value of A. If array has 4 elements then indexes are 0, 1, 2, 3 and unique pairs will be ( (0, 0), (1, 1), (2, 2), (3, 3), (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3) ).Input − Arr[] ... Read More

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

Sunidhi Bansal
Updated on 14-Aug-2020 08:40:57

653 Views

There are multiple ways of declaring constants in C and C++. First of all, we need to understand what constant is.What is a Constant?Constant means which can’t be changed. In terms of programming, constants are the fixed values that are assigned to the variables such that they can’t be altered by any other variable or component during the execution of a program. Constants can be of any data type. They are used in the programming for defining the non-changing component of a program. There are some data or variables which have fixed value like Pi have fixed float value as ... Read More

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

Sunidhi Bansal
Updated on 14-Aug-2020 08:05:48

2K+ Views

We are given with an array of integers. The array has multiple occurrences of the same elements. The task here is to find the maximum distance between any two same elements of the array. We will pick each element from the array starting from the left. Then we will find the last occurrence of that same number and store the difference between indexes. Now if this difference is maximum then return it.Input Arr[] = { 1, 2, 4, 1, 3, 4, 2, 5, 6, 5 }Output −Maximum distance between two occurrences of same element in array − 4Explanation − The repeating ... Read More

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

Sunidhi Bansal
Updated on 14-Aug-2020 08:02:44

961 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 − 4Explanation − The repeating characters are ‘a’ and ‘b’ only with indexes −1. 2‘a’ first index 0 last 5 , characters in between 5-0-1=4 2. ‘b’ first index 1 last 4 , characters in ... Read More

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

Sunidhi Bansal
Updated on 14-Aug-2020 08:00:34

1K+ Views

We are given a number of chocolates present in consecutive boxes in the form of an array and a number k which represents the number of students among which these chocolates will be distributed. The task here is to choose consecutive boxes such that the sum of chocolates present in them can be equally distributed among k students. Also we have to make sure that the number of chocolates is maximum.For this we will traverse the array from left to right and start adding the number of chocolates and divide the sum by k. If it is fully divided with ... Read More

Maximum money that can be withdrawn in two steps in C

Sunidhi Bansal
Updated on 14-Aug-2020 07:58:45

163 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
Updated on 14-Aug-2020 07:56:45

466 Views

We are given with 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 size) ... Read More

Maximum difference between first and last indexes of an element in array in C

Sunidhi Bansal
Updated on 14-Aug-2020 07:54:28

470 Views

We are given with an array of integers of size N. The array consists of integers in random order. The task is to find the maximum difference between the first and last indexes of an element in the array. We have to find a number which appears twice in the array and the difference between its indexes is maximum. If there are more such pairs then we will store the maximum such difference between the indexes.Input Arr[] = { 2, 1, 3, 1, 3, 2, 5, 5 }.Output −Maximum difference between first and last indexes of an element in array − ... Read More

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

Sunidhi Bansal
Updated on 14-Aug-2020 07:52:29

1K+ Views

We are given with an array of integers of size N. The array consists of integers in random order. 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 between two elements such that the larger element appears after the smaller number − 20Explanation − The maximum difference is between 21 and 1 and 21 appears after 1 in the array.Input Arr[] = {18, 2, 8, 1, 2, 3, 2, ... Read More

Maximum difference between two subsets of m elements in C

Sunidhi Bansal
Updated on 14-Aug-2020 07:13:21

586 Views

The task is to find the greatest difference between the sum of m elements in an array. Suppose we have an array and a number m, then we will first find the sum of highest m numbers and then subtract the sum of lowest m numbers from it to get the maximum difference. So the main thing is to find two subsets of m numbers which have the highest sum and lowest sum.Let’s now understand what we have to do using an example −Input arr = {1, 2, 3, 4, 5} ; m=3Output Maximum difference here is : 6Explanation − Here the ... Read More

Advertisements