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 74 of 96
C/C++ Program for Largest Sum Contiguous Subarray?
An array of integers is given. We have to find the sum of all contiguous elements whose sum is largest. This problem is known as the Maximum Subarray Problem and is efficiently solved using Kadane's Algorithm. Using dynamic programming, we store the maximum sum up to the current element. This helps find the optimal contiguous subarray with maximum sum. Syntax int maxSubarraySum(int arr[], int n); Algorithm: Kadane's Algorithm The algorithm maintains two variables − Input: An array of integers. {-2, -3, 4, -1, -2, 1, 5, -3} Output: Maximum Sum of ...
Read MoreC/C++ program to shutdown a system?
Here we will see how we can shut down the system by writing a simple C program. The shutdown process varies in different operating systems. If we are Linux user, we can use this terminal command to shut down − shutdown -P now If we are using Windows system, we can use this command − c:\windows\system32\shutdown /i Note: These programs require administrator/root privileges to execute successfully. On Linux, you may need to run with sudo. On Windows, run the program as administrator. Syntax int system(const char *command); ...
Read MoreC/C++ Program to Count set bits in an integer?
Here we will see how we can count the number of set bits in an integer. Set bits are the bits that have a value of 1 in the binary representation of a number. For example, the number 13 has binary representation 1101, which contains three set bits, so the count will be 3. To solve this problem, we will shift the number to the right and check if the least significant bit (LSB) is 1. If it is, we increment our count. This process continues until the number becomes 0. Syntax int countSetBits(int n); ...
Read MoreC/C++ Program to Count Inversions in an array using Merge Sort?
The inversion count in an array indicates how many changes are required to convert the array into its sorted form. When an array is already sorted, it needs 0 inversions. When the array is sorted in reverse order, the number of inversions is maximum. We can efficiently solve this problem using a modified merge sort algorithm to achieve O(n log n) time complexity. Syntax int merge(int arr[], int temp[], int left, int mid, int right); int mergeSort(int arr[], int temp[], int left, int right); int countInversions(int arr[], int n); Algorithm The approach uses a ...
Read MoreC/C++ Program for Triangular Matchstick Number?
Here we will see how to count number of matchsticks required to make a triangular pyramid. The base of the pyramid is given. So if the base is 1, it will take 3 matchsticks to make a pyramid, for base 2, 9 matchsticks are needed, for base size 3, it will take 18 matchsticks. Base = 1 3 matchsticks Base = 2 9 matchsticks ...
Read MoreC/C++ Program for Maximum height when coins are arranged in a triangle?
In this section, we will see one interesting problem. There are N coins, and we have to find the maximum height we can make if we arrange the coins as a pyramid. In this fashion, the first row will hold 1 coin, the second will hold 2 coins, and so on. Coin Pyramid 1 1 1 1 ...
Read Morec32rtomb() function in C/C++?
In C programming, the c32rtomb() function is used to convert a 32-bit wide character (char32_t) to its corresponding multibyte character representation. This function is defined in the uchar.h header file and is part of the C11 standard for Unicode support. Syntax size_t c32rtomb(char *s, char32_t c32, mbstate_t *ps); Parameters s − Pointer to a character array where the multibyte character will be stored c32 − The 32-bit wide character to convert ps − Pointer to an mbstate_t object that holds the conversion state Return Value The function returns the number ...
Read MoreA data structure for n elements and O(1) operations?
Here we will see one data structure with n elements, and O(1) operations. So the operations will take constant amount of time to execute. The data structure will hold n elements (from 0 to n-1). The data can be in any order. The insertion, deletion and searching will take O(1) amount of time. To solve this problem, we will use one Boolean array. This will indicate that the item is present or not at position i. If the item is present, it will hold 1, otherwise 0. Syntax void init(bool dataStructure[], int n); ...
Read MoreA C Programming Language Puzzle?
Here we will see one C programming language puzzle question. Suppose we have two numbers 48 and 96. We have to add the first number after the second one. So final result will be like 9648. But we cannot use any logical, arithmetic, string related operations, also cannot use any pre-defined functions. So how can we do that? This is easy. We can do by using Token Pasting operator(##) in C. The Token Pasting operator is a preprocessor operator. It sends commands to compiler to add or concatenate two tokens into one string. We use this operator at the ...
Read More2's compliment for a given string using XOR ?
In C programming, finding the 2's complement of a binary string can be efficiently done using the XOR operation. The 2's complement is calculated as 1's complement + 1. We use XOR to flip bits and implement a specific algorithm that traverses from the least significant bit (LSB). Syntax char* get2sComplement(char* binaryString); Algorithm The algorithm works by traversing the binary string from right to left − Ignore all trailing zeros until we find the first '1' Keep the first '1' unchanged Flip all remaining bits using XOR operation If no '1' is ...
Read More