Category Archives: C Array

C Code of arrays.

Find the occurence of each number from array in O(n) ?

Find the the occurrence of each number from given array of size n , where elements ranges from 0 to n-1 ?
Complexity : O(n)
Space complexity : O(1)
e.g.
Input:
5 3 3 2 1 3 8 7 3
Output:
3 occurs 4 times

Solution :

Code provided by : Arpit Agrawal

Print given square matrix in spiral order

Print givensquare matrix in spiral order :

Complexity : O(n^2)

Approach :

Step1 : write a 4 for loop for printing boundry

step 2: Apply the step1 for inner martrix by modifying starting point and ending point of matrix

step 3: Aplly step2 untill the matrix size becomes 1 or 0

end

Input :

—————-

1     2     3    4

12  13  14  5

11   16 15   6

10   9     8    7

—————-

Output :

——————————————————-

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

——————————————————-

C code to print given square matrix in spiral order :

 

find pair of numbers in array that add to given sum

Find pair of numbers in array that add to given sum :

Complexity: O(n logn)

Approach :

step 1: Sort an array using efficient sorting method

step 2: use two variable i & j , initialize i as starting position of array (i=0) and j as last position of array (j=n-1). Where n is number of element in array.

step 3:  if(A[i]+A[j]<x) i++

else if(A[i]+A[j]>x) j–

else (A[i]+A[j]==x) print A[i] , A[j] and  i++

step 4: Repeat step 3 untill i<j

 

Code to Find pair of numbers in array that add to given sum :

 

Counting sort

Counting Sort :

n is  number of values in array , k is the range of values (max – min )

Compexity : O(n+k)

Counting sort is a sorting technique to sort an array in linear time. It uses an extra space for counting occurrence  of each object in array. This technique is efficient when range of data is not much larger than number of inputs to be sorted.

 

 

Kth smallest element in Array

C code to find out Kth smallest element in array .

Approach 1:

Sort the array using any sorting method and return the value of kth index.

Approach 2:

Using partition function of quick sort which return the index of the pivot value.

comparing the returning value from partition function with k.

 

 

Hash implementation in Array (Direct Addressing)

C code to implement hash using array.

Description :

Hash can store only integer value. One slot can store only one value. Hash function is mod ( value % slots) .

 

Stack implementation using array in C

Stack implementation using array in C

Stack has following operations :

push // To add value at top of stack

pop // To remove value from top of stack

isstack_empty // To check whether stack is empty or not

isstack_full // To check whether stack is full or not

 

Stack implementation using array in C

count perfect numbers c program

Counting perfect numbers c program in array :

A perfect number is a positive integer that is equal to the sum of its integer positive divisors(excluding itself).

To know list of perfect nos click here 

 

 

Bubble sort on array

Algorithm: Bubble Sort

Code: