Found 33676 Articles for Programming

Maximum subarray sum in an array created after repeated concatenation in C++

Ayush Gupta
Updated on 10-Jul-2020 14:08:47

124 Views

In this tutorial, we will be discussing a program to find maximum subarray sum in an array created after repeated concatenation.For this we will be provided with an array and an integer K. Our task is to find the subarray with the maximum elements when the given array is repeated K times.Example Live Demo#include using namespace std; //returning sum of maximum subarray int maxSubArraySumRepeated(int a[], int n, int k) {    int max_so_far = INT_MIN, max_ending_here = 0;    for (int i = 0; i < n*k; i++) {       max_ending_here = max_ending_here + a[i%n];       if ... Read More

Maximum Subarray Sum Excluding Certain Elements in C++

Ayush Gupta
Updated on 10-Jul-2020 14:07:10

141 Views

In this tutorial, we will be discussing a program to find maximum Subarray Sum Excluding Certain Elements.For this we will be provided with two arrays of size M and N. Our task is to find a sub-array in the first array such that no element inside the subarray is present inside the second array and the elements of subarray sum up to be maximum.Example Live Demo#include using namespace std; //checking if element is present in second array bool isPresent(int B[], int m, int x) {    for (int i = 0; i < m; i++)    if (B[i] == x) ... Read More

Maximum subarray size, such that all subarrays of that size have sum less than k in C++

Ayush Gupta
Updated on 10-Jul-2020 14:05:05

156 Views

In this tutorial, we will be discussing a program to find maximum subarray size, such that all subarrays of that size have sum less than k.For this we will be provided with an array of size N and an integer k. Our task is to find the length of subarray such that all the sub-arrays of that length in the given array sum to be less than or equal to k.Example Live Demo#include using namespace std; //finding maximum length subarray int bsearch(int prefixsum[], int n, int k) {    int ans = -1;    //performing binary search    int left = ... Read More

Maximum students to pass after giving bonus to everybody and not exceeding 100 marks in C++

Ayush Gupta
Updated on 10-Jul-2020 14:01:45

152 Views

In this tutorial, we will be discussing a program to find maximum students to pass after giving bonus to everybody and not exceeding 100 marks.For this we will be provided with an array containing marks of N students. Our task is to get more student pass the exam (50 marks required) by giving each student the same amount of bonus marks without any student exceeding 100 marks.Example Live Demo#include #include using namespace std; int check(int n, int marks[]) {    int* x = std::max_element(marks, marks+5);    int bonus = 100-(int)(*x);    int c = 0;    for(int i=0;    i= 50) ... Read More

Python - Contiguous Boolean Range

Pradeep Elance
Updated on 10-Jul-2020 11:36:51

341 Views

Given a list of values, we are interested to know at which position are the Boolean values present as a contiguous list. Which means after we encounter a value which is TRUE there is a continuous value of true from that position until FALSE value is found. Similarly when a FALSE is found there is a contiguous value of FALSE until TRUE is found.With itertoolsW can use accumulate along with groupby from the itertools module. In this example we take a given list and then apply the accumulate function to keep track of the values that are brought together using ... Read More

Python - Column deletion from list of lists

Pradeep Elance
Updated on 10-Jul-2020 11:34:40

1K+ Views

In a list of lists an element at the same index of each of the sublist represents a column like structure. In this article we will see how we can delete a column from a list of lists. Which means we have to delete the element at the same index position from each of the sublist.Using popWe use the pop method which removes the element at a particular position. A for loop is designed to iterate through elements at specific index and removes them using pop.Example Live Demo# List of lists listA = [[3, 9, 5, 1], [4, 6, 1, 2], ... Read More

Python - Check if given words appear together in a list of sentence

Pradeep Elance
Updated on 10-Jul-2020 11:32:49

1K+ Views

Say we have a list containing small sentences as its elements. We have another list which contains some of the words used in this sentences of the first list. We want to find out if two words from the second list are present together in some of the sentences of the first list or not.With append and for loopWe use the for loop with in condition to check for the presence of words in the lists of sentences. Then we use the len function to check if we have reached the end of the list.Example Live Demolist_sen = ['Eggs on Sunday', ... Read More

Python - Check if all elements in a list are identical

Pradeep Elance
Updated on 10-Jul-2020 11:30:02

932 Views

There may be occasions when a list will contain all the values which are same. In this article we will see various way to verify that.With allWe use the all function to find the result of comparison of each element of the list with the first element. If each comparison gives a result of equality then the result is given as all elements are equal else all elements are not equal.Example Live DemolistA = ['Sun', 'Sun', 'Mon'] resA = all(x == listA[0] for x in listA) if resA:    print("in ListA all elements are same") else:    print("In listA ... Read More

Python - Check if a list is contained in another list

Pradeep Elance
Updated on 10-Jul-2020 11:28:07

1K+ Views

Given two different python lists we need to find if the first list is a part of the second list.With map and joinWe can first apply the map function to get the elements of the list and then apply the join function to cerate a comma separated list of values. Next we use the in operator to find out if the first list is part of the second list.Example Live DemolistA = ['x', 'y', 't'] listB = ['t', 'z', 'a', 'x', 'y', 't'] print("Given listA elemnts: ") print(', '.join(map(str, listA))) print("Given listB elemnts:") print(', '.join(map(str, listB))) res = ', '.join(map(str, ... Read More

Python - Check if a given string is binary string or not

Pradeep Elance
Updated on 10-Jul-2020 11:25:39

7K+ Views

In this article we check if a given string has characters which are only 1 or 0. We call such strings as binary strings. If it has any other digit like 2 or 3 etc., we classify it as a non-binary string.With setThe set operator in python stores only unique elements. So we take a string and apply the set function to it. Then we create another set which has only 0 and 1 as its elements. If both these sets are equal then the string is definitely binary. Also the string may have only 1s or only 0s. So ... Read More

Advertisements