FE_ Array Practice
FE_ Array Practice
List
Tree
Stack and Queue
Algorithm
Sorting
Array
- Same Data Type
- Cosecutive in memory
- Access using index
- Index value is integer
- Fixed Size
- Access time is faster than list.
Integer Num_array 1 2 3 4 5 6
Num_array[1] = 1
Num_array[2] = 2
Num_array[3] = 3
Num_array[4] = 4
Num_array[5] = 5
Num_array[6] = 6
Using Looping
For I from 1 to 6 by 1
Print (“Enter a value”)
Integer value=input()
Num_array[i] = value
End For
Integer Num_array 1 2 3 4 5 6
6 1 2 3 4 5
Integer temp=6
Num_array[2]=num_array[1]
6 1 2 3 4 5 Temp= 6
Integer temp=Num_array [len(Num_array)]
I=5
Num_array[6]=Num_array[5]
I=4
Num_array[5]=Num_array[4]
I=3
Num_array[4]=Num_array[3]
I=2
Num_array[3]=Num_array[2]
I=1
Num_array[2]=Num_array[1]
Index starts 1.
Temp= 6
I= 5
Num_array[6]=Num_array[5]
I= 4
Num_array[5]=Num_array[4]
I= 3
Num_array[4]=Num_array[3]
I= 2
Num_array[3]=Num_array[2]
I= 1
Num_array[2]=Num_array[1]
I= 0
Num_array[1]=Num_array[0]
Temp= 6
I=5
I>= 1 => Numarray[6]=Numarray[5]
I=4
I>= 1 => Numarray[5]=Numarray[4]
I=3
I>= 1 => Numarray[4]=Numarray[3]
I=2
I>= 1 => Numarray[3]=Numarray[2]
I=1
I>= 1 => Numarray[2]=Numarray[1]
Example
Input: prices =[ 7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price =1) and sell on day 5 (price = 6), profit = 6-1 = 5
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Price [ 7,1,5,3,6,4]
Integer max=0
Buy=0
Sell=0
For ( i from 1 to 5 by 1) {
For (j from i+1 to 6 by 1){
Problem Statement: Given an array of integers, rotating array of elements by k elements either left or
right. Array index starts at 1.
Example 1:
Input: N = 7, array[] = {1,2,3,4,5,6,7} , k=2 , right
Output: 6 7 1 2 3 4 5
Explanation: array is rotated to right by 2 position .
Integer k=2
Integer temp[]= [len(array)] //length= 7
Integer i=len(array); 1 2 1 2 3 4 5
Example 2:
Input: N = 6, array[] = {3,7,8,9,10,11} , k=3 , left
Output: 9 10 11 3 7 8
Explanation: Array is rotated to left by 3 position.
Revised Array
4. Write a pseudo code to print the count of 0 (zero ) elements from an array. Array index starts from 1.
Input: input [] ={ 2,0,8,0,3,1,0,0}
Output: 4
Integer count=0
For (integer I from 1 to len( input ) by 1) {
If ( input[I] = 0 )
count= count+1
}
Print (count)
Assignment
5. Modify the above code, to print the count of each elements from an array. Array index starts from 1.
Input: input [] ={ 2,0,8,0,3,1,2,0,1}
Output: element 2 = 2
element 0 = 3
element 8 = 1
element 3 = 1
6. Given an array of integers arr, return true if the number of occurrences of each value in the array is
unique or false otherwise.
Example 1:
7. Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the
non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example 1: