Server Side Programming Articles - Page 1987 of 2650

Minimum swaps required to make a binary string alternating in C++

Narendra Kumar
Updated on 20-Dec-2019 10:07:23

423 Views

Problem statementGiven a binary string of even length and equal number of 0’s and 1’s. What is the minimum number of swaps to make the string alternating? A binary string is alternating if no two consecutive elements are equalExampleIf str = 11110000 then 2 swaps are required.AlgorithmCount number of zeroes at odd position and even position of the string. Let their count be oddZeroCnt and evenZeroCnt respectivelyCount number of ones at odd position and even position of the string. Let their count be oddOneCnt and evenOneCnt respectivelyWe will always swap a 1 with a 0. So we just check if ... Read More

Minimum Swaps required to group all 1’s together in C++

Narendra Kumar
Updated on 20-Dec-2019 10:04:18

271 Views

Problem statementGiven an array of 0’s and 1’s. The task is to find the minimum number of swaps required to group all 1’s present in the array together.ExampleIf input array = {1, 0, 1, 1, 0, 1} then 1 swap is required. i.e. swap first 0 with last 1.AlgorithmCount total number of 1’s in the arrayIf count is x, then we need to find the subarray of length x of this array with maximum number of 1’sMinimum swaps required will be the number of 0’s in the subarray of length x with maximum number of 1’sExample Live Demo#include using namespace ... Read More

Minimum swaps required to bring all elements less than or equal to k together in C++

Narendra Kumar
Updated on 20-Dec-2019 10:01:08

500 Views

Problem statementGiven an array of n positive integers and a number k. Find the minimum number of swaps required to bring all the numbers less than or equal to k together.ExampleIf input array is = {1, 5, 4, 7, 2, 10} and k = 6 then 1 swap is required i.e. swap element 7 with 2.AlgorithmCount all elements which are less than or equals to ‘k’. Let’s say the count is ‘cnt’Using two pointer technique for window of length ‘cnt’, each time keep track of how many elements in this range are greater than ‘k’. Let’s say the total count ... Read More

Minimum Sum Path in a Triangle in C++

Narendra Kumar
Updated on 20-Dec-2019 09:58:12

205 Views

Problem statementGiven a triangular structure of numbers, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.ExampleIf input is −   5   7 3  8 1 2 9 6 4 5Then minimum sum is 13 as follows −5 + 3 + 1 + 4AlgorithmUse memorization technique of dynamic programmingCreate 1-D array for memorization namely memorizationFor each K row use below formula −memorization[i] = min( memorization[i], memorization[i+1]) + A[k][i];Example Live Demo#include using namespace std; int getMinSum(vector &arr) {    int memorization[arr.size()];    int n = arr.size() - 1;    for ... Read More

Minimum sum path between two leaves of a binary trees in C++

Narendra Kumar
Updated on 20-Dec-2019 09:49:26

226 Views

Problem statementGiven a binary tree in which each node element contains a number. The task is to find the minimum possible sum from one leaf node to another.ExampleIn above tree minimum sub path is -6 as follows: (-4) + 3 + 2 + (-8) + 1AlgorithmThe idea is to maintain two values in recursive calls −Minimum root to leaf path sum for the subtree rooted under current nodeThe minimum path sum between leavesFor every visited node X, we have to find the minimum root to leaf sum in left and right sub trees of X. Then add the two values ... Read More

Minimum sum of two numbers formed from digits of an array in C++

Narendra Kumar
Updated on 20-Dec-2019 09:33:40

540 Views

DescriptionGiven an array of digits which contains values from 0 to 9. The task is to find the minimum possible sum of two numbers formed from digits of the array. Please note that we have to use all digits of given arrayExampleIf input array is {7, 5, 1, 3, 2, 4} then minimum sum is 382 as, we can create two number 135 and 247.AlgorithmSort the array in ascending orderCreate two number by picking a digit from sorted array alternatively i.e. from even and odd indexExample Live Demo#include using namespace std; int getMinSum(int *arr, int n) {    sort(arr, arr ... Read More

Python Program to print all permutations of a given string

Pavitra
Updated on 20-Dec-2019 07:34:07

632 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string we need to display all the possible permutations of the string.Now let’s observe the solution in the implementation below −Example Live Demo# conversion def toString(List):    return ''.join(List) # permutations def permute(a, l, r):    if l == r:       print (toString(a))    else:       for i in range(l, r + 1):          a[l], a[i] = a[i], a[l]          permute(a, l + 1, r)          a[l], ... Read More

Python program to find uncommon words from two Strings

Pavitra
Updated on 20-Dec-2019 07:31:43

689 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given two strings, we need to get the uncommon words from the given strings.Now let’s observe the solution in the implementation below −Example Live Demo# uncommon words def find(A, B):    # count    count = {}    # insert in A    for word in A.split():       count[word] = count.get(word, 0) + 1    # insert in B    for word in B.split():       count[word] = count.get(word, 0) + 1    # return ans    return [word ... Read More

Python program to find number of local variables in a function

Pavitra
Updated on 20-Dec-2019 07:29:18

954 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a function, we need to display the number of local variables in the function.Now let’s observe the solution in the implementation below −Example Live Demo# checking locals def scope():    a = 25.5    b = 5    str_ = 'Tutorialspoint' # main print("Number of local varibales available:", scope.__code__.co_nlocals)OutputNumber of local varibales available: 3Example Live Demo# checking locals def empty():    pass def scope():    a, b, c = 9, 23.4, True    str = 'Tutiorialspoint' # main print("Number of local varibales ... Read More

Python Program to Count trailing zeroes in factorial of a number

Pavitra
Updated on 20-Dec-2019 07:24:23

842 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an integer n, we need to count the number of trailing zeros in the factorial.Now let’s observe the solution in the implementation below −Example Live Demo# trailing zero def find(n):    # Initialize count    count = 0    # update Count    i = 5    while (n / i>= 1):       count += int(n / i)       i *= 5    return int(count) # Driver program n = 79 print("Count of trailing 0s "+"in", n, ... Read More

Advertisements