Minimum inverting factor in an array
Last Updated :
01 Mar, 2023
Given an array of n positive integers, the task is to find the minimum inverting factor in the given array.
Inverting factor is defined as the absolute difference between the reverse of any two numbers arri and arrj where i != j.
Note: Trailing zeroes should be ignored while reversing the digits i.e. 1200 becomes 21 when reversed.
Examples:
Input: arr[] = { 56, 20, 47, 93, 45 }
Output: 9
The minimum inverting factor is 9, of the pair (56, 47).
Input: arr[] = { 26, 15, 45, 150 }
Output: 0
The minimum inverting factor is 0, of the pair (15, 150).
A naive approach is to iterate over two loops to find all possible pairs. Reverse both numbers individually and find their absolute difference. Update the inverting factor (minimum absolute difference) at every step. Time Complexity would be O(N2).
An efficient approach would be to precompute the reverse of each array element and store it in its reversed form only (considering the case of trailing zeroes also). Now, to find the minimum inverting factor, sort the array in non-decreasing order. Since the array is sorted, minimum absolute difference always occurs among any two adjacent numbers.
Below is the Steps to solve this problem:
- Initialize ans to the maximum possible value of an integer (INT_MAX).
- Iterate over the loop 0 to N-1:
- Initialize an empty string s
- Extract each digit of the number arr[i] and store it in reverse order in s.
- Find the position pos upto which trailing zeroes occur in s.
- Form the reversed number num by iterating from pos to the end of s, multiplying the existing num by 10 and adding the digit at the current index.
- Update arr[i] with the reversed number num.
- Sort the array arr in ascending order using sort() function.
- Iterate over loop 1 to N-1.
- Compute the absolute difference between adjacent elements in arr.
- Update ans with the minimum of the current value of ans and the computed difference.
- Return ans.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findMinimumInvertingFactor( int arr[], int N)
{
int ans = INT_MAX;
for ( int i = 0; i < N; i++) {
string s;
int num = arr[i];
while (num > 0) {
s.push_back(num % 10 + '0' );
num /= 10;
}
int pos;
for (pos = 0; pos < s.size(); pos++)
if (s[pos] != 0)
break ;
num = 0;
for ( int j = pos; j < s.size(); j++)
num = num * 10 + (s[j] - '0' );
arr[i] = num;
}
sort(arr, arr + N);
for ( int i = 1; i < N; i++)
ans = min(ans, abs (arr[i] - arr[i - 1]));
return ans;
}
int main()
{
int arr[] = { 56, 20, 47, 93, 45 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findMinimumInvertingFactor(arr, N) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int findMinimumInvertingFactor( int arr[], int N)
{
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; i++)
{
String s = "" ;
int num = arr[i];
while (num > 0 )
{
s+=( char )((num % 10 ) + '0' );
num /= 10 ;
}
int pos;
for (pos = 0 ; pos < s.length(); pos++)
if (s.charAt(pos) != 0 )
break ;
num = 0 ;
for ( int j = pos; j < s.length(); j++)
num = num * 10 + (s.charAt(j) - '0' );
arr[i] = num;
}
Arrays.sort(arr);
for ( int i = 1 ; i < N; i++)
ans = Math.min(ans, Math.abs(arr[i] - arr[i - 1 ]));
return ans;
}
public static void main(String[] args)
{
int arr[] = { 56 , 20 , 47 , 93 , 45 };
int N = arr.length;
System.out.println(findMinimumInvertingFactor(arr, N));
}
}
|
Python3
import sys
def findMinimumInvertingFactor(arr, N) :
ans = sys.maxsize
for i in range (N) :
num = arr[i]
s = ""
while (num > 0 ) :
s + = str (num % 10 )
num / / = 10
for pos in range ( len (s)) :
if (s[pos] ! = "0" ) :
break ;
num = 0
for j in range (pos, len (s)) :
num = num * 10 + ( ord (s[j]) - ord ( "0" ))
arr[i] = num
arr.sort()
for i in range (N) :
ans = min (ans, abs (arr[i] - arr[i - 1 ]))
return ans
if __name__ = = "__main__" :
arr = [ 56 , 20 , 47 , 93 , 45 ]
N = len (arr)
print (findMinimumInvertingFactor(arr, N))
|
C#
using System;
class GFG
{
static int findMinimumInvertingFactor( int []arr, int N)
{
int ans = int .MaxValue;
for ( int i = 0; i < N; i++)
{
String s = "" ;
int num = arr[i];
while (num > 0)
{
s+=( char )((num % 10) + '0' );
num /= 10;
}
int pos;
for (pos = 0; pos < s.Length;pos++)
if (s[pos] != 0)
break ;
num = 0;
for ( int j = pos; j < s.Length; j++)
num = num * 10 + (s[j] - '0' );
arr[i] = num;
}
Array.Sort(arr);
for ( int i = 1; i < N; i++)
ans = Math.Min(ans, Math.Abs(arr[i] - arr[i - 1]));
return ans;
}
public static void Main(String[] args)
{
int []arr = { 56, 20, 47, 93, 45 };
int N = arr.Length;
Console.WriteLine(findMinimumInvertingFactor(arr, N));
}
}
|
PHP
<?php
function findMinimumInvertingFactor( $arr , $N )
{
$ans = PHP_INT_MAX;
for ( $i = 0; $i < $N ; $i ++)
{
$s = "" ;
$num = $arr [ $i ];
while ( $num > 0)
{
$s .= chr ( $num % 10 + ord( '0' ));
$num =(int)( $num /10);
}
for ( $pos = 0; $pos < strlen ( $s ); $pos ++)
if ( $s [ $pos ] != 0)
break ;
$num = 0;
for ( $j = $pos ; $j < strlen ( $s ); $j ++)
$num = $num * 10 + (ord( $s [ $j ]) - ord( '0' ));
$arr [ $i ] = $num ;
}
sort( $arr );
for ( $i = 1; $i < $N ; $i ++)
$ans = min( $ans , abs ( $arr [ $i ] - $arr [ $i - 1]));
return $ans ;
}
$arr = array ( 56, 20, 47, 93, 45 );
$N = count ( $arr );
echo findMinimumInvertingFactor( $arr , $N ). "\n" ;
?>
|
Javascript
<script>
function findMinimumInvertingFactor(arr, N)
{
let ans = Number.MAX_VALUE;
for (let i = 0; i < N; i++)
{
let s = "" ;
let num = arr[i];
while (num > 0)
{
s+=String.fromCharCode((num % 10) + '0' .charCodeAt());
num = parseInt(num / 10, 10);
}
let pos;
for (pos = 0; pos < s.length;pos++)
if (s[pos] != 0)
break ;
num = 0;
for (let j = pos; j < s.length; j++)
num = num * 10 + (s[j].charCodeAt() - '0' .charCodeAt());
arr[i] = num;
}
arr.sort( function (a, b){ return a - b});
for (let i = 1; i < N; i++)
ans = Math.min(ans, Math.abs(arr[i] - arr[i - 1]));
return ans;
}
let arr = [ 56, 20, 47, 93, 45 ];
let N = arr.length;
document.write(findMinimumInvertingFactor(arr, N));
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(len), where len is the digits of the maximum number present in the array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...