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
Server Side Programming Articles - Page 1801 of 2646
251 Views
Suppose we have a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. We have to find the maximum result of ai XOR aj, where 0 ≤ i, j < n. So if the input is like [3, 10, 5, 15, 2, 8], then the output will be 28. The max result will be 5 XOR 25 = 28.To solve this, we will follow these steps −Define insertNode(), this will take val and headcurr := headfor i in range 31 to 0bit := val / (2^i) AND 1if child[bit] of curr is null, ... Read More
864 Views
Suppose we have an array of integers with possible duplicates, we have to pick the index randomly of a given target number. We can assume that the given target number must exist in the array. So if the array is like [1, 2, 3, 3, 3], then pick(3), may return 2, 3, 4 randomly.To solve this, we will follow these steps −ret := - 1, cnt := 1for i in range 0 to size of vif v[i] = target, thenif random number mod cnt = 0, then ret = icnt := cnt + 1return retExample (C++)Let us see the following ... Read More
751 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
368 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
914 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
431 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
325 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
571 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
147 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