Programming Articles - Page 326 of 3363

Find an N-length Binary String having maximum sum of elements from given ranges

Prabhdeep Singh
Updated on 17-May-2023 14:36:19

253 Views

We will be given an array that will contain the pairs which represent the range and their value will range from 0(inclusive) to N(exclusive). Here, N is the size of the binary string which we have to return as the answer. For all the given ranges we have to maximize the sum of the product of the frequencies of zero and one. We will implement two approaches one is the naive approach by finding all the strings and another is the efficient solution. Sample Examples Input 1 Given array: {{1, 3}, {2, 4}, {2, 5}} Length of string: 6 Output ... Read More

Sort an array by swapping adjacent elements from indices that contains ‘1’ in a given string

Prabhdeep Singh
Updated on 17-May-2023 14:33:57

534 Views

Sorting an array means ordering all the elements of the array in increasing order. Sorting an array by swapping adjacent elements means we can only swap elements that are adjacent to each other but we can swap adjacent elements any number of times. We will be given a binary string that will contain only two types of characters ‘0’ and ‘1’. If any character is ‘0’ in the given string then we cannot swap the element present at that index of the array with adjacent elements. Sample Examples Input 1 Given array: {1, 4, 3, 2, 5, 7, 6} Given ... Read More

Minimum number of flipping adjacent bits required to make given Binary Strings equal

Prabhdeep Singh
Updated on 17-May-2023 14:30:48

517 Views

A binary string is a string that contains only two different types of characters 0 and 1. We will be given two binary strings of the same length and our task is to make both of them equal by toggling two adjacent characters of the first string. Also, we have to do this in a minimum number of operations as possible. If it is not possible to convert the first string to the second string then return -1. Sample Example Input 1 string1: 101001 string 2: 100110 Output: 2 Explanation − We can toggle the second index character and the ... Read More

Python Program to get the first item from the array

Gireesha Devara
Updated on 29-May-2023 15:00:17

537 Views

In programming array is a data structure, which is used to store collection of homogeneous data elements. And each element in the array is identified by an index value or key. Arrays in Python Python doesn’t have a built-in data structure to represent arrays, but it has a built-in array module for arrays. And also we can use the NumPy package to work with arrays in python. An array defined by the array module is − array('i', [1, 2, 3, 4]) A Numpy array defined by the NumPy module is − array([1, 2, 3, 4]) Also, we can ... Read More

Max count of N using digits of M such that 2 and 5, and, 6 and 9 can be treated as same respectively

Prabhdeep Singh
Updated on 16-May-2023 13:52:56

253 Views

Max count is a count which is the maximum possible. Here we have given an integer N and a string of integer M. Our task is to return the maximum count of making the number N using the digits of the string of integer M. Also given that, we can be treated 2 and 5, and, 6 and 9 can be same respectively. Sample Examples Input 1 N = 29 M = "2569783" Output 1: 2 Explanation − As 5 is the same as 2 and 6 is the same as 9 so we have a total of two ... Read More

Find the smallest number formed by inserting a given digit

Prabhdeep Singh
Updated on 16-May-2023 13:50:26

418 Views

Inserting a number in the given number means adding a new given digit in the given number either at the front, end, or in between the numbers. We have given a number and a digit and have to add the digit in the number in a way such that the resultant new number will be minimum as possible. We will convert the number into a string to make the work of insertion easy. Also, the given number can be negative also so we have to consider this case. Sample Examples Input1 Given number: 124 Given digit: 3 Output: 1234 ... Read More

Maximize the missing values in a given time in HH:MM format

Prabhdeep Singh
Updated on 16-May-2023 13:46:32

191 Views

A string will be given to us of length five which represents the time in the HH:MM format. There may be some ‘?’ present in the string and we have to replace them with any number such that the result is the valid time which could be the maximum possible. Also, the given string numbers will be valid, and ‘:’ will be present at the exact position of the string. We will use the two approaches first the brute force, and another the efficient approach. Sample Examples Input 1 Given string: 12:5? Output: 12:59 Explanation We have only one ... Read More

Python Program to get the subarray from an array using a specified range of indices

Gireesha Devara
Updated on 29-May-2023 14:33:08

15K+ Views

An array is a homogeneous data structure, which is used to store a set of elements of the same data type. And each element in the array is identified by key or index value. Subarray A subarray is defined as a small part of continuous elements of an array. if we take an array of 5 integer elements like below. [9, 3, 1, 6, 9] Then the subarrays will be − [9, 3] [9, 3, 1] [3, 1, 6, 9] Arrays in Python In Python we don't have a specific data structure to represent arrays. However, ... Read More

Generate all possible strings formed by replacing letters with given respective symbols

Prabhdeep Singh
Updated on 16-May-2023 13:41:10

358 Views

Generating all possible strings is to replace a character of a string with a respective symbol and produce all possible strings. We will be given a string ‘s’ of size ‘N’ and an unordered map ‘mp’ of a pair of characters of size ‘M’. Here we can replace the mp[i][0] with mp[i][1] in the string ‘s’ and by doing this our task is to generate all possible strings. Sample Examples Input: s = “xyZ”, mp = {‘x’ : ‘$’, ‘y’ : ‘#’, ‘Z’ : ‘^’} Output: xyZ xy^ x#Z z#^ $yZ $y^ $#Z $#^ Explanation − In the ... Read More

Python Program to push an array into another array

Gireesha Devara
Updated on 29-May-2023 14:27:12

3K+ Views

In programming array is a data structure, which is used to store collection of homogeneous data elements. And each element in the array is identified by an index value. But Python doesn’t have a specific data type to represent arrays. Instead, we can use the List as an array. Arrays in Python Here, we are representing List as an array. [1, 4, 6, 5, 3] The indexing in python starts from 0, so that the above array elements are accessed using their respective index values 0, 1, 2, 3, 4. Pushing an array into another array means, ... Read More

Advertisements