# Base 7 Leetcode Solution  Difficulty Level Easy
algorithms coding Interview interviewprep LeetCode LeetCodeSolutions Math

The problem Base 7 Leetcode Solution, asks us to convert a number into a base 7 number. The given number can be negative or positive until 10 million, in both directions on the number line. The problem seems simple and is a simple conversion of a decimal number into a different base. Just like conversion of a number in binary format. Instead of the base being 2, the base is 7. The returning value should be a string. Let’s take a look at a few examples.

## Example  `100`
`"202"`

Explanation: The number 202 in base 7 after conversion yields 100 in decimal format. 202 in base 7 = 2*(7^2) + 0*(7^1) + 2*(7^0) = 100 in base 7. `-7`
`"-10"`

Explanation: The given number -7 after converting into base 7 will give -10.

## Approach for Base 7 Leetcode Solution  The problem Base 7 Leetcode Solution, provides us with a number or integer and asks us to convert it into base 7. The conversion into not only base 7, is a simple operation. We keep on dividing the given number with our required base. After dividing the number, the remainder is stored. Then the result found after dividing the number is again sent for the repetitive division. We keep on storing the remainder which is the answer to the question. The repetitive process is stopped once the number becomes less than the given base.

Next Greater Element I Leetcode Solution

So, let’s consider 100 as the input. First, we divide the number by 7, the quotient is 14, and the remainder is 2. Then the remainder is stored and the quotient is again sent for the division. Now, the quotient becomes 2, and the remainder is stored. Until now, the converted number becomes 20. Then the quotient is sent again for division but is returned because it is less than the given base(7). Thus, the final result comes out to be 202.

## Code  ### C++ code for Base 7 Leetcode Solution

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

string convertToBase7(int num) {
if(num < 0)return "-" + convertToBase7(-num);
else
return convertToBase7(num/7) + convertToBase7(num%7);
}

int main(){
cout<<convertToBase7(100);
}
```
`202`

### Java code for Base 7 Leetcode Solution

```import java.util.*;
import java.lang.*;
import java.io.*;

class Solution {
public static String convertToBase7(int num) {
if(num < 0)return "-" + convertToBase7(-num);
else if(num < 7) return Integer.toString(num);
else
return convertToBase7(num/7) + convertToBase7(num%7);
}

public static void main(String[] args){
System.out.print(convertToBase7(100));
}
}```
`202`

## Complexity Analysis  ### Time Complexity

O(M(n)log n), where n is the length of the given input, M(n) is the time it takes to divide two 2-bit numbers. So, the time complexity is logarithmic.

### Space Complexity

O(log n), the space used by the compiler stack. Here n represents the length of the number. So, the space complexity is also logarithmic.