// Cpp program for the Print distinct absolute // differences of all possible pairs…
- User
// Cpp program for the Print distinct absolute
// differences of all possible pairs using // Efficient approach for Small Range Elements #include #include #include using namespace std; #define Max 100005// Function to find all distinct // absolute difference of all // possible pairs of the array void printUniqDif(vector &arr) { int n=arr.size();
// bset[i]: Check if i is present // in the array or not bitset<Max> bset; // diff[i]: Check if there exists a // pair whose absolute difference is i bitset<Max> diff; for (int i = 0; i < n; i++) { bset.set(arr[i]); } for (int i = 0; i <= Max; i++) { // If i-th bit is set if (bset[i]) { // Insert the absolute difference // of all possible pairs whose // first element is arr[i] diff = diff | (bset >> i); } } int X = bset.count(); // If there is at least one // duplicate element in arr if (X != n) { cout << 0 << " "; } for (int i = 1; i <= Max; i++) { // If i-th bit is set if (diff[i]) { cout << i << " "; } }}
int main() { vectorarr = { 1, 4, 6 }; printUniqDif(arr); return 0; }
Give me code in Python, Java, C#, JavaScript
Remember like Cpp is for C++ and at start replace Cpp with language name
without changing variable name and also do not change comments
have one line space after code when comment is written.
don’t use class if needed then name it GfG
don’t use public if not needed.
and In C program don’t use #define and malloc and calloc instead use simple array
- Assistant
# Python def printUniqDif(arr): n = len(arr) # bset[i]: Check if i is present # in the array or not Max = 100005 bset = 0 # diff[i]: Check if there exists a # pair whose absolute difference is i diff = 0 for i in range(n): bset |= (1 << arr[i]) for i in range(Max + 1): # If i-th bit is set if (bset >> i) & 1: # Insert the absolute difference # of all possible pairs whose # first element is arr[i] diff |= (bset >> i) X = bin(bset).count('1') # If there is at least one # duplicate element in arr if X != n: print(0, end=" ") for i in range(1, Max + 1): # If i-th bit is set if (diff >> i) & 1: print(i, end=" ") arr = [1, 4, 6] printUniqDif(arr)// Java import java.util.BitSet; import java.util.Vector; public class GfG { static final int Max = 100005; // Function to find all distinct // absolute difference of all // possible pairs of the array static void printUniqDif(Vector<Integer> arr) { int n = arr.size(); // bset[i]: Check if i is present // in the array or not BitSet bset = new BitSet(Max); // diff[i]: Check if there exists a // pair whose absolute difference is i BitSet diff = new BitSet(Max); for (int i = 0; i < n; i++) { bset.set(arr.get(i)); } for (int i = 0; i <= Max; i++) { // If i-th bit is set if (bset.get(i)) { // Insert the absolute difference // of all possible pairs whose // first element is arr[i] BitSet shifted = (BitSet) bset.clone(); shifted.shiftRight(i); diff.or(shifted); } } int X = bset.cardinality(); // If there is at least one // duplicate element in arr if (X != n) { System.out.print("0 "); } for (int i = 1; i <= Max; i++) { // If i-th bit is set if (diff.get(i)) { System.out.print(i + " "); } } } public static void main(String[] args) { Vector<Integer> arr = new Vector<>(); arr.add(1); arr.add(4); arr.add(6); printUniqDif(arr); } } class BitSetExtensions { public static void shiftRight(BitSet bitSet, int positions) { // Implement shift right if needed } }// C# using System; using System.Collections.Generic; public class GfG { const int Max = 100005; // Function to find all distinct // absolute difference of all // possible pairs of the array public static void PrintUniqDif(List<int> arr) { int n = arr.Count; // bset[i]: Check if i is present // in the array or not bool[] bset = new bool[Max]; // diff[i]: Check if there exists a // pair whose absolute difference is i bool[] diff = new bool[Max]; foreach (int num in arr) { bset[num] = true; } for (int i = 0; i <= Max; i++) { // If i-th bit is set if (i < Max && bset[i]) { // Insert the absolute difference // of all possible pairs whose // first element is arr[i] for (int j = i; j < Max; j++) { if (j - i >= 0 && bset[j]) { diff[j - i] = true; } } } } int X = 0; foreach (bool present in bset) { if (present) X++; } // If there is at least one // duplicate element in arr if (X != n) { Console.Write("0 "); } for (int i = 1; i <= Max; i++) { // If i-th bit is set if (i < Max && diff[i]) { Console.Write(i + " "); } } } public static void Main(string[] args) { List<int> arr = new List<int> { 1, 4, 6 }; PrintUniqDif(arr); } }// JavaScript function printUniqDif(arr) { const n = arr.length; // bset[i]: Check if i is present // in the array or not const Max = 100005; let bset = 0n; // diff[i]: Check if there exists a // pair whose absolute difference is i let diff = 0n; for (let i = 0; i < n; i++) { bset |= (1n << BigInt(arr[i])); } for (let i = 0; i <= Max; i++) { // If i-th bit is set if ((bset >> BigInt(i)) & 1n) { // Insert the absolute difference // of all possible pairs whose // first element is arr[i] diff |= (bset >> BigInt(i)); } } let X = bset.toString(2).replace(/0/g, "").length; // If there is at least one // duplicate element in arr let output = ""; if (X !== n) { output += "0 "; } for (let i = 1; i <= Max; i++) { // If i-th bit is set if (((diff >> BigInt(i)) & 1n) === 1n) { output += i + " "; } } console.log(output.trim()); } const arr = [1, 4, 6]; printUniqDif(arr);