Suppose we have an array A (index starts at 1) with N numbers: A1, A2, ..., AN and another integer B. The integer B denotes that from any index is i in the array A, we can jump to any one of the places in the array A indexed i+1, i+2, …, i+B if this place can be jumped to. Also, if we step on the index i, we have to pay Ai amount of coins. If Ai is -1, it means we can’t jump to the place indexed i in the array.Now, when we start from the place indexed ... Read More
Suppose we have an array with n integers, we have to find the contiguous subarray whose length is greater than or equal to k that has the maximum average value. We have to find the maximum average value.So, if the input is like [1, 12, -5, -6, 50, 3], k = 4, then the output will be 12.75, as when length is 5, maximum average value is 10.8, when length is 6, maximum average value is 9.16667. Thus output is 12.75.To solve this, we will follow these steps −Define a function ok(), this will take x, an array nums, k, ... Read More
Suppose one company wants to give one of its best employees the option to travel among N cities to collect some resources. But employees want some vacations also, we could take vacations in some particular cities and weeks. Our task is to schedule the traveling to maximize the number of vacation days we could take, but there are certain rules and restrictions we have to follow.We can only travel among N cities; they are represented by indexes from 0 to N-1. Firstly, we are in the city indexed 0 on Monday.These cities are connected by flights. We have one N ... Read More
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string input, we need to convert it into dictionary typeHere we will discuss two methods to solve the problem without using a built-in dict() function.Method 1 − Using eval() methodEval method is used only when the syntax or the formation of string resembles that of a dictionary. Direct conversion of string to the dictionary can happen in that case as discussed below.Example Live Demo# String string = "{'T':1, 'U':2, 'T':3, 'O':4, 'R':5}" # eval() function dict_string = eval(string) print(dict_string) print(dict_string['T']) print(dict_string['T'])Output{'T': ... Read More
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 find the occurrence of each character in a given string.Here we will be discussing 3 approaches as discussed below:LApproach 1 − The brute-force approachExample Live Demotest_str = "Tutorialspoint" #count dictionary count_dict = {} for i in test_str: #for existing characters in the dictionary if i in count_dict: count_dict[i] += 1 #for new characters to be added else: count_dict[i] = 1 print ("Count of all characters in Tutorialspoint ... Read More
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to display the second largest number in a list.There are three approaches to solve the problem−Approach 1 − We use the set() function & remove() functionExample Live Demolist1 = [11, 22, 1, 2, 5, 67, 21, 32] # to get unique elements new_list = set(list1) # removing the largest element from list1 new_list.remove(max(new_list)) # now computing the max element by built-in method? print(max(new_list))Output32Approach 2 − We use sort() method and negative indexesExample Live Demolist1 = [11, 22, 1, 2, ... Read More
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given two integers, we need to print the second maximum value in the dictionaryNow let’s observe the concept in the implementation below−Approach 1 − Using sorted() function by negative indexesExample Live Demo#input example_dict ={"tutor":3, "tutorials":15, "point":9, "tutorialspoint":19} # sorting the given list and get the second last element print(list(sorted(example_dict.values()))[-2])Output15Approach 2 − Here we use sort method on the list and then access the second largest elementExample Live Demolist1 = [11, 22, 1, 2, 5, 67, 21, 32] # using built-in sort method list1.sort() # ... Read More
Suppose we have an array of n unique strings, We have to generate minimal possible abbreviations for every word following rules below.Starting with the first character and then the number of characters abbreviated, which followed by the last character.If we find any conflict and that is more than one words share the same abbreviation, a longer prefix can be used instead of only the first character until making the map from word to abbreviation become unique.When the abbreviation doesn't make the word shorter, then keep it as original.So, if the input is like ["like", "god", "internal", "me", "internet", "interval", "intension", ... Read More
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to count even and odd numbers in a list.There are three methods as discussed below−Approach 1 − Using brute-force approachExample Live Demolist1 = [21, 3, 4, 6, 33, 2, 3, 1, 3, 76] even_count, odd_count = 0, 0 # enhanced for loop for num in list1: #even numbers if num % 2 == 0: even_count += 1 #odd numbers else: odd_count += 1 print("Even numbers available in the ... Read More
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list iterable, we need to count positive and negative numbers in it and display them.Approach 1 − Brute-force approach using iteration construct(for)Here we need to iterate each element in the list using a for loop and check whether num>=0, to filter the positive numbers. If the condition evaluates to be true, then increase pos_count otherwise, increase neg_count.Example Live Demolist1 = [1, -2, -4, 6, 7, -23, 45, -0] pos_count, neg_count = 0, 0 # enhanced for loop for num in ... Read More