Programming Articles

Page 579 of 2547

Maximize Sum Of Array After K Negations in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 426 Views

Given an array of integers, we need to maximize the sum by negating exactly K elements. We can choose any element and replace it with its negative value, repeating this process K times. The strategy is to first negate all negative numbers (since negating them increases the sum), then handle remaining negations optimally. Algorithm The approach involves these steps: Sort the array to process negative numbers first Negate negative numbers until K operations are exhausted or no negatives remain If K operations remain and K is odd, negate the smallest positive number Return the sum ...

Read More

Non-decreasing Array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

A non-decreasing array is one where each element is less than or equal to the next element: array[i] nums[i+1] and count them ? If the array has 2 or fewer elements, return True Track violations using a boolean flag For each violation, decide whether to modify nums[i] or nums[i+1] If more than one violation exists, return False Implementation def checkPossibility(nums): if len(nums) nums[i + 1]: # If we already found a violation, return False ...

Read More

Shortest Unsorted Continuous Subarray in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 387 Views

Given an integer array, we need to find the shortest continuous subarray that, when sorted, makes the entire array sorted. For example, in the array [2, 6, 4, 8, 10, 9, 15], the subarray [6, 4, 8, 10, 9] needs to be sorted to make the whole array sorted, so the answer is 5. Approach We compare the original array with its sorted version to identify positions where elements differ. The shortest subarray spans from the first differing position to the last differing position. Algorithm Steps Create a sorted copy of the input array Compare ...

Read More

Palindrome Linked List in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 2K+ Views

A palindrome linked list is a linked list that reads the same forwards and backwards. For example, [1, 2, 3, 2, 1] forms a palindrome because it's identical when reversed. The most efficient approach uses the two-pointer technique with partial reversal to check palindrome in O(n) time and O(1) space. Algorithm Steps Use fast and slow pointers to find the middle of the linked list Reverse the first half while traversing to the middle Compare the reversed first half with the second half Return True if all elements match, False otherwise Implementation ...

Read More

Loop Control Statements in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 984 Views

Loop control statements in Python allow you to change the execution flow of loops. These statements provide flexibility to terminate loops early, skip iterations, or act as placeholders in your code structure. The break Statement The break statement terminates the loop immediately and transfers control to the statement following the loop ? # Using break in a for loop numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for num in numbers: if num == 5: print(f"Found {num}, breaking the ...

Read More

Data Type Conversion in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 9K+ Views

Data type conversion in Python allows you to transform one data type into another. Python provides built-in functions to perform these conversions, which return new objects representing the converted values. Common Type Conversion Functions Function Description Example int() Converts to integer int("42") → 42 float() Converts to floating-point float("3.14") → 3.14 str() Converts to string str(42) → "42" bool() Converts to boolean bool(1) → True list() Converts to list list("abc") → ['a', 'b', 'c'] tuple() Converts to tuple tuple([1, 2, 3]) → (1, 2, ...

Read More

String Data Type in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 3K+ Views

Strings in Python are identified as a contiguous set of characters represented in quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator [ ] and [:] with indexes starting at 0 in the beginning of the string and working their way from -1 at the end. String Creation You can create strings using single quotes, double quotes, or triple quotes for multi-line strings ? # Different ways to create strings single_quote = 'Hello World!' double_quote = "Hello World!" multi_line = """This is a ...

Read More

Number Data Type in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 580 Views

Number data types store numeric values. Number objects are created when you assign a value to them. For example − var1 = 1 var2 = 10 print(var1, var2) 1 10 You can also delete the reference to a number object by using the del statement. The syntax of the del statement is − del var1[, var2[, var3[...., varN]]]] You can delete a single object or multiple objects by using the del statement. For example − number1 = 42 number2 = 3.14 # Delete single object del ...

Read More

Multiple Statements in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 5K+ Views

Python allows you to write multiple statements on a single line or group them into code blocks called suites. This flexibility helps organize your code efficiently. Multiple Statements on a Single Line The semicolon (;) allows multiple statements on the same line, provided that neither statement starts a new code block ? import sys; x = 'foo'; sys.stdout.write(x + '') foo Example with Variables You can assign multiple variables and perform operations on a single line ? a = 5; b = 10; result = a + b; ...

Read More

Quotation in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 5K+ Views

Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same type of quote starts and ends the string. Types of Quotes in Python Python supports three types of quotation marks for creating strings. Each has its specific use cases and advantages. Single Quotes Single quotes are the most basic way to create strings in Python ? word = 'Hello' name = 'Python' print(word) print(name) Hello Python Double Quotes Double quotes work exactly like single quotes but are ...

Read More
Showing 5781–5790 of 25,466 articles
« Prev 1 577 578 579 580 581 2547 Next »
Advertisements