Friday, May 4, 2018

DS Quespaper

  1. Search, insert and delete in an unsorted array
  2. Search, insert and delete in a sorted array
  3. Given an array A[] and a number x, check for pair in A[] with sum as x
  4. Majority Element
  5. Find the Number Occurring Odd Number of Times
  6. Find the Missing Number
  7. Searching in an array where adjacent differ by at most k
  8. Merge an array of size n into another array of size m+n
  9. Searching in an array where adjacent differ by at most k
  10. Maximum Subarray Sum Excluding Certain Elements
  11. Leaders in an array
  12. Find the only repetitive element between 1 to n-1
  13. Sort elements by frequency | Set 1
  14. Count Inversions in an array | Set 1 (Using Merge Sort)
  15. Two elements whose sum is closest to zero
  16. Shortest Un-ordered Subarray
  17. Shortest Un-ordered Subarray
  18. Check for Majority Element in a sorted array
  19. Union and Intersection of two sorted arrays
  20. Ceiling in a sorted array
  21. Find the two repeating elements in a given array
  22. Sort an array of 0s, 1s and 2s
  23. Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
  24. Equilibrium index of an array
  25. Count number of occurrences (or frequency) in a sorted array
  26. Find the repeating and the missing | Added 3 new methods
  27. Median in a stream of integers (running integers)
  28. Find a Fixed Point in a given array
  29. Find sub-array with given sum
  30. Find a triplet that sum to a given value
  31. Find the two numbers with odd occurrences in an unsorted array
  32. Find a pair with the given difference
  33. Find four elements that sum to a given value | Set 1 (n^3 solution)
  34. Find four elements that sum to a given value | Set 2 ( O(n^2Logn) Solution)
  35. Median of two sorted arrays of different sizes
  36. Count the number of possible triangles
  37. Find a peak element
  38. Find number of pairs (x, y) in an array such that x^y > y^x
  39. Count all distinct pairs with difference equal to k
  40. Find if there is a sub-array with 0 sum
  41. Given a sorted array and a number x, find the pair in array whose sum is closest to x
  42. Count 1’s in a sorted binary array
  43. Print All Distinct Elements of a given integer array
  44. Construct an array from its pair-sum array
  45. Find common elements in three sorted arrays
  46. Find the first repeating element in an array of integers
  47. Find position of an element in a sorted array of infinite numbers
  48. Check if a given array contains duplicate elements within k distance from each other
  49. Find the element that appears once
  50. Find Union and Intersection of two unsorted arrays
  51. Delete an element from array (Using two traversals and one traversal)
  52. Count frequencies of all elements in array in O(1) extra space and O(n) time
  53. Trapping Rain Water
  54. Count triplets with sum smaller than a given value
  55. Count Inversions of size three in a given array
  56. Merge two sorted arrays with O(1) extra space
  57. Find lost element from a duplicated array
  58. Count pairs with given sum

  1. Search, insert and delete in an unsorted array
  2. Search, insert and delete in a sorted array
  3. Given an array A[] and a number x, check for pair in A[] with sum as x
  4. Majority Element
  5. Find the Number Occurring Odd Number of Times
  6. Find the Missing Number
  7. Searching in an array where adjacent differ by at most k
  8. Merge an array of size n into another array of size m+n
  9. Searching in an array where adjacent differ by at most k
  10. Maximum Subarray Sum Excluding Certain Elements
  11. Leaders in an array
  12. Find the only repetitive element between 1 to n-1
  13. Sort elements by frequency | Set 1
  14. Count Inversions in an array | Set 1 (Using Merge Sort)
  15. Two elements whose sum is closest to zero
  16. Shortest Un-ordered Subarray
  17. Shortest Un-ordered Subarray
  18. Check for Majority Element in a sorted array
  19. Union and Intersection of two sorted arrays
  20. Ceiling in a sorted array
  21. Find the two repeating elements in a given array
  22. Sort an array of 0s, 1s and 2s
  23. Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
  24. Equilibrium index of an array
  25. Count number of occurrences (or frequency) in a sorted array
  26. Find the repeating and the missing | Added 3 new methods
  27. Median in a stream of integers (running integers)
  28. Find a Fixed Point in a given array
  29. Find sub-array with given sum
  30. Find a triplet that sum to a given value
  31. Find the two numbers with odd occurrences in an unsorted array
  32. Find a pair with the given difference
  33. Find four elements that sum to a given value | Set 1 (n^3 solution)
  34. Find four elements that sum to a given value | Set 2 ( O(n^2Logn) Solution)
  35. Median of two sorted arrays of different sizes
  36. Count the number of possible triangles
  37. Find a peak element
  38. Find number of pairs (x, y) in an array such that x^y > y^x
  39. Count all distinct pairs with difference equal to k
  40. Find if there is a sub-array with 0 sum
  41. Given a sorted array and a number x, find the pair in array whose sum is closest to x
  42. Count 1’s in a sorted binary array
  43. Print All Distinct Elements of a given integer array
  44. Construct an array from its pair-sum array
  45. Find common elements in three sorted arrays
  46. Find the first repeating element in an array of integers
  47. Find position of an element in a sorted array of infinite numbers
  48. Check if a given array contains duplicate elements within k distance from each other
  49. Find the element that appears once
  50. Find Union and Intersection of two unsorted arrays
  51. Delete an element from array (Using two traversals and one traversal)
  52. Count frequencies of all elements in array in O(1) extra space and O(n) time
  53. Trapping Rain Water
  54. Count triplets with sum smaller than a given value
  55. Count Inversions of size three in a given array
  56. Merge two sorted arrays with O(1) extra space
  57. Find lost element from a duplicated array
  58. Count pairs with given sum

