Server Side Programming Articles - Page 1745 of 2646

Number of Segments in a String in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:45:28

591 Views

Suppose we have a string s. We have to count the number of segments in a string, where a segment is defined to be a contiguous sequence of characters (no whitespace).So, if the input is like "Hello, I love programming", then the output will be 4, as there are 4 segments.To solve this, we will follow these steps −n := 0for initialize i := 0, when i < size of s, update (increase i by 1), do −if s[i] is not equal to white space, then −(increase n by 1)while (i < size of s and s[i] is not equal ... Read More

Third Maximum Number in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:43:44

462 Views

Suppose we have a non-empty array of integers; we have to find the third maximum number in this array. If there is no 3rd max number, then return the maximum one. The challenge is, we have to solve this using linear time complexity.So, if the input is like [5, 3, 8, 9, 1, 4, 6, 2], then the output will be 6.To solve this, we will follow these steps −initialize a, b, c with NULLfor initialize i := 0, when i < size of nums, update (increase i by 1), do −if a is null or nums[i] >= value of ... Read More

Longest Palindrome in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:41:32

2K+ Views

Suppose we have a string which consists of lowercase or uppercase letters, we have to find the length of the longest palindromes that can be built with those letters. Now the string is case sensitive, so "Aa" is not considered a palindrome here.So, if the input is like "abccccdd", then the output will be 7, as one longest palindrome that can be built is "dccaccd", whose length is 7.To solve this, we will follow these steps −Define one map mpfor each character i in s(increase mp[i] by 1)ma := 0, c := 0, ans := 0for each key-value pair i ... Read More

Convert a Number to Hexadecimal in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:38:58

3K+ Views

Suppose we have an integer; we have to devise an algorithm to convert it to hexadecimal. For negative numbers we will use the two’s complement method.So, if the input is like 254 and -12, then the output will be fe and fffffff4 respectively.To solve this, we will follow these steps −if num1 is same as 0, then −return "0"num := num1s := blank stringwhile num is non-zero, do −temp := num mod 16if temp

Find the Difference in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:37:19

376 Views

Suppose we have two strings s and t which consist of only lowercase letters. Now, string t is generated by random shuffling string s and then add one more letter at a random index. We have to find the letter that was added in t.So, if the input is like "mnopq", "pqmnot", then the output will be "t", this is the extra letter.To solve this, we will follow these steps −sSum := 0, tSum := 0for initialize i := 0, when i < call length() of s, update (increase i by 1), do −sSum := sSum + s[i]for initialize j ... Read More

Guess Number Higher or Lower in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:36:04

645 Views

Suppose we are playing the Guess Game. The properties of this game is as follows −Player 1 will pick a number from 1 to n. player2 have to guess which number I picked. Every time player2 guess wrong, player1 will tell player2 whether the number is higher or lower.We can use the function guess(num) which will return 3 possible results as follows −-1 − Player1's number is lower1 − Player1's number is higher0 − Number is matchedSo, if the input is like n = 10, pick = 5, then the output will be 5.To solve this, we will follow these ... Read More

Intersection of Two Arrays in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:34:03

2K+ Views

Suppose we have two arrays; we have to find their intersections.So, if the input is like [1, 5, 3, 6, 9], [2, 8, 9, 6, 7], then the output will be [9, 6]To solve this, we will follow these steps −Define two maps mp1, mp2Define an array resfor x in nums1(increase mp1[x] by 1)for x in nums2(increase mp2[x] by 1)for each key-value pair x in mp1cnt := 0cnt := minimum of value of x and mp2[key of x]if cnt > 0, then −insert key of x at the end of resreturn resExample Let us see the following implementation to get a ... Read More

Power of Four in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:31:41

203 Views

Suppose we have an integer; we have to check whether that is a power of 4 or not.So, if the input is like 16, then the output will be True.To solve this, we will follow these steps −if num < 0, then −return falseif num & (num - 1) is non-zero, then −return falseif (num & 01010101010101010101010101010101) is zero, then −return falsereturn trueExample Let us see the following implementation to get better understanding − Live Demo#include using namespace std; class Solution { public:    bool isPowerOfFour(int num){       if (num < 0)          return false;   ... Read More

Nim Game in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:30:49

1K+ Views

Suppose we are playing a game called, Nim Game with another player. There is a heap of stones, each time one player takes turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. Player1 will take the first turn to remove the stones. Both of the players are very clever and have optimal strategies for the game. We have to devise an algorithm to determine whether player1 can win the game given the number of stones in the heap.So, if the input is like 5, then the output will be true, as ... Read More

Word Pattern in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:28:29

580 Views

Suppose we have a pattern and a string str, find if str follows the same pattern. Here follow means there is a bijection between a letter in pattern and a non-empty word in str.So, if the input is like pattern = "cbbc", str = "word pattern pattern word", then the output will be True.To solve this, we will follow these steps −strcin := strDefine an array of wordsfor each word in strcininsert word at the end of wordsDefine one map p2ii := 0pat := empty stringfor c in pattern −if c is not member of p2i, then −(increase i by ... Read More

Advertisements