The sorting problem
Input: Array of numbers , unsorted. Eg.
Output : Same numbers sorted in some order, say increasing order. Eg.
What is Bubble Sort?
The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items (in other words, all items are in the correct order).
[Read More]
Array of 0 and 1, put 0's at even position and 1's at odd position
you are given an array of integers containing only 0s and 1s. you have to place all the 0s in even position and 1s in odd position and if suppose no if 0s exceed no. of 1s or vice verse then keep them untouched. Do that in ONE PASS and WITHOUT taking EXTRA MEMORY.
input array:
{0 1 1 0 1 0 1 0 1 1 1 0 0 1 0 1 1 }
[Read More]
Anagram program implentation
#include using namespace std; #include "anaword.h" static const int ALPH\_SIZE = 26; Anaword::Anaword(const string & word) : myWord(word), myCounts(ALPH\_SIZE,0) // postcondition: constructed { normalize(); } Anaword::Anaword() : myWord(""), myCounts(ALPH\_SIZE,0) { } void Anaword::normalize() // postcondition: myCounts represents the letter signture of myWord { } string Anaword::toString() const // postcondition: return "bagel" or "gable", regular form of string { return myWord; } ostream & operator << (ostream & out, const Anaword & a) // postcondition: a printed t stream out, out returned { out << a.
[Read More]
Finding whether 2 arrays are intersecting
Here are 2 arrays:
**A1 1 8 7 5 6
A2 0 9 6 4 2
**
**These two are intersecting at 6.
Complexity? **
Solution:
This can be solved using Hash Tables.
Take 1 Hash Table.
Insert elements from A1 in Hast Table as key and put value in front of them
(In case elemente get repeated increment the value count).
Now traverse the second array A2 and check for element as a key.
[Read More]
Given an array of n integers from 1 to n with one integer repeated twice
Problem - Given an array of n integers from 1 to n with one integer repeated twice
This can be done via normal array traversal or hash table to find duplicates. But since we are given that the integers are from 1 to n, then there are better methods available.
Approach 1 - Arithmetic sum approach
So, the efficient method to solve it calculate the sum of actual input array, and expected sum of the array.
[Read More]
Given a list of n numbers from 1 to n-1, with one of the numbers repeated. Devise a method to determine which number is repeated
The sum of the numbers 1 to n-1 is (n)(n-1)/2. Add the numbers on the list, and subtract (n)(n-1)/2. The result is the number that has been repeated.
Give a fast way to multiply a number by 7
Multiply by 8 (left shift by 3 bits) and then subtract the number.
(x << 3) - x
Routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all.
Problem Write a routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all.
Solution Let
(x ^ 2 + y ^2 = r ^ 2)……………………………..1
The basic idea is to draw one quadrant and replicate it to other four quadrants.
Assuming the center is given as (a,b) and radius as r units, then start X from (a+r) down to (a) and start Y from (b) up to (b+r).
[Read More]
c program without main
How can we sum the digits of a given number in single statement?
int sum=0;
for(;num>0;sum+=num%10,num/=10); // This is the “single line”.