Server Side Programming Articles - Page 1796 of 2646

Program to find last digit of n’th Fibonnaci Number in C++

Ayush Gupta
Updated on 09-Oct-2020 08:47:13

601 Views

In this problem, we are given a number N. Our task is to create a Program to find last digit of Nth Fibonacci number in C++.Problem DescriptionWe need to find the last digit (i.e. LSB ) of the Nth Fibonacci number.Let’s take an example to understand the problem, Input: N = 120 Output: 1Solution ApproachA simple solution will be using the direct Fibonacci formula to find the Nth term. But this method will not be feasible when N is a large number. So to overcome this thing, we will use the property of the Fibonacci Series that the last digit ... Read More

Program to find largest element in an array in C++

Ayush Gupta
Updated on 04-May-2020 07:39:16

522 Views

In this tutorial, we will be discussing a program to find the largest element in an array.For this, we will be provided with an array. Our task is to find the largest number from the elements inside the array.Example Live Demo#include using namespace std; //finding largest integer int largest(int arr[], int n){    int i;    int max = arr[0];    //traversing other elements    for (i = 1; i < n; i++)    if (arr[i] > max)       max = arr[i];    return max; } int main(){    int arr[] = {10, 324, 45, 90, 9808};    int n = sizeof(arr) / sizeof(arr[0]);    cout

Program to find if a character is vowel or Consonant in C++

Ayush Gupta
Updated on 04-May-2020 07:35:56

266 Views

In this tutorial, we will be discussing a program to find if a character is a vowel or consonant.For this, we will be provided with a character. Our task is to print out to the user whether the provided character is a vowel or a consonant.Example Live Demo#include using namespace std; //checking if the character is a vowel or consonant void is_vowel(char x){    if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')       cout

Program to find HCF iteratively in C++

Ayush Gupta
Updated on 04-May-2020 07:21:14

277 Views

In this tutorial, we will be discussing a program to find HCF iteratively.For this, we will be provided with two numbers. Our task is to calculate the HCF of the given numbers using an iterative function.Example Live Demo#include using namespace std; int get_HCF(int a, int b){    while (a != b){       if (a > b)          a = a - b;       else          b = b - a;    }    return a; } int main(){    int a = 60, b = 96;    cout

Program to find N-th term of series 3, 5, 33, 35, 53… in C++

Ayush Gupta
Updated on 04-May-2020 07:19:07

177 Views

In this tutorial, we will be discussing a program to find N-th term of series 3, 5, 33, 35, 53…For this, we will be provided with a number. Our task is to find the term for the given series at that particular position.Example Live Demo#include using namespace std; //finding the nth term in the series int printNthElement(int n){    int arr[n + 1];    arr[1] = 3;    arr[2] = 5;    for (int i = 3; i

Random Pick with Weight in C++

Arnab Chakraborty
Updated on 02-May-2020 13:41:58

1K+ Views

Suppose we have an array w of positive integers, were w[i] describes the weight of index i, we have to define a function pickIndex() which randomly picks an index in proportion to its weight.So if the input is like [1, 3], call pickIndex() five times, then the answer may come as − 0, 1, 1, 1, 0.To solve this, we will follow these steps −Define an array v, Through the initializer, initialize asn := w[0]for i in range 1 to size of ww[i] := w[i] + w[i – 1]n := w[i]v = wThe pickIndex() will work as follows −Take a ... Read More

Contiguous Array in C++

Arnab Chakraborty
Updated on 02-May-2020 13:36:15

1K+ Views

Suppose we have a binary array, we have to find the maximum length of a contiguous subarray with equal number of 0 and 1. So if the input is like [0, 1, 0], then the output will be 2 as [0, 1] or [1, 0] is the largest contiguous array with equal number of 0s and 1s.To solve this, we will follow these steps −ret := 0, n := size of nums, sum := 0make a map m, set m[0] := - 1for i in range 0 to size of nums – 1sum := sum + 1 when nums[i] is ... Read More

Longest Word in Dictionary through Deleting in C++

Arnab Chakraborty
Updated on 02-May-2020 13:30:40

247 Views

Suppose we have a string and a string dictionary, we have to find the longest string in the dictionary that can be formed by deleting some of the characters of the given string. If there are more than one possible results, then just return the longest word with the smallest lexicographical order. If there is no result, then return a blank string. So if the input is like “abpcplea” and d = [“ale”, “apple”, “monkey”, “plea”], then the result will be “apple”.To solve this, we will follow these steps −Define a method called isSubsequence(). This will take s1 and s2j ... Read More

Continuous Subarray Sum in C++

Arnab Chakraborty
Updated on 02-May-2020 13:27:06

1K+ Views

Suppose we have a list of non-negative numbers and a target integer k, we have to write a function to check whether the array has a continuous subarray of size at least 2 that sums up to a multiple of k, sums up to n*k where n is also an integer. So if the input is like [23, 2, 4, 6, 7], and k = 6, then the result will be True, as [2, 4] is a continuous subarray of size 2 and sums up to 6.To solve this, we will follow these steps −Make a map m, set m[0] ... Read More

Random Flip Matrix in C++

Arnab Chakraborty
Updated on 02-May-2020 13:22:36

346 Views

Suppose we have a binary matrix with n_rows number of rows and n_cols number of columns. Here all values are initially 0. we have to define a function flip() which chooses a 0 value uniformly at random, changes it to 1, and then returns the position [row.id, col.id] of that value. Also, we have to write another function reset() which sets all values back to 0. We have to try to minimize the number of calls to system's Math.random() and optimize the time and space complexity.If we have the matrix of order 2x3, and we call flip four times, then ... Read More

Advertisements