Saturday, January 29, 2022

Inverse Of A Number

Inverse is basically inverting the places of any number with its value.

[if 5 digit number, then 1 to 5 all digit will be present]

For e.g…,
Before Inverse

  • Places --> 5 4 3 2 1 
  • Value —> 3 2 1 4 5

After Inverse

  • Places --> 5 4 3 2 1
  • Value —> 1 2 5 4 3



5 digit number = 21543-> all digit are present
7 digit number = 7456312 -> all digit are present
Solution = digit * 10 ^ (pos-1)
where, 
digit = position in original of number, and digit in inverse number
pos = digit in original number, and position in inverse of number

public class inverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt(); //O(1) (constant time)
int pos = 1;//O(1) (constant time)
int inverse = 0;//O(1) (constant time)
while (num > 0) { //O(log10 n) The number of digits in a decimal number n is approximately log10 n
//Therefore, the loop iterates a maximum of log10 n times.

int digit = num % 10;//O(1) (constant time)
inverse += (int) Math.pow(10, digit - 1) * pos++;//O(1) (constant time)
num /= 10;//O(1) (constant time)

}

System.out.println(inverse);

}
}

log10 n represents the base-10 logarithm of the number n. In simpler terms, 
it tells you approximately how many digits are required to represent the number n 
in the decimal system (base 10).

Time Complexity (TC):

1. Best Case (O(1)):
Example: Consider a single-digit number like 5. The loop iterates 
only once to extract the digit (5) and calculate its inverse (1/5).
Explanation: Since the number has only one digit, the loop executes 
only once.

2. Worst Case (O(log10 n)):
Example: Imagine a large multi-digit number like 12345. The loop 
iterates 5 times (number of digits) to extract and calculate inverses 
for all digits.
Explanation: The loop needs to iterate through all digits of the input 
number to calculate their inverses and construct the inverse number. 
The number of iterations is roughly proportional to the logarithm of 
the input number in base 10 (log10 n).

3. Average Case (O(log10 n)):
Explanation: Assuming a random distribution of input numbers with varying 
digits, the average number of iterations will fall between the best and 
worst cases, resulting in an average time complexity of O(log10 n).

Time Complexity Analysis:
  • Initialization:

    • Scanner sc = new Scanner(System.in); - O(1) (constant time)
    • int num = sc.nextInt(); - O(1) (constant time)
    • int reverseNumber = 0; - O(1) (constant time)
    • int index = 0; - O(1) (constant time)
  • while loop:

    • Loop condition check (num > 0) - O(1)
    • int lastDigit = num % 10; - O(1)
    • int digitPosition = (int) Math.pow(10, index++); - O(1) (assuming constant lookup or pre-computation for Math.pow)
    • reverseNumber += lastDigit * digitPosition; - O(1)
    • num /= 10; - O(1)
  • Final output: System.out.println(reverseNumber); - O(1)

Overall Time Complexity:

The dominant factor in the time complexity is the while loop. The loop iterates once for each digit in the input number num. The number of digits in a decimal number n is approximately log10 n. Therefore, the loop iterates a maximum of log10 n times.



Space Complexity (SC):

All Cases (O(1)):
Explanation: The code uses a constant amount of additional space for 
variables (num, inverse, pos, digit) regardless of the input size. These 
variables hold integer values, and their space usage doesn't increase with the input size.


No comments:

Post a Comment

Diagonal Traversal

 eg.  1       2       3       4 5      6       7       8 9    10    11     12 13  14   15    16 Output: 1 6 11 16 2 7 12 3 8 4  Approach:...