Found 26504 Articles for Server Side Programming

Minimum number of Appends needed to make a string palindrome in C++

Narendra Kumar
Updated on 22-Nov-2019 10:01:29

431 Views

Problem statementGiven a string, find minimum characters to be appended to make a string palindrome.ExampleIf string is abcac then we can make string palindrome by appending 2 highlighed characters i.e. abcacbaAlgorithmCheck if string is already palindrome, if yes then no need to append any characters.One by one remove a character from string and check whether remaining string is palindrome or notRepeat above process until string becomes palidromeReturn the number of characters removed so far as a final answerExample#include #include using namespace std; bool isPalindrome(char *str) {    int n = strlen(str);    if (n == 1) {   ... Read More

Minimum move to end operations to make all strings equal in C++

Narendra Kumar
Updated on 22-Nov-2019 09:56:34

415 Views

Problem statementGiven n strings that are permutations of each other. We need to make all strings same with an operation that moves first character of any string to the end of it.ExampleIf arr[] = {“abcd”, “cdab”} then 2 moves are required.Let us take first string “abcd”. Move character ‘a’ to the end of the string. After this operation string becomes “bcda”Now move character ‘b’ to the end of the string. After this operation string becomes “cdab”. Which in turn makes both strings equalAlgorithmTake first string. Let us call it as ‘str1’.Create a temp string by concating str1 to str1 as ... Read More

Minimum flips to make all 1s in left and 0s in right in C++

Narendra Kumar
Updated on 22-Nov-2019 09:52:57

393 Views

Problem statementGiven a binary string in which we can flip all 1’s in left part and all 0’s in right part. The task is to calculate minimum flips required to make all 1’s in left and all 0’s in rightExampleGiven binary string is 0010101. In this string there are 3 1-bits and 4 0-bits. We have to flip highlighted 4 bits to make all 1’s in left and all 0’s in right as shown below −0010101After flipping string will become −1110000AlgorithmTraverse the string from left to right and calculate the number of flips required to convert all 0’s to 1’s.Traverse ... Read More

Minimum height of a triangle with given base and area in C++

Narendra Kumar
Updated on 22-Nov-2019 09:49:26

282 Views

DescriptionGiven two integers a and b, find the smallest possible height such that a triangle of atleast area ‘a’ and base ‘b’ can be formed.ExampleIf a = 16 and b = 4 then minimum height would be 8AlgorithmArea of triangle can be calculate using below formula −area = ½ * height * baseUsing above formula, height can be calculated as −height = (2 * area) / baseSo Minimum height is the ceil() of the height obtained using above formula.Example#include #include using namespace std; float minHeight(int area, int base) {    return ceil((2 * area) / base); } int main() {    int area = 16, base = 4;    cout

C Program Coin Change

sudhir sharma
Updated on 22-Nov-2019 09:28:52

3K+ Views

In this problem, we are given a value n, and we want to make change of n rupees, and we have n number of coins each of value ranging from 1 to m. And we have to return the total number of ways in which make the sum.ExampleInput : N = 6 ; coins = {1, 2, 4}. Output : 6 Explanation : The total combination that make the sum of 6 is : {1, 1, 1, 1, 1, 1} ; {1, 1, 1, 1, 2}; {1, 1, 2, 2}; {1, 1, 4}; {2, 2, 2} ; {2, 4}.Example#include ... Read More

C / C++ Program for Dijkstra's shortest path algorithm

sudhir sharma
Updated on 08-Nov-2023 00:13:33

27K+ Views

We are given a graph with a source vertex in the graph. And we have to find the shortest path from the source vertex to all other vertices of the graph. The Dijikstra's algorithm is a greedy algorithm to find the shortest path from the source vertex of the graph to the root node of the graph. Algorithm Step 1 : Create a set shortPath to store vertices that come in the way of the shortest path tree. Step 2 : Initialize all distance values as INFINITE and assign distance values as 0 for source vertex so that it is ... Read More

Binomial Coefficient in C++

sudhir sharma
Updated on 22-Nov-2019 09:22:57

7K+ Views

Binomial coefficient denoted as c(n, k) or ncr is defined as coefficient of xk in the binomial expansion of (1+X)n.The Binomial coefficient also gives the value of the number of ways in which k items are chosen from among n objects i.e. k-combinations of n-element set. The order of selection of items not considered.Here, we are given two parameters n and k and we have to return the value of binomial coefficient nck .ExampleInput : n = 8 and k = 3 Output : 56There can be multiple solutions to this problem, General SolutionThere is a method to calculate the value ... Read More

Binary to Gray code using recursion in C program

sudhir sharma
Updated on 09-Jul-2020 11:49:04

2K+ Views

A Binary number is a number that has only two bits 0 and 1.Gray code is a special type of binary number that has a property that two successive number of the code cannot differ more than one bit. This property of gray code makes it useful more K-maps, error correction, communication, etc.This makes the conversion of binary to gray code necessary. So, let’s see an algorithm to convert binary to gray code using recursion.ExampleLet’s take an example of gray code coInput : 1001 Output : 1101AlgorithmStep 1 : Do with input n :    Step 1.1 : if n ... Read More

Binary Search for Rational Numbers without using floating point arithmetic in C program

sudhir sharma
Updated on 22-Nov-2019 09:16:45

253 Views

In this problem, we are given a sorted array of rational numbers. and we have to search the given element using binary search algorithm for this rational number array without using floating point arithmetic.A Rational number is number represented in the form p/q where both p and q are integers. For example, ⅔, ⅕.Binary search is searching technique that works by finding the middle of the array for finding the element.for finding the element using binary search from a sorted array of rational numbers, where floating point arithmetic are not allowed. We will compare the numerators and denominators to find ... Read More

Binary Search a String in C++

Ravi Ranjan
Updated on 06-Aug-2025 19:01:28

4K+ Views

The binary search algorithm works on the divide-and-conquer principle as it keeps dividing the array in half before searching. To search for an element in an array using binary search, it should be sorted. In the sorted array, we find the middle element and compare it with the element that has to be searched, and based on the comparison, we either search in the left or right sub-array or return the middle element. In this article, we are given a sorted array of strings, and our task is to search for the given string using a binary search algorithm. ... Read More

Advertisements