Found 34494 Articles for Programming

Nearest power of 2 of frequencies of each digit of a given number

Vanshika Sood
Updated on 27-Aug-2023 11:39:30

59 Views

The article describes a method to calculate the closest power of 2 for the frequency of each digit in a given number. The term "frequencies" refers to the count of occurrences of each unique digit in the number. Problem Statement Determine the occurrence count of each digit in a positive integer N. Then, for each digit, find the nearest power of 2 to its frequency. If there are two nearest powers of 2 for any frequency, print the larger one. Example Input n = 677755 Output 5 -> 2 6 -> 1 7 -> 4 ... Read More

Maximize count of occurrences of S2 in S1 as a subsequence by concatenating N1 and N2 times respectively

Vanshika Sood
Updated on 27-Aug-2023 11:35:53

118 Views

The following article discusses an approach to count the maximum occurrences of a string s2 in another string s1 after they have been concatenated N1 and N2 times respectively. This is an interesting type of pattern searching problem. In this article we have employed a relatively intuitive solution approach. Problem Statement The task is to determine the maximum number of non-overlapping occurrences of string s2 within string s1. The strings are concatenated multiple times: s1 is repeated n1 times, and s2 is repeated n2 times. Example Input s1 = “abc”, s2 = “ac”, n1 = 4, n2 ... Read More

Lexicographically smallest string with period K possible by replacing ‘?’s from a given string

Vanshika Sood
Updated on 27-Aug-2023 11:24:27

235 Views

A string is a period of K if and only if it repeats itself every K characters. For example, the string "abcabcabc" is a period of 3, because it repeats itself every 3 characters. The string "abcabc?abc" is not a period of 3, because the character '?' does not repeat itself every 3 characters. Problem Statement Given a string "str" containing N lowercase characters and a positive integer K, the objective is to replace every occurrence of the character '?' within the string "str" with a lowercase alphabet such that the resulting string forms a period of length ... Read More

Lexicographically largest string possible by repeatedly appending first character of two given strings

Vanshika Sood
Updated on 27-Aug-2023 10:07:19

158 Views

Lexicographic ordering refers to a method of comparing sequences of elements, akin to the ordering of words in a dictionary. The comparison process involves evaluating the first element of each sequence. If the first element of the first sequence is considered smaller than the first element of the second sequence, the first sequence is considered lexicographically less than the second sequence. Conversely, if the first element of the first sequence is deemed larger than the first element of the second sequence, the first sequence is lexicographically greater than the second sequence. Problem Statement The objective is to generate ... Read More

Count all prime numbers that can be formed using digits of a given number

Vanshika Sood
Updated on 27-Aug-2023 09:57:29

187 Views

Any number larger than 1 is said to be prime if it cannot be written as the product of two smaller natural numbers except 1 and the number itself. For instance, 5 is prime since its only product forms, 1 5 and 5 1, both involve 5. Primes play a crucial role in number theory as stated by the prime number theorem, which asserts that any natural number greater than 1 is either a prime itself or can be expressed as a unique product of prime numbers. This theorem highlights the significance of prime numbers in the realm of mathematics. ... Read More

Calculate time required to type a word using given single-row keyboard

Vanshika Sood
Updated on 27-Aug-2023 09:54:43

89 Views

The following article discusses an efficient approach to compute the total time taken to type out a word using a single-row keyboard. This is an interesting problem and has been asked previously in technical interviews. Problem Statement Consider a hypothetical scenario wherein all the keys on a keyboard are placed in a single row. The first key is indexed 0 and the last key is indexed 25. For a given string ‘s’ calculate the time taken to type all the characters of ‘s’ on a special keyboard with layout specified as ‘keyboard_layout’. The time taken ... Read More

Find Mth Eelement after K Right Rotations of an Array

Vanshika Sood
Updated on 27-Aug-2023 09:52:00

52 Views

Right rotating an array means shifting its elements to the right by a certain number of positions. In a single right rotation, the last element of the array becomes the first element, and the remaining elements are shifted to the right. Problem Statement The goal is to find the Mth element of an array after performing K right rotations, where K and M are non-negative integers and the array contains N elements. Sample Examples Input arr = [12 34 56 21], K = 2, M = 1 Output 56 Explanation Arr after K right ... Read More

Sum and product of K smallest and largest Fibonacci numbers in the array

Shubham Vora
Updated on 25-Aug-2023 16:08:46

90 Views

In this problem, we will find the sum and product of the K maximum and minimum Fibonacci numbers in the array. The given problem is very basic and aims to focus on improving the problem-solving skills of beginners. The main goal of the problem is to introduce how to filter the Fibonacci numbers from the given array of elements and sum and product minimum and maximum Fibonacci numbers. Problem Statement We have given a nums[] array containing the N integer values. Also, we have given K positive integer. We need to find the sum and product of the K minimum ... Read More

Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees

Shubham Vora
Updated on 25-Aug-2023 16:04:10

51 Views

In this problem, we will update the level of each child node based on the left and right subtree's weight difference. Here, we will recursively traverse the subtree of each node to get the weight of the left and right subtree. After that, we will again traverse each subtree node to update its level according to the difference in weight of the left and right sub-tree. Problem Statement We have given a complete binary tree containing N levels and 2N -1 nodes. The levels are numbered from 0 to N − 1 in decreasing order (0, -1, -2, -3, etc.). ... Read More

Print Nth Stepping or Autobiographical number

Shubham Vora
Updated on 25-Aug-2023 16:02:24

168 Views

In this problem, we will print the Nth Stepping number. The naïve approach for solving the problem is to traverse natural numbers, check whether each number is a Stepping number, and find the Nth Stepping number. Another approach can be using the queue data structure. Problem Statement We have given a positive integer N. We need to print the Nth Stepping number. The number is called a Stepping number if the difference between two adjacent digits of a number is 1. Sample Examples Input N = 15 Output 34 Explanation The Stepping numbers are 1, 2, ... Read More

Advertisements