Updation in array

Updation in array

Updation in array is the update operation refers to updating an existing element from the at a given index.This operation is quite similar to the insert method, except that it will replace the existing value at the given index. This means will simply assign a new value at the given index. This method expects two arguments index and value.

 

Diagramatic Representation

 

Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to update an element available at the Kth position of LA.
1. Start
2. Set LA[K-1] = ITEM
3. Stop

 

Updation in array in different languages

Program in C

void main()
{
   int LA[] = {1,3,5,7,8};
   int k = 3, n = 5, item = 11;
   int i, j;

   printf("The original array elements are:\n");

   for(i = 0; i<n; i++)
   {
      printf("LA[%d] = %d \n", i, LA[i]);
   }

   LA[k-1] = item;

   printf("The array elements after updation:\n");

   for(i = 0; i<n; i++)
   {
      printf("LA[%d] = %d \n", i, LA[i]);
   }
}

Output

The original array elements are:
LA[0]=2
LA[1]=4
LA[2]=6
LA[3]=8
LA[4]=9
The array elements after updation:
LA[0]=2
LA[1]=4
LA[2]=11
LA[3]=8
LA[4]=9

Program in CPP

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
    int arr[3] = {0,1,2};
    cout << "Before update "<<arr[2]<<endl;
    arr[2]=1;//updating element
    cout <<"After update "<<arr[2]<<endl;
}

Output

Before update 2
After update 1

 

Program in Java

class array{
  public static void main(String[] args){
    int arr[] = {1,2,3,4};
    System.out.println("Before update" + arr[2]);
    arr[2] = 9;//updating the value
    System.out.println("After update" + arr[2]);
  }
}

 

Output

Before update3
After update9

 

Program in Python

from collections import deque
 
def Queries(arr, N, Q):
 
    dq = deque()
 
    for i in range(N):
        dq.append(arr[i])
 
    sz = len(Q)
 
    for i in range(sz):
 
        if (Q[i][0] == 0):
 
            front = dq[0]
 

            dq.popleft()
 

            dq.appendleft(front)
 

        elif (Q[i][0] == 1):
 
            back = dq[N - 1]
 
            dq.popleft()
 
            dq.appendleft(back)
 
        elif (Q[i][0] == 2):
            dq[Q[i][1]] = Q[i][2]
 
        else:
            print(dq[Q[i][1]], end = " ")
 
if __name__ == '__main__':
 
    arr = [ 1, 2, 3, 4, 5 ]
    N = len(arr)
 
    Q = [ [ 0 ], [ 1 ], [ 3, 1 ],
          [ 2, 2, 54 ], [ 3, 2 ] ]
 
    Queries(arr, N, Q)

Output

2 54

 

Program in Javascript

<script>

function Queries(arr,N,Q)
{

  let dq = [];
  
  for (let i = 0; i < N; i++)
  {
    dq.push(arr[i]);
  }
  
  let sz = Q.length;
  
  for (let i = 0; i < sz; i++)
  {
    if (Q[i][0] == 0)
    {

      let front = dq[0];
  
      dq.shift();
  
      dq.push(front);
    }
  
    else if (Q[i][0] == 1)
    {

      let back = dq[dq.length - 1];
  
      dq.pop();
  
      dq.unshift( back);
    }
  

    else if (Q[i][0] == 2)
    {
      dq[Q[i][1]] = Q[i][2];
    }
  
    
    else
    {
      document.write(dq[Q[i][1]] + " ");
    }
  }
}
 
let arr=[1, 2, 3, 4, 5];
let N = arr.length;
    
 
let Q = [[0], [1], [3, 1],
[2, 2, 54], [3, 2]];
Queries(arr, N, Q);
 
 
</script>

Output

2 54

 

Time Complexity:

O(n)

 

Array operation

Insertion in array

Deletion in array

Traversing in array

Searching in array

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *