Find the Fractional or N-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

First Natural Number Whose Factorial is Divisible by X in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:45:33

220 Views

We have to find the first natural number whose factorial is divisible by x. The x is given by the user. So if the x = 16, then output will be 6. as 6! mod 16 = 0. We will use general approach to solve this problem. iteratively count 1!, 2!, …. n! and check divisibility using x. If modulus is 0, then stop and return the number.Example Live Demo#include using namespace std; int getNumber(int x) {    int fact = 1;    int i = 0;    while(fact % x != 0){       i++;       fact = fact * i;    }    return i; } int main() {    int x = 16;    cout

Find Final X and Y in C++ Under Given Conditions

Arnab Chakraborty
Updated on 19-Dec-2019 12:40:59

101 Views

Consider we have the initial values of two positive integers X and Y. Find the final value of X and Y, such that there will be some alteration as mentioned below −step1 − If X = 0 and Y = 0 then terminate the process, otherwise go to step2step2 − If X >= 2Y, then set X = X – 2Y, and go to step1, otherwise go to step3step3 − If Y >= 2X, then set Y = Y – 2X, and go to step1, otherwise end the process.The number X and Y will be in range [0 and 1018] ... Read More

Count 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

Count of Strictly Decreasing Subarrays in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:34:05

257 Views

Suppose we have an array A. And we have to find the total number of strictly decreasing subarrays of length > 1. So if A = [100, 3, 1, 15]. So decreasing sequences are [100, 3], [100, 3, 1], [15] So output will be 3. as three subarrays are found.The idea is find subarray of len l and adds l(l – 1)/2 to result.Example Live Demo#include using namespace std; int countSubarrays(int array[], int n) {    int count = 0;    int l = 1;    for (int i = 0; i < n - 1; ++i) {       ... Read More

Count Numbers Formed Using Digits 3 and 4 in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:31:23

117 Views

Given a number N. We have to find the count of such numbers that can be formed using digit 3 and 4. So if N = 6, then the numbers will be 3, 4, 33, 34, 43, 44.We can solve this problem if we look closely, for single digit number it has 2 numbers 3 and 4, for digit 2, it has 4 numbers 33, 34, 43, 44. So for m digit numbers, it will have 2m values.Example Live Demo#include #include using namespace std; long long countNumbers(int n) {    return (long long)(pow(2, n + 1)) - 2; } int main() {    int n = 3;    cout

Find First Repeated Word in a String in Python Using Dictionary

Pradeep Elance
Updated on 19-Dec-2019 12:30:11

2K+ Views

In a given sentence there may be a word which get repeated before the sentence ends. In this python program, we are going to catch such word which is repeated in sentence. Below is the logical steps we are going to follow to get this result.Splits the given string into words separated by space.Then we convert these words into Dictionary using collectionsTraverse this list of words and check which first word has the frequency > 1Program - Find the Repeated WordIn the below program we use the counter method from the collections package to keep a count of the words.Example Live ... Read More

Exploring Correlation in Python

Pradeep Elance
Updated on 19-Dec-2019 12:26:02

576 Views

Correlation is a statistical term to measure the relationship between two variables. If the relationship is string, means the change in one variable reflects a change in another variable in a predictable pattern then we say that the variables are correlated. Further the variation in first variable may cause a positive or negative variation in the second variable. Accordingly, they are said to be positively or negatively correlated. Ideally the value of the correlation coefficient varies between -1 to +1.If the value is +1 or close to it then we say the variables are positively correlated. And they vary in ... Read More

Find Closest and Smaller Tidy Number in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:23:29

151 Views

Suppose we have a number n, we have to find the closest and smaller tidy number of n. So a number is called tidy number, if all of its digits are sorted in non-decreasing order. So if the number is 45000, then the nearest and smaller tidy number will be 44999.To solve this problem, we will traverse the number from end, when the tidy property is violated, then we reduce digit by 1, and make all subsequent digit as 9.Example Live Demo#include using namespace std; string tidyNum(string number) {    for (int i = number.length()-2; i >= 0; i--) {   ... Read More

Different Messages in Tkinter Python

Pradeep Elance
Updated on 19-Dec-2019 12:22:14

472 Views

Tkinter is the GUI module of python. It uses various message display options which are in response to the user actions or change in state of a running program. The message box class is used to display variety of messages like confirmation message, error message, warning message etc.Example-1The below example shows display of a message with the background colour, font size and colour etc customizable.import tkinter as tk main = tk.Tk() key = "the key to success is to focus on goals and not on obstacles" message = tk.Message(main, text = key) message.config(bg='white', font=('times', 32, 'italic')) message.pack() ... Read More

Advertisements