Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 652 of 855
Find Indexs of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array - Set-2 in Python
In binary arrays, we often need to find the optimal position to flip a 0 to 1 to create the longest possible sequence of consecutive 1s. This problem requires tracking sequences on both sides of each 0 to determine which replacement yields maximum benefit. Given a binary array like [1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1], we need to find which 0 should be replaced to get the longest continuous sequence of 1s. The answer is index 10, creating [1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1]. ...
Read MoreFind index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in Python
Given two strings S1 and S2 of the same length, we need to find an index i such that the prefix S1[0...i] and suffix S2[i+1...n-1] form a palindrome when concatenated. If no such index exists, return -1. For example, with S1 = "pqrsu" and S2 = "wxyqp", we find that S1[0..1] = "pq" and S2[2..4] = "yqp" combine to form "pqyqp", which is a palindrome at index 1. Algorithm To solve this problem, we follow these steps − Initialize n as the length of both strings For each index i from 0 to n-1: ...
Read MoreFind if there is a path of more than k length from a source in Python
Finding a path longer than a given length in a graph is a classic graph traversal problem. We need to determine if there exists a simple path (without cycles) from a source vertex to any other vertex with total edge weights exceeding a threshold k. 0 1 2 ...
Read MoreFind if neat arrangement of cups and shelves can be made in Python
Suppose we have three different types of cups in array p and saucers in array q, and m number of shelves. We need to check whether a neat arrangement of cups and saucers can be made following specific constraints. The arrangement is considered neat if it follows these conditions: No shelf can hold both cups and saucers A shelf may contain at most 5 cups A shelf may contain at most 10 saucers Problem Understanding Given arrays p = [4, 3, 7], q = [5, 9, 10], and m = 11, we need to ...
Read MoreFind if it is possible to get a ratio from given ranges of costs and quantities in Python
Suppose we have a range of cost from lowCost to upCost and another range of quantity from lowQuant to upQuant. We need to check whether we can find the given ratio r where r = cost/quantity, with lowCost ≤ cost ≤ upCost and lowQuant ≤ quantity ≤ upQuant. So, if the input is like lowCost = 2, upCost = 10, lowQuant = 3, upQuant = 9 and r = 3, then the output will be True as the cost = r * quantity = 3 * 3 = 9 where cost is in range [2, 10] and quantity is ...
Read MoreFind if given vertical level of binary tree is sorted or not in Python
In binary tree problems, we often need to analyze nodes at specific vertical levels. A vertical level represents all nodes that are at the same horizontal distance from the root. This problem asks us to check if nodes at a given vertical level are sorted in ascending order. Understanding Vertical Levels In a binary tree, vertical levels are determined by horizontal distance from the root ? Root node is at vertical level 0 Left child is at (parent's level - 1) Right child is at (parent's level + 1) ...
Read MoreFind if an undirected graph contains an independent set of a given size in Python
An independent set in an undirected graph is a set of vertices where no two vertices are adjacent (directly connected by an edge). This article demonstrates how to find if a graph contains an independent set of a given size using Python. We need to check whether a graph contains an independent set of size k. If such a set exists, return "Yes", otherwise "No". Problem Understanding Given an undirected graph represented as an adjacency matrix, we want to find k vertices such that none of them are connected to each other. For example, if k = ...
Read MoreFind Four points such that they form a square whose sides are parallel to x and y axes in Python
Finding four points that form a square with sides parallel to the coordinate axes is a geometric problem. We need to identify diagonal points first, then verify if the other two corner points exist in our point set. Problem Approach To form a square with sides parallel to x and y axes, we need four points that satisfy these conditions ? Two points should form a diagonal of the square The diagonal points must have equal x-distance and y-distance The other two corner points must exist in our point set If multiple squares exist, select the ...
Read MoreFind four missing numbers in an array containing elements from 1 to N in Python
Finding missing numbers in an array is a common programming problem. When we have an array of distinct numbers from range [1, N] with size (N-4), exactly four numbers are missing. This problem can be solved efficiently using array manipulation techniques. Problem Understanding Given an array of distinct numbers where: Each number lies in range [1, N] Array size is (N-4) No element is repeated We need to find 4 missing numbers in sorted order For example, if A = [2, 8, 4, 13, 6, 11, 9, 5, 10], then N = 13 and the ...
Read MoreFind First element in AP which is multiple of given Prime in Python
In this problem, we need to find the position of the first element in an Arithmetic Progression (AP) that is divisible by a given prime number. Given the first term A, common difference D, and prime number P, we use modular arithmetic to efficiently find this position. For example, if A = 3, D = 4, P = 5, the AP terms are: 3, 7, 11, 15, 19... The fourth term (15) is the first multiple of 5, so the answer is 3 (0-indexed position). Algorithm Approach The solution uses Fermat's Little Theorem and modular exponentiation. For ...
Read More