Operating an array | Practice | GeeksforGeeks

Given an array A of size N, your task is to do some operations, i.e., search an element, insert an element, and delete an element by completing the functions. Also, all functions should return a boolean value.
Input Format:
The first line of input consists of T, the number of test cases. T testcases follow. Each testcase contains 3 lines of input. The first line of every test case consists of an integer N, denotes the number of elements in an array. The second line of every test cases consists of N spaced integers Ai. The third line of every test case consists of four integers X, Y, Yi and Z, denoting the elements for the search operation, insert operation, insert operation element index and delete operation respectively.
Output Format:
For each testcase, return 1 for each operation if done successfully else 0.
Your Task:
Since this is a function problem, you only need to complete the provided functions.
Constraints:
1 <= T <= 100
1 <= N <= 100
0 <= Ai <= 1000
Example:
Input:
1
5
2 4 1 0 6
1 2 2 0
Output:
1 1 1
-1
srivastavnirdesh4
This comment was deleted.
0
jaideepnagillscsebtech3 weeks ago
i think the deleteEle function is not implemented properly.
-1
rahulp00rtx1 month ago
Simple JavaScript Solution
class Solution {      searchEle(a,x){       //Your code here
       for(let i =0; i<a.length; i++){           if(a[i] == x){               return 1;           }                     }        return 0;   }
   insertEle(a,y,yi){       //Your code here       a.splice(yi,0,y);       return 1;
       }   
   deleteEle(a,z){       //Your code here    if(a.includes(z)){        let index = a.indexOf(z);        a.splice(index,1);        return 1;    }else{        return 1;    }   }}
0
mummadikarthik20001 month ago
J
+3
dangrio2 months ago
bool searchEle(int a[],int x){   // add code here.   for(int i=0;a[i]!=-1;i++){       if(a[i] == x) return true;   }   return false;}bool insertEle(int a[],int y,int yi){   // add code here.   int size=0;   for(int i=0;a[i]!=-1;i++) size++;   if(yi >= size) return false;   a[yi] = y;   return true;}bool deleteEle(int a[],int z){   // add code here.   int size=0, index;   for(int i=0;a[i]!=-1;i++) size++;   for(int i=0;i<size;i++){       if(a[i]==z){           index = i;           break;       }   }   for(int j=index;j<size;j++){       a[j] = a[j+1];   }}
-2
rohitbamrara203 months ago
 
/* Complete the function(s) below */
bool searchEle(int a[],int x){   // add code here.   int i,n=100;      for(i=0;i<n;i++)   {   if(a[i]==x)     return 1;   }   return 0;}bool insertEle(int a[],int y,int yi){   // add code herefor (int i=0;i<100;i++){              if (i ==yi) {                          a[i] =y;                          return true;              }          }      return false;  }   bool deleteEle(int a[],int z){   // add code here.   int i,n;   for(i= 0;i<n;i++)   {       if(a[i]== z)       a[i]= -1;   }   return true ;}
0
abhinav200619923 months ago
bool searchEle(int a[],int x){//   unsigned int n = (sizeof(a))/(sizeof(int));    int n = 100;      for(int i=0;i<n;i++){       if(a[i]==x){           return 1;       }   }return 0;}
bool insertEle(int a[],int y,int yi){  //      unsigned int n = (sizeof(a))/(sizeof(int));  //   int x = 0;       int n = 100;          for(int i=n;i>=yi;i--){         a[i]=a[i-1];     }     a[yi]=y;     return 1;}bool deleteEle(int a[],int z){  // bool x = searchEle(a,z);  int pos;    //    unsigned int n = (sizeof(a))/(sizeof(int));      int n = 100;      int x = 0;      for(int i=0;i<n;i++){       if(a[i]==z){          pos = i;          a[pos]=z+1;          return 1;//           x = 1;       }   }   /*    if(x==0){       return 0;   }   if(x==1){       for(int i=pos;i<n;i++){          a[i]=a[i+1];       }       return 1;}*/   }
0
prachiagarwal07084 months ago
class Solution {      searchEle(a,x){       for(let i = 0; i< a.length ; i++){           if(a[i] == x){               return 1;           }       }       return 0;   }
   insertEle(a,y,yi){       //Your code here       a.splice(yi, 0, y);       return 1   }
   deleteEle(a,z){       //Your code here       if(a.includes(z)){           let index = a.indexOf(z);           a.splice(index,1);           return 1;       }else{       return 1;       }   }}
-4
shaikhkasim962214 months ago
#include <stdio.h>
Int main()
Int a=2;
Int b=3;
{
Printf("the sum of two number is %d/n");
Return 0;
}
0
mgkr3214 months ago
C++ Solution:
 
bool searchEle(int a[],int x){   unsigned int len = (sizeof(a))/(sizeof(int));   unsigned int i = 0;      for (i=0; i<1000; i++)   {       if (a[i] == x)       {           return 1;       }   }   return 0;}
bool insertEle(int a[],int y,int yi){   unsigned int len = (sizeof(a))/(sizeof(int));   unsigned int i = 0;      for (i=0; i<1000; i++)   {       if (i == yi)       {           a[i] = y;           return 1;       }   }   return 0;}
bool deleteEle(int a[],int z){   int count=0;   for(int i=1;i<=100;i++)   {       if(a[i]==z)       {           for(int j=i;j<100;j++)           {               a[i]=a[i+1];           }       }   }
   for(int i=1;i<=100;i++)   {       if(a[i]==z)       {           count++;       }   }      if(count==0)   {       return true;   }   else   {       return false;   }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.