find a no. in an array of nxn containing all distinct elements in O(n)
Date : March 29 2020, 07:55 AM
should help you out Give an nxn array of elements such that each row is in ascending order and each column is in ascending order, devise an O(n) algorithm to determine if a given element x in the array. You may assume all elements in the nxn array are distinct. , Start at 1st row last column i.e. i=0, j = n1 while true {
if (j< 0  i > (n1) )
break; // element not found
if (array[i][j] == x)
return i,j
else if (x < array[i][j])
j = j 1; // and repeat
else
i = i+1; // and repeat
}

Find all the numbers that can be represented as sum of three distinct elements of an array
Tag : java , By : Christopher
Date : March 29 2020, 07:55 AM
it fixes the issue Not sure if my solution is actually faster than the brute force approach (expecting giggles and downvotes). But, my solution uses dynamic programming... I think :) The basic idea is that the solution for the problem (N,1) is trivial. The sum of each 1 tuple of values in the input array is just the input array. import Data.Array
import Data.List
import qualified Data.Map.Strict as Map
type Lut = Map.Map [Int] Int
foo :: Array Int Int > Int > Int > Lut
 This is our trivial base case. Create a map for the (N,1) case.
 "input ! i" means what other languages would write as input[i].
foo input n 1 = Map.fromList [([i],input ! i)  i < [1..n]]
 This is the general case, which uses recursion.
foo input n m =
(Map.fromList . fixKeys . concat . fmap sums) (Map.keys lut)
where
lut :: Lut
 Here, the recursion happens. (lut stands for LookUp Table)
lut = foo input n (m1)
all = [1..n]
sums k =
 Here we use our (N,M1) data to create the output
 for this recursion step.
 (forall unused element indices do input[i] + lut[k])
fmap (\(i,k) > (i:k,input ! i + query k) ) unused
where
 The \\ means: difference.
 All indices minus the indices used in a key of a (N,M1)
 element.
unused = fmap (\i > (i,k)) (all \\ k)
query k =
case Map.lookup k lut of
Just v > v
Nothing > error "key cannot not be in the map!"
 Remove duplicates (e.g. [1,2,3] and [1,3,2]) by sorting them.
fixKeys l =
fmap (\(k,v) > (sort k,v)) l
to1BasedArray l = listArray (1,length l) l
raw = [2, 4, 6, 8, 9]
input = to1BasedArray raw
output = foo input (length raw) 3
#include "stdafx.h"
#include <vector>
#include <map>
#include <algorithm> // sort()
#include <iostream>
typedef std::vector<int32_t> Data;
typedef std::vector<size_t> Key;
typedef std::map<Key, int32_t> Lut;
auto contains(const Key& indices, size_t index) > bool
{
for (auto x : indices)
{
if (x == index)
return true;
}
return false;
}
auto unused(size_t n, const Key& k) > Key
{
Key result;
result.reserve(n);
for (size_t i = 1; i <= n; i++)
{
if (!contains(k, i))
{
result.push_back(i);
}
}
return result;
}
// 'input' is a vector we use as 1based (not 0 based) array.
auto foo(const Data& input, size_t n, size_t m) > Lut
{
Lut result;
switch (m)
{
case 1:
for (size_t i = 1; i <= n; i++)
{
Key k = { i };
result[k] = input[i];
}
break;
default:
{
Lut lut = foo(input, n, m  1);
for (const auto& kv : lut)
{
auto uns = unused(n, kv.first);
for (auto i : uns)
{
auto nk = Key(kv.first.begin(), kv.first.end());
nk.push_back(i);
std::sort(nk.begin(), nk.end());
result[nk] = kv.second + input[i];
}
}
}
break;
}
return result;
}
std::ostream& operator<<(std::ostream& os, const Key& values)
{
bool first = true;
os << "[";
for (const auto v : values)
{
if (first)
{
os << v;
first = false;
}
else
{
os << ", " << v;
}
}
os << "]";
return os;
}
int main()
{
// leading 0 because it is a 1 based array.
Data data = { 0, 2, 4, 6, 8, 9 };
Lut result = foo(data, 5, 3);
for (auto kv : result)
{
std::cout << kv.first << " = " << kv.second << std::endl;
}
return 0;
}

How to find distinct indices of elements of one unsorted array in another one?
Date : March 29 2020, 07:55 AM
hope this fix your issue You could simply insert the indices into a multimap keyed by the value of the element, then iterate the other array, find the first index which is what you need, then erase it from the map. Should be O(n log n): std::multimap<char, int> charmap;
for (unsigned i = 0; i < sizeof a; i++) {
charmap.emplace(a[i], i);
}
for (char c : b) {
auto it = charmap.find(c);
std::cout << it>second + 1 << " ";
charmap.erase(it);
}

fastest way to find if all the elements of an array are distinct?
Tag : arrays , By : Mario Tristan
Date : March 29 2020, 07:55 AM

find distinct elements in an array using c++
Tag : cpp , By : Sigfrieg
Date : March 29 2020, 07:55 AM

