< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page
  • Q1: Reverse Array
  • Q2: Copying two sorted arrays into 3rd Array
  • Q3: Merge Sorted Array
  • Q4: Move Zeroes
  • Q5: Rotate array to the right by k-Steps
  • Q6: Array left rotation by d positions
  • Q7: Check if Array Is Sorted and Rotated
  • Recursive Approach
  • Q8: TwDo Sum
  • Q9: Even elements appear first
  • Q10: Remove Duplicates from Sorted Array
  • Q11: Remove Duplicates from Sorted Array II

Was this helpful?

  1. Arrays

1-D Array

Practice Questions

PreviousArraysNext2-D Arrays

Last updated 1 year ago

Was this helpful?

size = sizeof(arr)/sizeof(arr[0]);

Q1:

void ReverseArray(vector<int>& arr, int m){
   int s = m + 1; // Condition for reversing whole array s = 0;    
   int e = arr.size() - 1;
   while(s <= e){
      swap(arr[s], arr[e]);
      s++;
      e--;
   }
}

Q2: Copying two sorted arrays into 3rd Array

void merge(vector<int>& arr1, int m, vector<int>& arr2, int n, vector<int>& arr3){
    int i = 0, j = 0, k = 0;

    while(i < m && j < n){   
        
        if(arr1[i] < arr2[j]){ 
            arr3[k++] = arr1[i++]; 
        }
        
        else { 
            arr3[k++] = arr2[j++];  
        }
    }
    
    // copy the remaining elements into the arr3
    while(i<m){ 
        arr3[k++] = arr1[i++]; 
    }

    while(j<n){
        arr3[k++] = arr3[j++];
    }
}
 nums1[m] = {1, 4, 5, 7, 0, 0, 0}  assume all zeros as an empty space. m -= n is the real size of an array, so the remaining elements are going to be merged with nums2 
 nums2[n] = {2, 3, 6}
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
    int j  = 0;
    while(j < n){ 
    
        nums1[m++] = nums2[j++]; 
    
    // nums1[m+n] = {1, 4, 5, 7, 2, 3, 6}
    }
    sort(nums1.begin(),nums1.end()); // sorting the unsorted array
}
void moveZeroes(vector<int>& arr) {
    int nonZero = 0; //This provides an index for all nonzero values
    for(int i = 0; i < arr.size(); i++){
        // ignore if 0 != 0 and increment the i++
        if(arr[i] != 0){
            swap(arr[i], arr[nonZero]);
            nonZero++;
        }
    } 
}

Approach 2:

Rotate array to the right by k-Steps
    void rotate(vector<int>& v, int k) {
        int n = v.size();
        vector<int> temp (n);
    
        for(int i = 0; i < n; i++){
            temp[(i + k) % n] = v[i];
        }
        v = temp;
    }
#include <iostream>
using namespace std;

int* rotateLeftByk(int* arr, int size, int k) {
    int *temp = new int[size];

    // approach # 1
    // for (int i = 0; i < size; i++) {
    //     temp[(i+size-k)%size] = arr[i];
    // }

    // approach # 2
    int j = 0; 
    for (int i = k; i < size; i++)
    {
        temp[j++] = arr[i];
    }

    for (int i = 0; i < k; i++)
    {
        temp[j++] = arr[i];
    }

    return temp;
}

int main() {
    int arr[] = {1, 2, 5, 9, 10};
    
    int* temp = rotateLeftByk(arr, 5, 2);

    for (int i = 0; i < 5; i++) {
        cout << temp[i] << " ";
    }
    
    delete[]temp;
}

Case-I True

Sorted -> nuns[ ] = {1, 2, 3, 4, 5}

Case-II True

Sorted-Rotated -> nums[ ] = {3, 4, 5, 1, 2}

Case-III True

Same Element -> nums[ ] = {1, 1, 1, 1}

Case-IV False

Unsorted -> nums[ ] = {2, 1, 3, 4} Must use this I/P

// Sorted array = [1,2,3,4,5]
// Sorted-Rotated array = [3,4,5,1,2]

bool check(vector<int>& nums) {
    int count = 0;
    int n = nums.size();
    for(int i = 1; i < n; i++){
    // Condition for sorted-rotated array [3,4,5,1,2] -> (5 > 1) True
        if(nums[i-1] > nums[i])  count++;
    } 
    // Condition for Sorted Array [1,2,3,4,5]-> (5 > 1) True
    if(nums[0] < nums[n-1]) count++;

    return (count<=1); // count == 1 for same elements in an array like [1,1,1]
}
For Arrays
#include <iostream>
using namespace std;
int main() {
 
    int arr[] = {2, 3, 5, 7};
    int size = sizeof(arr)/sizeof(arr[0]);

    int target = 7;
    int temp[2];

    for (int i = 0; i < size; i++) {   
        bool found = false;
                    
        for (int j = i+1; j < size; j++) {
        
            if((arr[i] + arr[j]) == target) {
                temp[0] = i;
                temp[1] = j;
                found = true;
            }
        }
        if(found) break;
    }

    for (int i = 0; i < 2; i++) {
        cout << temp[i] <<" ";
    }
}
For Vector
class Solution {
public:
    vector<int> twoSum(vector<int>& arr, int target) {
        for(int i = 0; i < arr.size(); i++) {
            for(int j = i+1; j < arr.size(); j++) {
                if((arr[i] + arr[j]) == target)
                    return {i, j};
            }
        }
        return {}; // no solution found
    }
};

Q9: Even elements appear first

#include <iostream>
using namespace std;

void rearrange(int* arr, int s) {
    int oddIndex = 0;
    for (int i = 0; i < s; i++) {
        if(arr[i]%2 == 0) {
            swap(arr[i], arr[oddIndex++]);
        }
    }
}

int main() {
    int arr[] ={1, 2, 3, 4, 5, 6};
    rearrange(arr, 6);

    for (int i = 0; i < 6; i++) {
        cout << arr[i] << " ";
    }
}
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        stack<int> st;
        st.push(nums[0]);
        
        for(int i = 1; i < nums.size(); i++) {
            if(st.top() != nums[i]) 
                st.push(nums[i]);   
        }
        nums.clear();
        while(!st.empty()) {
            nums.push_back(st.top());
            st.pop();
        }

        int s = 0, e = nums.size()-1;
        
        while(s<=e) swap(nums[s++], nums[e--]);
        
        return nums.size();
    }
};
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        
        stack<int> st;
        
        for(int i = 0; i < nums.size(); i++) {

            if( st.empty() || (!st.empty() && st.top() != nums[i])  ) {
                st.push(nums[i++]);
                bool isRun = false;
                if(i < nums.size() && st.top() == nums[i]) {
                    st.push(nums[i]);
                    isRun = true;
                }
                if(isRun == false) i--;
            }  
        }
        
        nums.clear();

        while(!st.empty()) {
            nums.push_back(st.top());
            st.pop();
        }

        int s = 0, e = nums.size()-1;
        
        while(s<=e) swap(nums[s++], nums[e--]);
        
        return nums.size();
    }
};

Q3:

Q4:

Q5:

Approach 1:

Q6:

Q7:

Q8:

Q10:

Q11:

Reverse Array
Merge Sorted Array
Move Zeroes
Rotate array to the right by k-Steps
Using STL Algorithm
Array left rotation by d positions
Check if Array Is Sorted and Rotated
TwDo Sum
Remove Duplicates from Sorted Array
Remove Duplicates from Sorted Array II
Page cover image
Recursive Approach