Array Problems
Array Problems
i = 3,
array now becomes,
arr[8] = { 4, 5, 6, 7, 8, 1, 2, 3 };
second problem,
third proble,
VARIATION:
for an input value N generate a matrix, havin the elements from 1 to N*N,
without using ne external storage for storing the values from 1 to N*N,
123
456
789
another variation
now for input value 3 ,
make spiral
like
789
6 12
5 4 3
implement a queue using two stacks such that insertion and deletion
are in O(1) time
implement a stack in which push, pop and find_min all 3 operations take O(1 ) time .
struct {
int value;
int min_index;
} arr;
struct data_structure {
int top;
struct arr[20];
} a;
Solve this
can u print any string wthout using " " in printf function?
without using " " anywhere in the program
e.g.:
#include <stdio.h>
int main()
{
char a[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
printf(a);
return 0;
}
Simple One!!
You're given an array of integers. Rearrange the
array so that all even numbers come in the begining
and odd numbers come in the end. There are no
constraints on algorithm but come up with optimized
solution. (less space and time complexity)
top = 0;
pos = 0;
while (pos <= size - 1) {
if (a[pos] % 2 == 0) {
temp = a[pos];
a[pos] = a[top];
a[top] = temp;
top++;
}
pos++;
}
tree problems
given inorder and preorder traversal of a tree make that tree,
given preorder and postorder traversal of a tree, how many trees are there for this
combination
make all the trees given preorder and postorder traversal of a tree
u r given two linked lists , such that they merge at the end,
wat is the best possible way to detect it
temp = number;
VARIATION:
now use only bitwise operators
solution
#include <stdio.h>
return inp;
}
int main()
{
int inp = 0;
printf("\nIN = %d, OUT = %x", inp, func(inp));
inp = 10;
printf("\nIN = %d, OUT = %x", inp, func(inp));
inp = 1;
printf("\nIN = %d, OUT = %x", inp, func(inp));
inp = 0x8000000;
printf("\nIN = %x, OUT = %x", inp, func(inp));
return 0;
initial configuration
final configuration
another one
3 jugs,
initial configuration
15 ( 15 ) 10 ( 0 ) 6(0)
final configuration
15(0 ) 10 ( 8 ) 6(5)
15 10 6
----------------------------
15 0 0
5 10 0
5 4 6
11 4 0
11 0 4
1 10 4
1 8 6
7 8 0
7 2 6 - remove 2
7 6 0
1 6 6
1 10 2
11 0 2
11 2 0
5 2 6
5 8 0
0 8 5
a|=a<<1;
a|=a<<2;
a|=a<<4;
a|=a<<8;
a|=a<<16;
C puzzle
a one line C statement to reverse the bits of a byte.
suppose byte is 11000001 then the reverse is 10000011
n=n^0xf
Solution
Let the number missing be 'a' & repeated be 'b'
Get sum of all numbers of the array = n(n+1)/2 - a +b
subtract sum of first 'n' natural numbers = b- a
now calculate the sum of squares = sum of squares of 'n' natural number + b^2 - a^2
similarly subtract sum of suares of first n natural numbers....
for range 10 ,
{ 4, 3, 1, 6, 4, 1, 3, 3, 3, 8 }
output shud be 4
puzzle
Find the number such that if we put last digit to the first place it will becomes twice.
I mean if number be a........mn than new number be na.......m and it will be twice of
previous. where a,b,....m,n are all digits
not all a,b,..m,n are zero. I mean at least one is non-zero.
Repeat this process until u get "9" (i.e the same number u started with) dont write this
number(9) n This is ur desried number:-)
You can get more numbers similarly
Ps:- u should not start with '1' or '0' coz of obvious reasons :-)
Compare two numbers A and B and return " A is greated" or "B is greater" or " Both are equal"
You donot have to use
1. Donot use if, if-else, switch-case
2. Conditional operator ,ternary operator
3. Any loop statement
4. no relational operator
#include <stdio.h>
a = atoi(argv[1]);
b = atoi(argv[2]);
(a-b) || printf("Equal");
((a-b) & c) && printf("a<b");
((b-a) & c) && printf("a>b");
printf("\n");
}
Problem:
Given an array having n integers. Write a prog to find the first number for which there exists
another number whose sum is a given number. For example
Problem
Array of size N is given, N is even. In this array one entry is repeated
n/2 times and the remaining n/2 entries are unique. Write algo to find
the repeated value.
Suggested Sol:
If and n are same then here is solution in O(n/2)
1)make pairs like,a[0] and a[1], a[2] and a[3] and so on as length is even.let that repeated
value be x
2) compare two elements in each pair, if any pair has two element as same ie x then , that x is
found,
hence value is identified in O(n/2),
Problem
design an algo for checking whether two given words are anagrams i.e. whether one word can
be obtained by permuting the letters of the other ( the words tea and eat are anagrams ).
Keep in mind that the words may contain multiple occurances of the same letter.
if anybody has ne idea abt the specific algorithm for this purpose,
than plz reply or what keywords i shud use to get it on internet.
Solution
1)
You can find anagram.c file In the internet.
http://orion.math.iastate.edu/burkardt/c_src/anagram/anagram.c
2)
Use something like a bucket sort..
take two arrays of 26 int's say a[] and b[], loop through the char array , for each letter
increment the appropriate index (for 'a' increment a[0] if occurring in 1st word or increment
b[0] if occurring in second word).
Now loop through both the arrays checking if a[i]==b[i] , exit at the first failure and declare
that words are not anagrams.
Problem
Given a binary tree and two node pointers of that tree, find out the closest common ancestor
of two nodes.
Solutioin
At any stage if found that root is one of the node, give ancestor as root itself.(or may be parent
of root)
Problem
Solution:
Hashing the seen charaters may be one way of solving this problem.[
Eachtime we see a character, will increment its count. the character
with has count 1 and apper first in the string will be the result]
Is there anyother way of solving this problem in O(n) time
complexity???
Problem:
Solution: