sudhir sharma

sudhir sharma

975 Articles Published

Articles by sudhir sharma

Page 56 of 98

Primality test for the sum of digits at odd places of a number in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 165 Views

In this problem, we are given a number N. our task is to check whether the sum of digits at odd place of the number gives a prime number or not.Primality Test is the algorithm that is used to check whether the given number is prime or not.Let’s take an example to understand the problem, Input: 3425 Output: No Explanation: sum digits at odd place = 5 + 4 = 9, which is not a prime number.To solve this problem an easy approach would be adding all digits that are at odd places in the number and then checking whether ...

Read More

Primality Test in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 2K+ Views

In this problem, we are given a number N and our task is to check whether it is a prime number or not.Primality test s the algorithm that is used to check whether the given number is prime or not.Prime number is a number which can be divided by itself only. Example : 2, 3, 5, 7.Let’s take an example to understand our problem, Input: 11 Output: YesThere are multiple methods to check for primality test of a number.One simple method to check for primality is by checking the division of the number by all numbers less than N. If ...

Read More

Previous smaller integer having one less number of set bits in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 207 Views

In this problem, we are given an integer n. Our task is to print the largest number less than n which can be formed by changing one set bit of the binary representation of the number.Let’s take an example to understand the problemInput: n = 3 Output: 2 Explanation: (3)10 = (011)2 Flipping one set bit gives 001 and 010. 010 is greater i.e. 2.To solve this problem, we will have to flip the rightmost set bit and make it zero which will create the number the greatest possible number less than n that is found by flipping one bit ...

Read More

Previous number same as 1's complement in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 431 Views

In this problem, we are given an integer n. Our task is to check weather the preceding number is equal to 1’s complement of the number.Let’s take a few examples to understand our problemInput: 12 Output: No Explanation: (12)10 = (1100)2 Preceding number 11 = (1011)2 1’s complement of 12 = (0011)2 Input: 4 Output: Yes Explanation: 4 = (100)2 Preceding number 3 = (011)2 1’s complement of 12 = (011)2To solve this problem, we can use a simple approach which is by comparing the previous number and the 1’s complement of the number.This approach is simple but consumes space ...

Read More

Previous greater element in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 1K+ Views

In this problem, we are given an array. Our task is to return the greatest element that precedes the current element in the array otherwise print -1.Let’s take an example to understand the problemInput: {6, 2, 7, 1, 5, 3} Output: -1, 6, -1, 7, 7, 7To solve this problem, an easy and obvious solution will be using nested loops that will check greater element in the preceding part of the array.Program to show the implementation of our solutionExample#include using namespace std; void preceddingGreatestElement(int arr[], int n){    cout = 0; j--) {          if (arr[i]

Read More

Binomial Random Variables in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 505 Views

Random variables are those variables that are an outcome of the outcomes of a process that has the probability of giving rise to multiple outcomes. For example, The variable denoting head or tail as an outcome on tossing a coin is a random variable.A binomial random variable is a special type of random variable whose value is related to an event that has a fixed probability of an outcome in an event.There are certain properties that are possessed by a binomial random variable that makes it special. These are a must for a variable to become a binomial random variable ...

Read More

Print all subsequences of a string using Iterative Method in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 479 Views

In this problem, we are given a string and we have to find the substring from the given string. The substring to be found should start with a vowel and end with constant character.A string is an array of characters.The substring that is to be generated in this problem can be generated by deleting some characters of the string. And without changing the order of the string.Input: ‘abc’ Output: ab, ac, abcTo solve this problem, we will iterate the string and fix vowels and check for the next sequence. Let’s see an algorithm to find a solution −AlgorithmStep 1: Iterate ...

Read More

Print all subarrays with 0 sum in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 356 Views

In this problem, we are given an array of integer values and we have to print all those subarrays from this array that have the sum equal to 0.Let’s take an example to understand the topic better, Input: array = [-5, 0, 2, 3, -3, 4, -1] Output: Subarray with sum 0 is from 1 to 4. Subarray with sum 0 is from 5 to 7 Subarray with sum 0 is from 0 to 7To solve this problem, we will check all subarrays possible. And check if the sum of these subarrays is equal to 0 and print them. This ...

Read More

Print direction of moves such that you stay within the [-k, +k] boundary in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 254 Views

In this problem, we have to find a valid way to move positive direction or negative direction in such a way that we stay within a certain limit that is provided by the user.Here, We are given a certain maximum limit K, which is the maximum value to which we can move and an array of n positive values to move. We have to return the sequence i.e. positive or negative directions to move so that it never crosses K value.Let’s take an example to understand the topic better, Input : K = 56 and the array is [25 , ...

Read More

Print digit's position to be removed to make a number divisible by 6 in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 323 Views

In this problem, we are given number and we have to remove more digit from the number. So that the new number formed after removal is divisible by 6.Let’s take an example to learn the concept better −Input : 1324 Output : 4Explanation − on removing the 4th number we will get 132 which is divisible by 6.Here, we are given a number and we have to return the position from where the number is removed to make it divisible by 6.For solving this problem, we will try to create a logic that solves the problem. For this, we will ...

Read More
Showing 551–560 of 975 articles
« Prev 1 54 55 56 57 58 98 Next »
Advertisements