Narendra Kumar

Narendra Kumar

180 Articles Published

Articles by Narendra Kumar

Page 15 of 18

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

Narendra Kumar
Narendra Kumar
Updated on 22-Nov-2019 466 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
Narendra Kumar
Updated on 22-Nov-2019 454 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
Narendra Kumar
Updated on 22-Nov-2019 349 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

Read More

Minimum number of power terms with sum equal to n using C++.

Narendra Kumar
Narendra Kumar
Updated on 31-Oct-2019 361 Views

Problem statementGiven two positive integer N and X. The task is to express N as a sum of powers of X (X0 + X1 +…..+ Xn) such that the number of powers of X should be minimum.Print the minimum number of power of N used to make the sum equal to N.If N = 15 and X = 3 then we need 3 powers of ‘3’ as follows −15 = (32 + 31 + 31)AlgorithmUse below formula to calculate final result −1. If x = 1, then answer will be n only (n = 1 + 1 +…. n times)s ...

Read More

Minimum number of points to be removed to get remaining points on one side of axis using C++.

Narendra Kumar
Narendra Kumar
Updated on 31-Oct-2019 635 Views

Problem statementWe are given N points in a Cartesian plane. Our task is to find the minimum number of points that should be removed in order to get the remaining points on one side of any axis.If given input is {(10, 5), (-2, -5), (13, 8), (-14, 7)} then if we remove (-2, -5) then all remaining points are above X-axis.Hence answer is 1.Algorithm1. Finds the number of points on all sides of the X-axis and Y-axis 2. Return minimum from both of themExample#include #include #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; struct point{    int x, ...

Read More

Minimum Number of Platforms Required for a Railway Station using C++.

Narendra Kumar
Narendra Kumar
Updated on 31-Oct-2019 384 Views

Problem statementGiven arrival and departure times of all trains that reach a railway station, the task is to find the minimum number of platforms required for the railway station so that no train waits.We are given two arrays that represent arrival and departure times of trains that stop.For below input, we need at least 3 platforms −TrainArrival timeDeparture timeTrain-109:0009:15Train-209:3511:45Train-309:4011:05Train-411:0012:00Train-514:3018:15Train-618:0019:00Algorithm1. Sort arrival and departure time arrays in ascending order 2. Trace the number of trains at any time keeping track of trains that haves arrived, but not departedExample#include #include #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; int getPlatformCount(int ...

Read More

Minimum number of palindromes required to express N as a sum using C++.

Narendra Kumar
Narendra Kumar
Updated on 31-Oct-2019 263 Views

Problem statementGiven a number N, we have to find the minimum number of palindromes required to express N as a sum of themIf N = 15 then 2 palindromes are required i.e. 8 and 7.Algorithm1. Generate all the palindromes up to N in a sorted fashion 2. Find the size of the smallest subset such that its sum is NExample#include #include #include #include using namespace std; vector table; int createPalindrome(int input, bool isOdd){    int n = input;    int palindrome = input;    if (isOdd)       n /= 10;    while (n > ...

Read More

Minimum number of page turns to get to a desired page using C++.

Narendra Kumar
Narendra Kumar
Updated on 31-Oct-2019 632 Views

Problem statementGiven a book of N pages, the task is to calculate the minimum number of page turns to get to a give desired page K.we can either start turning pages from the front side of the book (i.e from page 1) or from the backside of the book (i.e page number N).Each page has two sides, front and back, except the first page, which has only backside and the last page which may only have backside depending on the number of pages of the book.If N = 5 and K = 4 then we have to turn minimum 1 ...

Read More

Minimum number of operations required to sum to binary string S using C++.

Narendra Kumar
Narendra Kumar
Updated on 31-Oct-2019 286 Views

Problem statementGiven a binary string str. Find the minimum number of operations required to be performed to create the number represented by str. Only following operations can be performed −Add 2xSubtract 2xIf binary string is “1000” then we have to perform only 1 operation i.e. Add 23If binary string is “101” then we have to perform 2 operations i.e. Add 22 + 20Example#include #include #include using namespace std; int getMinOperations(string s){    reverse(s.begin(), s.end());    int n = s.length();    int result[n + 1][2];    if (s[0] == '0') {       result[0][0] = 0;   ...

Read More

Minimum number of operations required to delete all elements of the array using C++.

Narendra Kumar
Narendra Kumar
Updated on 31-Oct-2019 1K+ Views

Problem statementGiven an integer array arr, the task is to print the minimum number of operations required to delete all elements of the array. While deleting element following restriction is imposed −Any element from the array can be chosen at random and every element divisible by it can be removed from the arrayIf arr[] = {2, 4, 15, 10, 8, 5, 3} then 3 operation are required to delete all elements −If we choose 2 then it will delete {2, 4, 10, 8}If we choose 5 then it will remove {5, 15}If we choose 3 then it will remove {3}Algorithm1. ...

Read More
Showing 141–150 of 180 articles
Advertisements