Skip to main content

Command Palette

Search for a command to run...

Array (Level-1)

Level-1 Question arrays

Published
7 min readView as Markdown
Array (Level-1)
F

Assalamualaikum warahmatullah wabarakatuh( traditional Islamic greeting in Arabic "Assalamu alaikum": "Peace be upon you." "Wa rahmatullahi": "And the mercy of Allah." "Wa barakatuh": "And His blessings.") I’m Faraz Alam, and I’m documenting my journey through the world of software technology. Despite earning a master’s degree in Computer Applications and having access to opportunities provided by my tier-3 college, I struggled to take full advantage of them due to poor management and a less productive environment. This led to joblessness, primarily due to a lack of upskilling. Now, I am dedicated to enhancing my skills and knowledge with the aim of securing a valuable job offer from leading product-based companies, including those in the FAANG group (Facebook, Amazon, Apple, Netflix, Google) and other prominent tech giants. This documentation is not for self-promotion; rather, it is for anyone who is waiting for an opportunity but feels they lack the tools and skills required to overcome challenges. It’s a testament to the effort and responsibility needed to navigate the journey towards success when you take charge of your own path. Date: 31 July 2024, 07:25 AM This page will be updated regularly to reflect new achievements and milestones as I continue to build my career.

  1. Working of vector and list STL.
//vector (Implemented as a dynamic array, storing elements in contiguous memory locations.)
// C++ program to illustrate the above functions

#include <iostream> 
#include <vector> 

using namespace std; 

int main() 
{ 
    vector<int> v; 

    // Push elements 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i); 

    cout << "Size : " << v.size(); 

    // checks if the vector is empty or not 
    if (v.empty() == false) 
        cout << "\nVector is not empty"; 
    else
        cout << "\nVector is empty"; 

    cout << "\nOutput of begin and end: "; 
    for (auto i = v.begin(); i != v.end(); ++i) 
        cout << *i << " "; 

    // inserts at the beginning 
    v.emplace(v.begin(), 5); 
    cout << "\nThe first element is: " << v[0]; 

    // Inserts 20 at the end 
    v.emplace_back(20); 
    int n = v.size(); 
    cout << "\nThe last element is: " << v[n - 1]; 

    // erases the vector 
    v.clear(); 
    cout << "\nVector size after erase(): " << v.size();     

    return 0; 
}
//list (Implemented as a doubly-linked list, where each element (node) stores the data 
// and pointers to the previous and next nodes. Elements are not stored contiguously.)
#include <iostream> 
#include <list> 
#include <iterator> 
using namespace std; 

//function for printing the elements in a list 
void showlist(list <int> g) 
{ 
    list <int> :: iterator it; 
    for(it = g.begin(); it != g.end(); ++it) 
        cout << '\t' << *it; 
    cout << '\n'; 
} 

int main() 
{ 

    list <int> gqlist1, gqlist2; 


    for (int i = 0; i < 10; ++i) 
    { 
        gqlist1.push_back(i * 2); 
        gqlist2.push_front(i * 3); 
    } 
    cout << "\nList 1 (gqlist1) is : "; 
    showlist(gqlist1); 

    cout << "\nList 2 (gqlist2) is : "; 
    showlist(gqlist2); 

    cout << "\ngqlist1.front() : " << gqlist1.front(); 
    cout << "\ngqlist1.back() : " << gqlist1.back(); 

    cout << "\ngqlist1.pop_front() : "; 
    gqlist1.pop_front(); 
    showlist(gqlist1); 

    cout << "\ngqlist2.pop_back() : "; 
    gqlist2.pop_back(); 
    showlist(gqlist2); 

    cout << "\ngqlist1.reverse() : "; 
    gqlist1.reverse(); 
    showlist(gqlist1); 

    cout << "\ngqlist2.sort(): "; 
    gqlist2.sort(); 
    showlist(gqlist2); 

    return 0; 

}
  1. Iterator in STL
// C++ code to demonstrate the working of
// iterator, begin() and end()
#include<iostream>
#include<iterator> // for iterators
#include<vector> // for vectors
using namespace std;
int main()
{
    vector<int> ar = { 1, 2, 3, 4, 5 };

    // Declaring iterator to a vector
    vector<int>::iterator ptr;

    // Displaying vector elements using begin() and end()
    cout << "The vector elements are : ";
    for (ptr = ar.begin(); ptr < ar.end(); ptr++)
        cout << *ptr << " ";

    return 0;    
}
//
// C++ code to demonstrate the working of
// advance()
#include<iostream>
#include<iterator> // for iterators
#include<vector> // for vectors
using namespace std;
int main()
{
    vector<int> ar = { 1, 2, 3, 4, 5 };

    // Declaring iterator to a vector
    vector<int>::iterator ptr = ar.begin();

    // Using advance() to increment iterator position
    // points to 4
    advance(ptr, 3);

    // Displaying iterator position
    cout << "The position of iterator after advancing is : ";
    cout << *ptr << " ";

    return 0;

}
//
// C++ code to demonstrate the working of
// next() and prev() 
#include<iostream>
#include<iterator> // for iterators
#include<vector> // for vectors
using namespace std;
int main()
{
    vector<int> ar = { 1, 2, 3, 4, 5 };

    // Declaring iterators to a vector
    vector<int>::iterator ptr = ar.begin();
    vector<int>::iterator ftr = ar.end();


    // Using next() to return new iterator
    // points to 4
    auto it = next(ptr, 3);

    // Using prev() to return new iterator
    // points to 3
    auto it1 = prev(ftr, 3);

    // Displaying iterator position
    cout << "The position of new iterator using next() is : ";
    cout << *it << " ";
    cout << endl;

    // Displaying iterator position
    cout << "The position of new iterator using prev()  is : ";
    cout << *it1 << " ";
    cout << endl;

    return 0; 
}
//
// C++ code to demonstrate the working of
// inserter()
#include<iostream>
#include<iterator> // for iterators
#include<vector> // for vectors
using namespace std;
int main()
{
    vector<int> ar = { 1, 2, 3, 4, 5 };
    vector<int> ar1 = {10, 20, 30}; 

    // Declaring iterator to a vector
    vector<int>::iterator ptr = ar.begin();

    // Using advance to set position
    advance(ptr, 3);

    // copying 1 vector elements in other using inserter()
    // inserts ar1 after 3rd position in ar
    copy(ar1.begin(), ar1.end(), inserter(ar,ptr));

    // Displaying new vector elements
    cout << "The new vector after inserting elements is : ";
    for (int &x : ar) 
        cout << x << " ";

    return 0;    
}
  1. Operation on arrays
