Found 26504 Articles for Server Side Programming

Single Element in a Sorted Array in C++

Arnab Chakraborty
Updated on 29-Apr-2020 05:54:10

387 Views

Suppose we have a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. we have to find this single element that appears only once. So if the array is like [1, 1, 2, 3, 3, 4, 4, 8, 8], then the output will be 2To solve this, we will follow these steps −ans := 0for i in range 0 to nums array sizeans := ans XOR nums[i]return ansExample(C++)Let us see the following implementation to get a better understanding − Live Demo#include using namespace std; class Solution { public:   ... Read More

Coin Change 2 in C++

Arnab Chakraborty
Updated on 29-Apr-2020 05:51:30

516 Views

Suppose we have coins of different denominations and a total amount of money. we have to Write a module to compute the number of combinations that make up that amount. we can assume that we have infinite number of each kind of coin. So if the amount is 5 and coins are [1, 2, 5], then there are four combinations. (1+1+1+1+1), (1+1+1+2), (1+2+2), (5)To solve this, we will follow these steps −create one array dp of size amount + 1dp[0] := 1n := size of coins arrayfor i in range 0 to n – 1for j in range coins[i] to ... Read More

Next Greater Element II in C++

Arnab Chakraborty
Updated on 29-Apr-2020 05:46:18

242 Views

Suppose we have a circular array (the next element of the last element is the first element of the array), we have to display the Next Greater Number for every element. Here the Next Greater Number of a number x is the first greater number to its traversing-order next in the array, this means we could search circularly to find its next greater number. If it is not present, then it will be -1. So if the numbers are [1, 2, 1, 3, 2, 1], then output will be: [2, 3, 3, -1, 3, 2]To solve this, we will follow ... Read More

Target Sum in C++

Arnab Chakraborty
Updated on 29-Apr-2020 05:41:25

714 Views

Suppose we have a list of non-negative integers, a1, a2, ..., an, and another value, that is target, S. Now we have 2 symbols + and -. For each integer, we should choose one from + and - as its new symbol. we have to find out how many ways to assign symbols to make sum of integers same as the target value S. So if the numbers are [1, 1, 1, 1, 1], and S = 3, then the output will be 5, as the combinations are – 1 + 1 + 1 + 1 + 1 = 3, ... Read More

Minimum Moves to Equal Array Elements II in Python

Arnab Chakraborty
Updated on 28-Apr-2020 13:41:57

461 Views

Suppose we have a non-empty integer array, we have to find the minimum number of moves that are required to make all array elements equal, where a move is incrementing or decrementing a selected element by 1. So when the array is like [1, 2, 3], then the output will be 2, as 1 will be incremented to 2, and 3 will be decremented to 2.To solve this, we will follow these steps −sort the array numsset counter as 0for i in nums, docounter := counter + absolute of (i – nums[length of nums / 2])return counterExample(Python)Let us see the ... Read More

4Sum II in Python

Arnab Chakraborty
Updated on 28-Apr-2020 13:09:00

796 Views

Suppose we have four lists A, B, C, D of integer values, we have to compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. Consider all A, B, C, D have same length of N where 0 ≤ N ≤ 500. Remember all integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1. So if the inputs are A = [1, 2], B = [-2, -1], C = [-1, 2], D = [0, 2], then ... Read More

Sort Characters By Frequency in C++

Arnab Chakraborty
Updated on 28-Apr-2020 13:01:13

2K+ Views

Suppose we have a string, we have to sort the characters based on the frequency. So if the string is like “abbbacbcc”, then the output will be “bbbbcccaa”To solve this, we will follow these steps −create an array of pairs called v, create one map mfor all characters in string, increase value of m[character] by 1i := first element of mapwhile map has elementsinsert (i.second, i.first) into vand increase i to point to the next elementsort the vector vans := an empty stringfor i := 0 to size of vt := first element of v[i]while t is not 0ans := ... Read More

Find All Duplicates in an Array in C++

Arnab Chakraborty
Updated on 02-Dec-2024 00:25:56

11K+ Views

Suppose we have an array of integers, in range 1 ≤ a[i] ≤ n (n = size of an array), here some elements appear twice and others appear once. We have to find all the elements that appear twice in this array. So if the array is [4, 3, 2, 7, 8, 2, 3, 1], then the output will be [2, 3]. Approach To solve this, we will follow these steps − n := size of array, make one array called ans for i in range 0 to n – 1 ... Read More

Longest Repeating Character Replacement in C++

Arnab Chakraborty
Updated on 28-Apr-2020 12:42:36

2K+ Views

Suppose we have given a string s that consists of only uppercase letters, we can perform at most k operations on that string. In one operation, we can select any character of the string and change it to any other uppercase letters. We have to find the length of the longest sub-string containing all repeating letters we can get after performing the above operations. So if the input is like: “ABAB” and k = 2, then the output will be 4. This is because two ‘A’s with two ‘B’s or vice versa.To solve this, we will follow these steps −maxCount ... Read More

Reconstruct Original Digits from English in C++

Arnab Chakraborty
Updated on 28-Apr-2020 12:32:27

406 Views

Suppose we have a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order. There are some properties −Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.Input length is less than 50, 000.So if the input is like “fviefuro”, then the output will be 45.To solve this, we will follow these steps −nums := an array that is holding the numbers in English letter from 0 to 9.make one array count of size 10ans := an empty ... Read More

Advertisements