Programming Articles - Page 1750 of 3366

Find an integer X which is divisor of all except exactly one element in an array in Python

Arnab Chakraborty
Updated on 28-Aug-2020 11:56:56

164 Views

Suppose we have an array of numbers; we have to find a number B which is the divisor of all except for exactly one element in the given array. We have to keep in mind that the GCD of all the elements is not 1.So, if the input is like {8, 16, 4, 24}, then the output will be 8 as this is the divisor of all except 4.To solve this, we will follow these steps −n := size of arrayif n is same as 1, thenreturn(array[0] + 1)prefix := an array of size n, and fill with 0suffix := ... Read More

Find a string in lexicographic order which is in between given two strings in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:43:02

286 Views

Suppose we have two strings S and T, we have to check whether a string of the same length which is lexicographically bigger than S and smaller than T. We have to return -1 if no such string is available. We have to keep in mind that S = S1S2… Sn is termed to be lexicographically less than T = T1T2… Tn, provided there exists an i, so that S1= T1, S2= T2, … Si – 1= Ti – 1, Si < Ti.So, if the input is like S = "bbb" and T = "ddd", then the output will be ... Read More

Find a permutation that causes worst case of Merge Sort in C++

Arnab Chakraborty
Updated on 28-Aug-2020 08:25:54

158 Views

Suppose we have a set of elements; we have to find which permutation of these elements would result in worst case of Merge Sort? As we know asymptotically, merge sort always consumes O (n log n) time, but some cases need more comparisons and consumes more time. Here we have to find a permutation of input elements that will require higher number of comparisons when sorted implementing a typical Merge Sort algorithm.So, if the input is like [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26] , then the output will be [11, ... Read More

Find a pair with given sum in a Balanced BST in C++

Arnab Chakraborty
Updated on 28-Aug-2020 08:20:06

136 Views

Suppose we have a balanced binary search tree and a target sum, we have to define a method that checks whether it is a pair with sum equals to target sum, or not. In this case. We have to keep in mind that the Binary Search Tree is immutable.So, if the input is likethen the output will be (9 + 26 = 35)To solve this, we will follow these steps −Define stacks s1, s2done1 := false, done2 := falseval1 := 0, val2 := 0curr1 := root, curr2 := rootinfinite loop, do −while done1 is false, do −if curr1 is not ... Read More

Find a number which give minimum sum when XOR with every number of array of integer in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:12:07

129 Views

Suppose we have an array A, we have to find a number X such that (A[0] XOR X) + (A[1] XOR X) + … + A[n – 1] XOR X is as minimum as possible.So, if the input is like [3, 4, 5, 6, 7], then the output will be X = 7 , Sum = 10To solve this, we will follow these steps −Define a function search_res() . This will take arr, nelement := arr[0]for i in range 0 to size of arr, doif arr[i] > element, thenelement := arr[i]p := integer of (log of element base 2) + ... Read More

Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes in Python

Arnab Chakraborty
Updated on 27-Aug-2020 14:25:51

282 Views

Suppose we have two sorted linked lists, we have to make a linked list that consists of largest sum path from start node to end node. The final list may consist of nodes from both input lists.When we are creating the result list, we may switch to the other input list only for the point of intersection (two node with the same value in the lists). We have to solve it using constant amount of extra space.So, if the input is like [6, 8, 35, 95, 115, 125], [5, 8, 17, 37, 95, 105, 125, 135], then the output will ... Read More

Construct a linked list from 2D matrix in C++

Arnab Chakraborty
Updated on 27-Aug-2020 14:20:57

1K+ Views

Suppose we have one matrix, we have to convert it to 2d linked list using recursive approach.The list will have the right and down pointer.So, if the input is like102030405060708090then the output will beTo solve this, we will follow these steps −Define a function make_2d_list(), this will take matrix mat, i, j, m, n, if i and j are not in the matrix boundary, then −return nulltemp := create a new node with value mat[i, j]right of temp := make_2d_list(mat, i, j + 1, m, n)down of temp := make_2d_list(mat, i + 1, j, m, n)return tempExampleLet us see the ... Read More

Construct a linked list from 2D matrix (Iterative Approach) in C++

Arnab Chakraborty
Updated on 27-Aug-2020 14:18:10

226 Views

Suppose we have one matrix, we have to convert it to 2d linked list using iterative approach. The list will have the right and down pointer.So, if the input is like102030405060708090then the output will beTo solve this, we will follow these steps −real_head := NULLDefine an array head_arr of size: m.for initialize i := 0, when i < m, update (increase i by 1), do −head_arr[i] := NULLfor initialize j := 0, when j < n, update (increase j by 1), do −p := new tree node with value mat[i, j]if real_head is null, then −real_head := pif head_arr[i] is ... Read More

Construct a distinct elements array with given size, sum and element upper bound in Python

Arnab Chakraborty
Updated on 27-Aug-2020 14:14:11

187 Views

Suppose we have one size variable N, we also have one variable SUM this is the total sum of all elements available in the array and another variable K such that there is no element in array is greater than K, We have to find one orthogonal array where all elements in the array are distinct. If there is no solution return -1.So, if the input is like N = 4, SUM = 16 K = 9, then the output will be [1, 2, 4, 9]To solve this, we will follow these steps −minimum_sum := (N *(N + 1)) / ... Read More

Construct a DataFrame in Pandas using string data in Python

Arnab Chakraborty
Updated on 27-Aug-2020 14:11:58

658 Views

Here we will see how we can construct a pandas dataframe using string type data. Pandas supports csv files, but we can do the same using string also. For string type data, we have to use one wrapper, that helps to simulate as the data is taken as csv reader.Here we are using a string that takes data and separated by semicolon.ExampleLet us see the following implementation to get better understanding −import pandas as pd from io import StringIO str_data = StringIO("""Id;Subject;Course_Fee    10;DBMS;3000    11;Basic Maths;2000    12;Data Science;40000    13;Algorithm;5000    """) df = pd.read_csv(str_data, sep =";") print(df)OutputId ... Read More

Advertisements