#include <iostream>
#include <cmath>
using namespace std;

int insert(int arr[], int n, int x, int cap, int pos)
{
    if(n == cap)
        return n;

    int idx = pos - 1;

    for(int i = n - 1; i >= idx; i--)
    {
        arr[i + 1] = arr[i];
    }

    arr[idx] = x;

    return n + 1;
} 



int main() {

       int arr[5], cap = 5, n = 3;

       arr[0] = 5; arr[1] = 10; arr[2] = 20;

       cout<<"Before Insertion"<<endl;

       for(int i=0; i < n; i++)
       {
           cout<<arr[i]<<" ";
       }

       cout<<endl;

       int x = 7, pos = 2;

       n = insert(arr, n, x, cap, pos);

       cout<<"After Insertion"<<endl;

       for(int i=0; i < n; i++)
       {
               cout<<arr[i]<<" ";
       }

}

#include <iostream>
#include <cmath>
using namespace std;

int search(int arr[], int n, int x)
{
    for(int i = 0; i < n; i++)
    {
        if(arr[i] == x)
            return i;
    }

    return -1;
} 



int main() {

    int arr[] = {20, 5, 7, 25}, x = 5;

    cout<<search(arr, 4, x);  

}
  1. Insertion and deletion in array
//insertion
for(i = len-1; i >= idx; i--)
{
    arr[i+1] = arr[i];
}
//deletion
for(i = idx+1; i < len; i++)
{
    arr[i-1] = arr[i];
}

len = len-1;

5) Largest and smallest in array

int largest(int arr[], int n)
{
    int i;

    // Initialize maximum element
    int max = arr[0];

    // Traverse array elements
    // from second and compare
    // every element with current max
    for (i = 1; i < n; i++)
        if (arr[i] > max)
            max = arr[i];

    return max;
}

6) Second smallest and largest in array

// returns the index of second largest
// if second largest didn't exist return -1
int secondLargest(int arr[], int n) {
    int first = 0, second = -1;
    for (int i = 1; i < n; i++) {
        if (arr[i] > arr[first]) {
            second = first;
            first = i;
        }
        else if (arr[i] < arr[first]) {
            if (second == -1 || arr[second] < arr[i])
                second = i;
        }
    }
    return second;
}

7) Check if array is sorted or not

bool arraySortedOrNot(int arr[], int n)
{
    // Array has one or no element
    if (n == 0 || n == 1)
        return true;

    for (int i = 1; i < n; i++)

        // Unsorted pair found
        if (arr[i - 1] > arr[i])
            return false;

    // No unsorted pair found
    return true;
}

8) Reverse an array

/* Function to reverse arr[] from start to end*/
void rvereseArray(int arr[], int start, int end)
{
    while (start < end)
    {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

9) Remove duplicate from sorted array

int remDups(int arr[], int n)
{
    int res = 1;

    for(int i = 1; i < n; i++)
    {
        if(arr[res - 1] != arr[i])
        {
            arr[res] = arr[i];
            res++;
        }
    }

    return res;
}

10) Move all zeroes to end

// function to move all zeroes at the end of array
void moveZerosToEnd(int arr[], int n)
{
    // Count of non-zero elements
    int count = 0;

    // Traverse the array. If arr[i] is non-zero, then
    // update the value of arr at index count to arr[i]
    for (int i = 0; i < n; i++)
        if (arr[i] != 0)
            arr[count++] = arr[i];

    // Update all elements at index >=count with value 0
    for (int i = count; i < n; i++)
        arr[i] = 0;
}

// function to print the array elements
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}

11) Left and right rotate by 1 place

// Left Rotate by 1
void leftRotateByOne(vector<int>& arr) {
    int n = arr.size();
    if (n <= 1) return; // nothing to rotate

    int temp = arr[0];
    for (int i = 1; i < n; i++) {
        arr[i - 1] = arr[i];
    }
    arr[n - 1] = temp;
}

// Right Rotate by 1
void rightRotateByOne(vector<int>& arr) {
    int n = arr.size();
    if (n <= 1) return; // nothing to rotate

    int temp = arr[n - 1];
    for (int i = n - 1; i > 0; i--) {
        arr[i] = arr[i - 1];
    }
    arr[0] = temp;
}

Faraz_Alam_DSA

Part 2 of 5

This is series where i have documented questions which i have practiced for problem solving and algorithmic thinking. All topic have different levels from level-1 to level-5 as we increase in levels difficulty increases.

Up next

Disjoint Set (Level- 1)

Union operation void union(int i, int j) { int irep = find(i); // representative (root) of i int jrep = find(j); // representative (root) of j // Attach i’s root under j’s root Parent[irep] = jrep; } Find Operation int fin...