Server Side Programming Articles - Page 1990 of 2650

Python Program for Basic Euclidean algorithms

Sarika Singh
Updated on 13-Jun-2025 18:14:27

627 Views

Euclidean AlgorithmThe Euclidean Algorithm is used to find the Greatest Common Divisor (GCD) of two numbers. The GCD of two integers is the largest number that divides both of them without leaving a remainder. This algorithm is based on the principle that the GCD of two numbers also divides their difference. The method repeatedly replaces the larger number by the remainder of dividing the larger number by the smaller one, until one of the numbers becomes zero. Problem Statement You are given two positive integers a and b. You need to find the greatest common divisor (GCD) of these two ... Read More

Python Program for array rotation

Pavitra
Updated on 20-Dec-2019 05:23:17

201 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − Given a text and a pattern, we need to print all occurrences of pattern and its permutations (or anagrams) in text.Now let’s observe the solution in the implementation below −Example Live Demo# maximum value MAX = 300 # compare def compare(arr1, arr2):    for i in range(MAX):       if arr1[i] != arr2[i]:          return False    return True # search def search(pat, txt):    M = len(pat)    N = len(txt)    # countP pattern account    # countTW ... Read More

Python Program for Activity Selection Problem

Sarika Singh
Updated on 13-Jun-2025 18:11:19

1K+ Views

The activity selection problem selects the maximum number of non-overlapping activities from a given set. Each activity has a start and finish time, and a single person can perform only one activity at a time. Problem Statement You are given n activities, each defined by a start time and a finish time. The goal is to choose the maximum number of activities that a single person can perform without any overlap between the selected activities. Variable Notations Below are the variables used in the problem definition: N: Total number of activities S: ... Read More

Python Program for 0-1 Knapsack Problem

Sarika Singh
Updated on 10-Jun-2025 11:59:57

5K+ Views

Knapsack is a classic problem that involves decision-making based on constraints. Suppose we have a knapsack with fixed capacity, and items with fixed values and sizes (it might be volume of the item or weight). We need to add items into the knapsack such that we pack the maximum value items. From the available combinations, we need to provide an optimized solution. There are three types of knapsack problems 0-1 Knapsack: We can either pick an item or not (0 or 1). Fractional Knapsack: We can pick a part of an ... Read More

Find the second last node of a linked list in single traversal in C++

Arnab Chakraborty
Updated on 19-Dec-2019 13:02:06

683 Views

Now we will see how to get the second last element in the linked list. Suppose there are few elements like [10, 52, 41, 32, 69, 58, 41], second last element is 58.To solve this problem, we will use two pointers one will point to current node, and another will point to previous node of the current position, then we will move until the next of current is null, then simply return the previous node.Example Live Demo#include using namespace std; class Node {    public:       int data;       Node *next; }; void prepend(Node** start, int new_data) ... Read More

Find the Second Largest Element in a Linked List in C++

Arnab Chakraborty
Updated on 19-Dec-2019 13:00:00

491 Views

Here we will see the second largest element in the linked list. Suppose there are n different nodes with numerical values. So if the list is like [12, 35, 1, 10, 34, 1], then second largest element will be 34.This process is similar to the finding of second largest element in an array, we will traverse through the list and find second largest element by comparing.Example#include using namespace std; class Node {    public:       int data;       Node *next; }; void prepend(Node** start, int new_data) {    Node* new_node = new Node;    new_node->data = ... Read More

Find the next identical calendar year in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:54:41

252 Views

Suppose we have an year Y. Find next identical calendar year to Y. So the calendar of 2017 is identical with 2023.A year X is identical to given previous year Y if it matches these two conditions.x starts with the same day as year, If y is leap year, then x also, if y is normal year, then x also normal year.The idea is to check all years one by one from next year. We will keep track of number of days moved ahead. If there are total 7 moved days, then current year begins with same day. We also ... Read More

Find the fractional (or n/k – th) node in linked list in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:51:30

279 Views

Suppose we have a singly linked list and the number k. We have to write a function to find the (n/k)th element, where n is the number of elements in the list. For decimals, we will choose the ceiling values. So if the list is like 1, 2, 3, 4, 5, 6, and k = 2, then output will be 3, as n = 6 and k = 2, then we will print n/k th node so 6/2 th node = 3rd node that is 3.To solve this we have to follow some steps like below −Take two pointers called ... Read More

To print all elements in sorted order from row and column wise sorted matrix in Python

Niharikaa Aitam
Updated on 20-Jun-2025 19:18:12

318 Views

In Python, we may go through the matrices that are sorted in a particular order. A common case is a matrix where each row and each column is sorted in increasing order, and this does not mean that the entire matrix is sorted. In such cases, we need to extract all elements and print them in a fully sorted order. Row and Column-wise Sorted Matrix? A row and column-wise sorted matrix means each row is sorted from left to right, and each column is sorted from top to bottom. For example, let's consider the following matrix - matrix = ... Read More

Find the count of substrings in alphabetic order in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:37:36

158 Views

Suppose we have a string of length n. It contains only uppercase letters. We have to find the number of substrings whose character is occurring in alphabetical order. Minimum size of the substring will be 2. So if the string is like: “REFJHLMNBV”, and substring count is 2, they are “EF” and “MN”.So to solve this, we will follow these steps −Check whether str[i] + 1 is same as the str[i+1], if so, then increase the result by 1, and iterate the string till next character which is out of alphabetic order, otherwise continue.Example Live Demo#include using namespace std; int countSubstr(string ... Read More

Advertisements