Coordinate Compression
Given an array, arr[] containing Integers from 1 to 10^12 compress them to numbers ranging from 1 to 10^6. size of the array is ≤ 10^6.
Examples:
Input: arr[] = {100000000000, 100000000001, 100000000002, 999999999999}
Output: arr[] = {100000, 100000, 100000, 999999, 0, 1, 2, 999999}
Explanation: Every element of the new array is in the range 1 to 10^6Input: arr[] = {1, 2, 3}
Output: arr[] = {1, 2, 3}
Approach: To solve the problem follow the below idea:
We can use a Mod value of 10^6 and store its quotient and remainder in two places i.e., i and N + i. Where N is the size of original array. So, we have to construct the array of size 2*N. After the operations, every element of new array is in the range 1 to 10^6.
Follow the below steps to solve the problem:
- Initialize a value, Mod = 10^6, and array res[] of size 2*N.
- Pass the array res[], arr[], and N to the solve function.
- Run a loop and set, res[i] = arr[i] / Mod and res[N + i] = arr[i] % Mod.
Below is the implementation of the above approach:
C++
// C++ code to implement the above approach #include <bits/stdc++.h> using namespace std; const int Mod = 1e6; // Function to compress the array containing // number from 1 to 10^12 to 1 to 10^6 void solve( long long * arr, int * res, int N) { for ( int i = 0; i < N; i++) { res[i] = arr[i] / Mod; res[N + i] = arr[i] % Mod; } } // Function to print New Array void printAns( int arr[], int N) { for ( int i = 0; i < 2 * N; i++) { cout << arr[i] << " " ; } cout << endl; } // Function to print original array // recovered from new one void OrigArray( int arr[], int N) { for ( int i = 0; i < N; i++) { cout << (( long long )arr[i] * Mod + arr[N + i]) << " " ; } cout << endl; } // Driver Code int main() { long long arr[] = { 100000000000, 100000000001, 100000000002, 999999999999 }; int N = sizeof (arr) / sizeof (arr[0]); int res[2 * N] = {}; solve(arr, res, N); cout << "New Array is : " << endl; printAns(res, N); cout << "\nOriginal Array recovered from new one is : " << endl; OrigArray(res, N); return 0; } |
Java
// Java code to implement the above approach import java.io.*; import java.util.*; class GFG { public static final int Mod = 1000000 ; // Function to compress the array containing // number from 1 to 10^12 to 1 to 10^6 public static void solve( long [] arr, int [] res) { int N = arr.length; for ( int i = 0 ; i < N; i++) { res[i] = ( int )(arr[i] / Mod); res[N + i] = ( int )(arr[i] % Mod); } } // Function to print New Array public static void printAns( int [] arr) { for ( int i : arr) { System.out.print(i + " " ); } System.out.println(); } // Function to print original array // recovered from new one public static void OrigArray( int [] arr) { int N = arr.length / 2 ; for ( int i = 0 ; i < N; i++) { System.out.print( (( long )arr[i] * Mod + arr[N + i]) + " " ); } System.out.println(); } public static void main(String[] args) { long [] arr = { 100000000000L, 100000000001L, 100000000002L, 999999999999L }; int [] res = new int [ 2 * arr.length]; solve(arr, res); System.out.println( "New Array is : " ); printAns(res); System.out.println( "\nOriginal Array recovered from new one is : " ); OrigArray(res); } } // This code is contributed by lokesh. |
Python3
#Python3 code for the above approach def solve(arr, N): """ Function to compress the array containing number from 1 to 10^12 to 1 to 10^6 """ res = [ 0 ] * ( 2 * N) for i in range (N): res[i] = arr[i] / / Mod res[N + i] = arr[i] % Mod return res def print_ans(arr, N): """ Function to print New Array """ for i in range ( 2 * N): print (arr[i], end = " " ) print () def OrigArray(arr, N): """ Function to print original array recovered from new one """ for i in range (N): print ((arr[i] * Mod + arr[N + i]), end = " " ) print () Mod = 1000000 arr = [ 100000000000 , 100000000001 , 100000000002 , 999999999999 ] N = len (arr) res = solve(arr, N) print ( "New Array is:" ) print_ans(res, N) print ( "\nOriginal Array recovered from new one is:" ) OrigArray(res, N) #This code is contributed by Potta Lokesh |
C#
// C# code to implement the above approach using System; using System.Linq; public class GFG { public const int Mod = 1000000; // Function to compress the array containing // number from 1 to 10^12 to 1 to 10^6 public static void Solve( long [] arr, int [] res) { int N = arr.Length; for ( int i = 0; i < N; i++) { res[i] = ( int )(arr[i] / Mod); res[N + i] = ( int )(arr[i] % Mod); } } // Function to print New Array public static void PrintAns( int [] arr) { foreach ( int i in arr) { Console.Write(i + " " ); } Console.WriteLine(); } // Function to print original array // recovered from new one public static void OrigArray( int [] arr) { int N = arr.Length / 2; for ( int i = 0; i < N; i++) { Console.Write((( long )arr[i] * Mod + arr[N + i]) + " " ); } Console.WriteLine(); } static public void Main() { // Code long [] arr = { 100000000000L, 100000000001L, 100000000002L, 999999999999L }; int [] res = new int [2 * arr.Length]; Solve(arr, res); Console.WriteLine( "New Array is : " ); PrintAns(res); Console.WriteLine( "\nOriginal Array recovered from new one is : " ); OrigArray(res); } } // This code is contributed by lokeshmvs21. |
Javascript
// Javascript code to implement the above approach const Mod = 1e6; // Function to compress the array containing // number from 1 to 10^12 to 1 to 10^6 function solve( arr, res, N) { for (let i = 0; i < N; i++) { res[i] = Math.floor(arr[i] / Mod); res[N + i] = arr[i] % Mod; } } // Function to print New Array function printAns( arr, N) { for (let i = 0; i < 2 * N; i++) { console.log(arr[i] + " " ); } } // Function to print original array // recovered from new one function OrigArray(arr, N) { for (let i = 0; i < N; i++) { console.log(arr[i] * Mod + arr[N + i])+ " " ; } } // Driver Code let arr = [ 100000000000, 100000000001, 100000000002, 999999999999 ]; let N = arr.length; let res= new Array(2*N); solve(arr, res, N); console.log( "New Array is : \n" ); printAns(res, N); console.log( "\nOriginal Array recovered from new one is : " ); OrigArray(res, N); // This code is contributed by poojaagarwal2. |
New Array is : 100000 100000 100000 999999 0 1 2 999999 Original Array recovered from new one is : 100000000000 100000000001 100000000002 999999999999
Time Complexity: O(N)
Auxiliary Space: O(N)
Related Articles:
Please Login to comment...