# Basic codes

[**nth number in Fibonacci Series**](https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/)

Solved on: Tuesday 07, 2020

{% tabs %}
{% tab title="C++" %}

```cpp
#include<bits/stdc++.h> 
using namespace std; 
  
int fib(int n) 
{ 
    if (n <= 1) 
        return n; 
    return fib(n-1) + fib(n-2); 
} 
  
int main () 
{ 
    int n;
    cin >> n;
    cout << fib(n-1); 
    return 0; 
} 
  
```

{% endtab %}
{% endtabs %}

**Print** [**Fibonacci Sequence**](https://www.javatpoint.com/fibonacci-series-in-c)

Solved on: Tuesday 07, 2020

{% tabs %}
{% tab title="C++" %}

```cpp
#include<bits/stdc++.h>
 
using namespace std; 
  
int main () 
{ 
    int n, n1=0, n2=1, n3;
    cin >> n;
    cout << 0 << " " << 1 << " " ;
    for (int i=2;i<n;i++) {
        n3=n1+n2;
        cout << n3 <<" ";
        n1=n2;
        n2=n3;
    }
    return 0; 
} 
```

{% endtab %}
{% endtabs %}

[**Subarray with given sum**](https://practice.geeksforgeeks.org/problems/subarray-with-given-sum/0/?track=sp-bit-magic\&batchId=152)

Solved on: Monday 13 April 2020

{% tabs %}
{% tab title="C++" %}

```cpp
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include "bits/stdc++.h" 
#include <string>

using namespace std;


int main() {
    int n, k, t, i, c, j;
    cin >> t;
    while (t--) {
        cin >> n >> c;
        int arr[n];
        for (i=0;i<n;i++) {
            cin >> arr[i];  
        }
        int sum, b=0;  
    
        for (i = 0; i < n; i++) {  
            sum = arr[i];  
    
            for (j = i + 1; j <= n; j++) {  
                if (sum == c) {  
                    cout << i+1 << " " << j << "\n";  
                    b=1;
                    break;  
                }  
                if (sum > c || j == n)  
                    break;  
                sum = sum + arr[j];  
            }
            if (b==1){
                break;
            }  
        }
        if (b !=1)
            cout << "-1" << "\n";  
    }
    return 0;
}

```

{% endtab %}
{% endtabs %}

[**Sort an array of 0s, 1s and 2s**](https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s/0/)

Solved on: Monday 13 April 2020

{% tabs %}
{% tab title="C++" %}

```cpp
#include<iostream>
#include "bits/stdc++.h" 

using namespace std;
int main()
 {
	int i, j, n, t;
	cin >> t;
	while (t--) {
	    cin >> n;
	    int arr[n];
	    
	    for (i=0;i<n;i++) {
	        cin >> arr[i];
	    }
	    
	    sort (arr, arr+n);
	    
	    for (j=0;j<n;j++) {
	        cout << arr[j] << " ";
	    }
	    cout << "\n";
	}
	
	return 0;
}

```

{% endtab %}
{% endtabs %}

[**Kaprekars Constant**](https://www.hackerrank.com/contests/let-us-master-algorithms/challenges/kaprekars-constant-2)\
Solved on: Aug 7th 2020

```cpp
int kaprekars(int num, int prev, int c) {
    if (num==0){
        return 0;
    }
    int digits[4];
    prev = num; 
    for (int i=0;i<4;i++){
        digits[i] = num%10;
        num=num/10;
    }
    
    sort(digits, digits+4);
    int asc=0, dec=0;
    for (int i=0;i<4;i++) {
        asc=asc*10 +digits[i];
    }
    for (int j=4-1;j>=0;j--) {
        dec=dec*10+digits[j];
    }
    
    int dif=abs(asc-dec);
 
    if (dif == prev) {
        return c; 
    }
    c++;
    return kaprekars(dif, prev, c);
}
int main() {
    int n;
    cin >> n;
    int pre =0;
    int c=0;
    int k = kaprekars(n, pre, c);
    cout << k;
    return 0;
}

```

[**Insertion Sort**](https://practice.geeksforgeeks.org/problems/insertion-sort/1)\
Solved on: Aug 15th 2020

```cpp
void insertionSort(int arr[], int n)
{
   int i;
   for (i = 1; i < n; i++)
      insert(arr, i);
}

void insert(int arr[], int i)
{
    int j=i-1;
    int key=arr[i];
    while (j>=0 && arr[j] > key) {
        arr[j+1] = arr[j];
        j--;
    }
    arr[j+1] = key;
}

```

[**Selection Sort:**](https://practice.geeksforgeeks.org/problems/selection-sort/1)\
Solved on Sep 6th 2020

```cpp
void swapi(int *a, int *b) {
    int temp = *a;
    *a=*b;
    *b=temp;
}

int findMin(int arr[], int start, int end) {
    int min=start;
    for (int i=start+1;i<end;i++) {
        if (arr[i] < arr[min]) {
            min = i;
        }
    }
    return min;
}

void selectionSort(int arr[], int n) {
    for (int i=0;i<n;i++) {
        int minInd = findMin(arr, i, n);
        if (minInd != i) {
            swapi(&arr[i], &arr[minInd]);
        }
    }    
}
```

[**Bubble Sort**](https://practice.geeksforgeeks.org/problems/bubble-sort/1)\
Solved on: Sep 6th 2020

```cpp
void swapi(int *a, int *b) {
    int temp=*a;
    *a=*b;
    *b=temp;
}

void bubble(int arr[], int i, int n) {
    for (int i=0;i<n;i++) {
        for (int j=i+1;j<n;j++) {
            if (arr[i] > arr[j]) {
                swapi(&arr[i], &arr[j]);
            }
        }
    }
}
```

**Insertion Sort**\
Solved on: Sep 7th 2020

```cpp
void insertionSort(int arr[], int n)  {  
    int i, key, j;  
    for (i = 1; i < n; i++) {  
        key = arr[i];  
        j = i - 1;  
        while (j >= 0 && arr[j] > key) {  
            arr[j + 1] = arr[j];  
            j = j - 1;  
        }  
        arr[j + 1] = key;  
    }  
}  
```

[**Minimum operation to make all elements equal in array**](https://www.geeksforgeeks.org/minimum-operation-make-elements-equal-array/)\
Solved on: Sep 7th 2020

```cpp
#include <iostream>
using namespace std;
#include <bits/stdc++.h>

int main() {
    int arr[7] = {1, 1, 1, 1, 1, 1, 1};
    int n=7;

    map<int, int> m;
    for (int i=0;i<n;i++) {
        m[arr[i]]++;
    }
    int max=0;
    for (auto i: m) {
        if (i.second > max) {
            max=i.second;   
        }
    }
    cout << n-max;
	return 0;
}
```
