Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1974 of 3363
749 Views
Suppose we have a positive integer n and we can do these operations as follow −If n is even, replace n with n/2.If n is odd, you can replace n with either n + 1 or n - 1.We have to find the minimum number of replacements needed for n to become 1?So if the number is 7, then the answer will be 4, as 7 → 8 → 4 → 2 → 1 or 7 → 6 → 3 → 2 → 1To solve this, we will follow these steps −ret := 0, n := xwhile n > 1if ... Read More
363 Views
Suppose we have Given an array of integers A and let n is the length of array A. Now assume Bk to be an array obtained by rotating the array A, k positions clock-wise. Here the rotation can be defined as −F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1].Now find the maximum value of F(0), F(1), ..., F(n-1).So if the input is like A = [4, 3, 2, 6], then −F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 ... Read More
3K+ Views
Suppose we have a list integers representing the data. We have to check whether it is valid UTF-8 encoding or not. One UTF-8 character can be 1 to 4-byte long. There are some properties −For 1-byte character, the first bit is a 0, followed by its unicode code.For n-bytes character, the first n-bits are all 1s, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.So the encoding technique is as follows −Character Number RangeUTF-8 octet sequence0000 0000 0000 007F0xxxxxxx0000 0080 0000 07FF110xxxxx 10xxxxxx0000 0800 0000 FFFF1110xxxx 10xxxxxx 10xxxxxx0001 0000 0010 FFFF11110xxx 10xxxxxx 10xxxxxx ... Read More
913 Views
Suppose we have a list of sorted integers from 1 to n. That is starting from left and ending at right, we have to remove the first number and every other number afterward until we reach the end of the list. We will repeat the previous step again, but this time from right to left, remove the right most number and every other number from the remaining numbers. We will repeat the steps again, alternating left to right and right to left, until one single number remains. We have to find the last number that remains starting with a list ... Read More
430 Views
Suppose we have a singly linked list, we have to find a random node's value from the linked list. Here each node must have the same probability of being chosen. So for example, if the list is [1, 2, 3], then it can return random node in range 1, 2, and 3.To solve this, we will follow these steps −In the getRandom() method, do the following −ret := -1, len := 1, v := xwhile v is not nullif rand() is divisible by len, then ret := val of vincrease len by 1v := next of vreturn retExample(C++)Let us see ... Read More
324 Views
Suppose we have a sequence of numbers that is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference may be either positive or negative. A sequence with less than two elements is trivially a wiggle sequence. So for example, [1, 7, 4, 9, 2, 5] is a wiggle sequence because if you see, the differences (6, -3, 5, -7, 3) are alternately positive and negative. But, [1, 4, 7, 2, 5] and [1, 7, 4, 5, 5] are not wiggle sequences, the first one because its first two differences are ... Read More
568 Views
Suppose we have to calculate a^b mod 1337 where a is one positive integer and b is an extremely large positive integer given in the form of an array. So if a = 2 and b = [1, 0] then the output will be 1024To solve this, we will follow these steps −Define powerMod() method this takes base and powerm := 1337, ret := 1while power is not 0if power is odd, then ret := ret * base mod mbase := base^2 mod mpower := power / 2return retDefine superPower(), this takes a and bif size of b = 0, ... Read More
145 Views
Suppose we have a set of distinct positive integers, we have to find the largest subset such that every pair like (Si, Sj) of elements in this subset satisfies: Si mod Sj = 0 or Sj mod Si = 0.So if the input is like [1, 2, 3], the possible result may come like [1, 2] or [1, 3]To solve this, we will follow these steps −Create an array ret, set endpoint := 0, retLen := 1, n := size of numsif n is 0, then return empty setsort nums arraycreate two arrays len and par of size n, initialize ... Read More
2K+ Views
Suppose we have two jugs with capacities x and y liters. There is an infinite amount of water supply available to us. Now we need to determine whether it is possible to measure exactly z liters using these two jugs. If z liters of water are measurable, we must have z liters of water contained within one or both buckets by the end.We can do these few operations −Fill any of the jugs fully with water.Empty any of the jugs.Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.So ... Read More
1K+ Views
Suppose we have a non-negative integer n. We have to count all numbers with unique digits x, where x is in range 0 to 10^n. So if the number n is 2, then the result will be 91, as we want to find numbers from 0 to 100 without 11, 22, 33, 44, 55, 66, 77, 88, 99.To solve this, we will follow these steps −if n is 0, then return 1n := min of 10 and nif n is 1, then return 10ans := 9 and ret := 10for i in range 2 to nans := ans * (9 ... Read More