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
C Articles
Page 32 of 96
Maximum number of 2×2 squares that can be fit inside a right isosceles triangle in C
We are given a right isosceles triangle where we need to find the maximum number of 2×2 squares that can fit inside it. An isosceles triangle has two sides of equal length, and in a right isosceles triangle, the base and height are perpendicular and equal to each other. Base Height The key insight is that from the corner triangular areas, we need to subtract ...
Read MoreCount number of 1s in the array after N moves in C
We are given an array of size N. The array has all 0's initially. The task is to count the number of 1's in the array after N moves. Each Nth move has a rule associated with it − 1st Move − Change element at positions 1, 2, 3, 4... 2nd Move − Change element at positions 2, 4, 6, 8... 3rd Move − Change element at positions 3, 6, 9, 12... In each move, we flip the elements (0 becomes 1, 1 becomes 0) at the specified positions. Let's understand with examples. Syntax ...
Read MoreMaximum binomial coefficient term value in C
In C, finding the maximum binomial coefficient term for a given positive integer 'N' involves calculating all binomial coefficients nCr and determining the largest value among them. The binomial coefficient series is nC0, nC1, nC2, ..., nCr, ..., nCn-2, nCn-1, nCn. Syntax nCr = n! / (r! * (n - r)!) For N=4: 4C0=1, 4C1=4, 4C2=6, 4C3=4, 4C4=1. Maximum coefficient is 6. For N=5: 5C0=1, 5C1=5, 5C2=10, 5C3=10, 5C4=5, 5C5=1. Maximum coefficient is 10. Method 1: Using Pascal's Triangle Approach This method builds Pascal's triangle using dynamic programming to calculate binomial ...
Read MoreMaximize the difference between two subsets of a set with negatives in C
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 MoreMaximum and minimum of an array using minimum number of comparisons in C
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 MoreMaximum 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 More