Coder Social home page Coder Social logo

dsa-practice's Introduction

DSA-Practice

Day 1

  • Sort an array of 0’s 1’s 2’s without using extra space or sorting algo
    Similar Questions
  • Repeat and Missing Number Similar Questions
  • Merge two sorted Arrays without extra space
    Optimal Method - Gap Method
    // Merging two sorted arrays with O(1) extra space
    // Function to find next gap.
    
    int nextGap(int gap) {
      if (gap <= 1)
        return 0;
      return (gap / 2) + (gap % 2);
    }
    
    void merge(int* arr1, int* arr2, int n, int m) {
      int i, j, gap = n + m;
      for (gap = nextGap(gap); gap > 0; gap = nextGap(gap)) {
        // comparing elements in the first array.
        for (i = 0; i + gap < n; i++)
          if (arr1[i] > arr1[i + gap])
            swap(arr1[i], arr1[i + gap]);
    
        // comparing elements in both arrays.
        for (j = gap > n ? gap - n : 0; i < n && j < m; i++, j++)
          if (arr1[i] > arr2[j])
            swap(arr1[i], arr2[j]);
    
        if (j < m) {
          // comparing elements in the second array.
          for (j = 0; j + gap < m; j++)
            if (arr2[j] > arr2[j + gap])
              swap(arr2[j], arr2[j + gap]);
        }
      }
    }
    
    // Driver code
    int main() {
      int a1[] = {10, 27, 38, 43, 82};
      int a2[] = {3, 9};
      int n = sizeof(a1) / sizeof(int);
      int m = sizeof(a2) / sizeof(int);
    
      // Function Call
      merge(a1, a2, n, m);
    
      printf("First Array: ");
      for (int i = 0; i < n; i++)
        printf("%d ", a1[i]);
    
      printf("\nSecond Array: ");
      for (int i = 0; i < m; i++)
        printf("%d ", a2[i]);
      printf("\n");
      return 0;
    } ```
  • Kadane’s Algorithm
  • Merge Overlapping Subintervals
  • Find the duplicate in an array of N+1 integers.

Day 2

dsa-practice's People

Contributors

amandugar avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.