Tuesday, February 13, 2018

python

Python Moudule

Modules are used to categorize code in Python into smaller part. A module is simply a file, where classes, functions and variables are defined. Grouping similar code into a single file makes it easy to access.
Have a look over example:
If the content of a book is not indexed or categorized into individual chapters, then the book might have turned boring and hectic. Hence, dividing book into chapters made it easy to understand.
In the same sense python modules are the files which have similar code. Thus module is simplify a python code where classes, variables and functions are defined.
Advantage:
Python provides the following advantages for using module:
1) Reusability: Module can be used in some other python code. Hence it provides the facility of code reusability.
2) Categorization: Similar type of attributes can be placed in one module.

Importing a Module:

There are different ways by which you we can import a module. These are as follows:

1) Using import statement:

"import" statement can be used to import a module.
Syntax:
  1. import <file_name1, file_name2,...file_name(n)="">  
  2. </file_name1,>  
Have a look over an example:
eg:
  1. def add(a,b):  
  2.     c=a+b  
  3.     print c  
  4.     return  
Save the file by the name addition.py. To import this file "import" statement is used.
  1. import addition  
  2. addition.add(10,20)  
  3. addition.add(30,40)

Sunday, January 7, 2018

python

Date and Time

Python is very useful in case of Date and Time. We can easily retrieve current date and time using Python.

Retrieve Time

To retrieve current time a predefined function localtime() is used. localtime() receives a parameter time.time() . Here,
time is a module,
time() is a function that returns the current system time in number of ticks since 12:00 am , January 1,1970. It is known as epoch.
Tick is simply a floating point number in seconds since epoch.

  1. import time;  
  2. localtime = time.localtime(time.time())  
  3. print "Current Time is :", localtime




  1. import calendar  
  2. calendar.prcal(2017)  

  1. import calendar  
  2. print (calendar.isleap(2000))

  1. import calendar  
  2. print (calendar.monthcalendar(2014,6))


  1. import calendar  
  2. print (calendar.prmonth(2014,6) )


  1. import calendar  
  2. print "Current month is:"  
  3. cal = calendar.month(2014, 6)  
  4. print (cal)  


  1. import time  
  2. t = (2014, 6, 26, 17, 3, 38, 1, 48, 0)  
  3. t = time.mktime(t)  
  4. print (time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t)))

Monday, December 18, 2017

c Program in file input & output

File Handling in c language is used to open, read, write, search or close file. It is used for permanent storage.


Advantage of File
It will contain the data even after program exit. Normally we use variable or array to store data, but data is lost after program exit. Variables and arrays are non-permanent storage medium whereas file is permanent storage medium.


Functions for file handling
There are many functions in C library to open, read, write, search and close file. A list of file functions are given below:
No.
Function
Description
1
fopen()
opens new or existing file
2
fprintf()
write data into file
3
fscanf()
reads data from file
4
fputc()
writes a character into file
5
fgetc()
reads a character from file
6
fclose()
closes the file
7
fseek()
sets the file pointer to given position
8
fputw()
writes an integer to file
9
fgetw()
reads an integer from file
10
ftell()
returns current position
11
rewind()
sets the file pointer to the beginning of the file
ModeDescription
ropens a text file in read mode
wopens a text file in write mode
aopens a text file in append mode
r+opens a text file in read and write mode
w+opens a text file in read and write mode
a+opens a text file in read and write mode
rbopens a binary file in read mode
wbopens a binary file in write mode
abopens a binary file in append mode
rb+opens a binary file in read and write mode
wb+opens a binary file in read and write mode
ab+opens a binary file in read and write mode
  1. #include <stdio.h>  
  2. main(){  
  3.    FILE *fp;  
  4.    fp = fopen("file.txt", "w");//opening file  
  5.    fprintf(fp, "Hello file by fprintf...\n");//writing data into file  
  6.    fclose(fp);//closing file  
  7. }