Server Side Programming Articles - Page 1922 of 2646

Play with Chips in C++

Arnab Chakraborty
Updated on 27-Apr-2020 09:10:14

430 Views

Suppose there are some chips, the i-th chip is at present at position chips[i]. We can perform any of the two following types of operations any many numbers of times as we want (possibly zero) on any chip −Move the i-th chip by 2 units to the left side or to the right side with a cost of 0.Move the i-th chip by 1 unit to the left side or to the right side with a cost of 1.Initially, there can be two or more chips. We have to return the minimum cost needed to move all the chips to ... Read More

Maximize Sum Of Array After K Negations in Python

Arnab Chakraborty
Updated on 27-Apr-2020 09:06:24

388 Views

Suppose we have an array A of integers, we have to modify the array in the following way −We can choose an i and replace the A[i] with -A[i], and we will repeat this process K times. We have to return the largest possible sum of the array after changing it in this way.So, if the array A = [4, 2, 3], and K = 1, then the output will be 5. So choose indices 1, the array will become [4, -2, 3]To solve this, we will follow these steps −Sort the array Afor i in range 0 to length ... Read More

Non-decreasing Array in Python

Arnab Chakraborty
Updated on 27-Apr-2020 09:12:39

1K+ Views

Suppose we have an array with n integers, our task is to check whether it could become nondecreasing by modifying at most one element. We can define an array is non-decreasing if it satisfies this rule: array[i] 0if arr[i - 1] > arr[i + 1], then arr[i + 1] := arr[i]return trueExample (Python)Let us see the following implementation to get a better understanding − Live Democlass Solution(object):    def checkPossibility(self, nums):       if len(nums) nums[i+1]:             if ans:                return False           ... Read More

Merge Two Binary Trees in C++

Arnab Chakraborty
Updated on 27-Apr-2020 08:59:01

775 Views

Suppose we have two binary trees and consider that when we put one of them to cover the other, some nodes of the two trees are overlapped while the others are overlapping. We have to merge them into a new binary tree. The merge rule is like that if two nodes are overlapping, then sum node values up as the new value of the merged node. Otherwise, the non-empty node will be used as the node of the new tree.So if the trees are −Then the output will be −To solve this, we will follow these steps −The method is ... Read More

Shortest Unsorted Continuous Subarray in Python

Arnab Chakraborty
Updated on 27-Apr-2020 08:55:20

355 Views

Suppose we have an integer array, we need to find one continuous subarray such that, if we only sort that subarray in ascending order, then the whole array will be sorted too. We need to find the shortest such subarray and output its length. So if the array is [2, 6, 4, 8, 10, 9, 15], then the output will be 5. The array will be [6, 4, 8, 10, 9]To solve this, we will follow these steps −res := sort the nums as an arrayans := 0set r as a linked listfor i in range 0 to the length ... Read More

Path Sum III in C++

Arnab Chakraborty
Updated on 04-May-2020 06:33:43

306 Views

Suppose we have given a binary tree in which each node holds an integer key. We have to find the paths that sum to a given value. The path should start from root to leaf. We have to find the path where the sum is same.If the tree is like [5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1], and sum is 22, then it will be −The paths are [[5, 4, 11, 2], [5, 8, 4, 5]].To solve this, we will follow these steps −Use the dfs function to solve this problem, the dfs is ... Read More

Palindrome Linked List in Python

Arnab Chakraborty
Updated on 27-Apr-2020 08:50:56

2K+ Views

Suppose we have a linked list. We have to check whether the list elements are forming a palindrome or not. So if the list element is like [1, 2, 3, 2, 1], then this is a palindrome.To solve this, we will follow these steps −fast := head, slow := head, rev := None and flag := 1if the head is empty, then return truewhile fast and next of fast is availableif next of the next of fast is available, then set flag := 0 and break the loopfast := next of the next of fasttemp := slow, slow := next ... Read More

Difference between monolithic and microservices architecture

Mahesh Parahar
Updated on 27-Jan-2020 10:37:48

15K+ Views

Monolithic architecture  is built as one large system and is usually one code-base. Monolithic application is tightly coupled and entangled as the application evolves, making it difficult to isolate services for purposes such as independent scaling or code maintainability.It extremely difficult to change technology or language or framework because everything is tightly coupled and depend on each other.Microservices architecture is built as small independent module based on business functionality. In microservices application, each project and services are independent from each other at the code level. Therefore it is easy to  configure and deploy completely and also easy to scale based ... Read More

Print a case where the given sorting algorithm fails in C++

sudhir sharma
Updated on 27-Jan-2020 06:50:32

140 Views

In this problem, we are given a sorting algorithm and a number n. Our task is to print an array of n elements that could not be sorted by the algorithm. i.e the algorithm will fail.Algorithmloop i from 1 to n-1    loop j from i to n-1    if a[j]>a[i+1]       swap(a[i], a[j+1])Let’s look into this sorting algorithm, it is using two nested loops. The outer one will start from 1 to n-1 and inner one from i to n-1, and will check the value of inner loop element and outer loop elements at each iteration and ... Read More

Print a closest string that does not contain adjacent duplicates in C++

sudhir sharma
Updated on 27-Jan-2020 06:47:12

160 Views

In this problem, we are given a string. Our task is to print a string that is closest to the current string and does not contain any adjacent duplicate characters.Let’s take an example to understand the problemInput: string = “good” Output: goadIn this example, we found that the elements at index 1 and 2 are the same, so we change the elements at index 2.To solve this problem, we will traverse the string and check if any two adjacent elements have are same. If yes, then change the second element (if i and i+1 elements are the same, change i+1 ... Read More

Advertisements