Common Elements in three sorted Arrays:
using System;
public class Test
{
public static void Main()
{
int[] ar1 = { 1, 5, 10, 20, 40, 80 };
int[] ar2 = { 6, 7, 20, 80, 100 };
int[] ar3 = { 3, 4, 15, 20, 30, 70, 80, 120 };
Arrays.CommonInthreeSortedArrays(ar1, ar2, ar3, ar1.Length, ar2.Length, ar3.Length);
}
public static void CommonInthreeSortedArrays(int[] a,int[] b,int[] c,int n1,int n2,int n3)
{
IEnumerable<int> temp = a.Intersect(b).Intersect(c);
foreach (int i in temp)
{
Console.Write(i + " ");
}
}
}
Find the first repeating element in an array of integers:
public static int FirstRepeatingElementinArray(int[] ar)
{
int min=-1;
HashSet<int> hash = new HashSet<int>();
for (int i = 0; i < ar.Length; i++)
{
if (hash.Contains(ar[i]))
min = i;
else
hash.Add(ar[i]);
}
return ar[min];
}
{
int powercount = 1<<size;
for(int counter=0;counter<powercount;counter++)
{
for(int i=0;i<size;i++)
{
if(Convert.ToBoolean((counter &(1<<i))))
Console.Write(ar[i]);
}
Console.WriteLine("\n");
}
}
{
int[] sum = new int[arr.Length];
sum[0]=0;
for (int i = 1; i < arr.Length; i++)
sum[i] = sum[i - 1] + arr[i];
for (int j = 0; j < arr.Length; j++)
{
for (int k = j + 1; k < arr.Length; k++)
{
if (sum[j] == sum[k])
printzerosums(j, k, arr);
}
}
}
public static void printzerosums(int i, int j, int[] arr)
{
Console.WriteLine("subset set is ");
for(int g=i+1;g<=j;g++)
Console.Write(arr[g]+" ");
Console.WriteLine("\n");
}
public static int mergesort(int[] a,int[] temp,int left,int right )
{
int inversecount=0;
if(right>left)
{
int mid = left+(right-left)/2;
inversecount=mergesort(a,temp,left,mid);
inversecount+=mergesort(a,temp,mid+1,right);
inversecount+=merge(a,temp,left,mid+1,right);
}
return inversecount;
}
public static int merge(int[] a, int[] temp,int left,int mid,int right)
{
int i=left,j=mid,k=left,inversecount=0;
while((i<=mid-1)&&(j<=right))
{
if(a[i]<=a[j])
temp[k++]=a[i++];
else
{
temp[k++]=a[j++];
inversecount+=(mid-i);
}
}
while(i<=mid-1)
{
temp[k++]=a[i++];
}
while(j<=right)
{
temp[k++]=a[j++];
}
for(int i1=left;i1<=right;i1++)
{
a[i1]=temp[i1];
}
return inversecount;
}
}
Sum of two array numbers to a given target :
public static void sumtwonumberstoanumber(int[] ar,int target)
{
Dictionary<int, int> dic = new Dictionary<int, int>();
for (int i = 0; i < ar.Length; i++)
{
int k = target - ar[i];
if (dic.ContainsValue(k))
{
Console.WriteLine(dic.FirstOrDefault(x => x.Value == k).Key + "," + k);
}
else
{
dic.Add(k, ar[i]);
}
}
}
Find all the pair of triple sum : a[i]+a[j]+a[k]=target
public void find3numbers(int[] a, int n, int target)
{
Mergesort(a);
int i = 0, k = n - 1, j= (i + k) / 2;
while (i<k)
{
int sum= a[i] + a[j] + a[k];
if (sum == target)
{
Console.WriteLine(a[i] + "," + a[j] + "," + a[k]);
i++; k--;
}
else if (sum < target)
{
if (j + 1 != k)
j++;
else
i++;
}
else
{
if (j + 1 != k)
k--;
else
j--;
}
}
}
using System;
public class Test
{
public static void Main()
{
int[] ar1 = { 1, 5, 10, 20, 40, 80 };
int[] ar2 = { 6, 7, 20, 80, 100 };
int[] ar3 = { 3, 4, 15, 20, 30, 70, 80, 120 };
Arrays.CommonInthreeSortedArrays(ar1, ar2, ar3, ar1.Length, ar2.Length, ar3.Length);
}
public static void CommonInthreeSortedArrays(int[] a,int[] b,int[] c,int n1,int n2,int n3)
{
IEnumerable<int> temp = a.Intersect(b).Intersect(c);
foreach (int i in temp)
{
Console.Write(i + " ");
}
}
}
Find the first repeating element in an array of integers:
public static int FirstRepeatingElementinArray(int[] ar)
{
int min=-1;
HashSet<int> hash = new HashSet<int>();
for (int i = 0; i < ar.Length; i++)
{
if (hash.Contains(ar[i]))
min = i;
else
hash.Add(ar[i]);
}
return ar[min];
}
Power set of a set :
public static void Powerset(int[] ar,int size){
int powercount = 1<<size;
for(int counter=0;counter<powercount;counter++)
{
for(int i=0;i<size;i++)
{
if(Convert.ToBoolean((counter &(1<<i))))
Console.Write(ar[i]);
}
Console.WriteLine("\n");
}
}
Print all the subsets sum to zero in a given set of postive and negative numbers.
public static void SumToZero(int[] arr){
int[] sum = new int[arr.Length];
sum[0]=0;
for (int i = 1; i < arr.Length; i++)
sum[i] = sum[i - 1] + arr[i];
for (int j = 0; j < arr.Length; j++)
{
for (int k = j + 1; k < arr.Length; k++)
{
if (sum[j] == sum[k])
printzerosums(j, k, arr);
}
}
}
public static void printzerosums(int i, int j, int[] arr)
{
Console.WriteLine("subset set is ");
for(int g=i+1;g<=j;g++)
Console.Write(arr[g]+" ");
Console.WriteLine("\n");
}
Count Inversions in an array :
{
int inversecount=0;
if(right>left)
{
int mid = left+(right-left)/2;
inversecount=mergesort(a,temp,left,mid);
inversecount+=mergesort(a,temp,mid+1,right);
inversecount+=merge(a,temp,left,mid+1,right);
}
return inversecount;
}
public static int merge(int[] a, int[] temp,int left,int mid,int right)
{
int i=left,j=mid,k=left,inversecount=0;
while((i<=mid-1)&&(j<=right))
{
if(a[i]<=a[j])
temp[k++]=a[i++];
else
{
temp[k++]=a[j++];
inversecount+=(mid-i);
}
}
while(i<=mid-1)
{
temp[k++]=a[i++];
}
while(j<=right)
{
temp[k++]=a[j++];
}
for(int i1=left;i1<=right;i1++)
{
a[i1]=temp[i1];
}
return inversecount;
}
}
Sum of two array numbers to a given target :
public static void sumtwonumberstoanumber(int[] ar,int target)
{
Dictionary<int, int> dic = new Dictionary<int, int>();
for (int i = 0; i < ar.Length; i++)
{
int k = target - ar[i];
if (dic.ContainsValue(k))
{
Console.WriteLine(dic.FirstOrDefault(x => x.Value == k).Key + "," + k);
}
else
{
dic.Add(k, ar[i]);
}
}
}
Find all the pair of triple sum : a[i]+a[j]+a[k]=target
public void find3numbers(int[] a, int n, int target)
{
Mergesort(a);
int i = 0, k = n - 1, j= (i + k) / 2;
while (i<k)
{
int sum= a[i] + a[j] + a[k];
if (sum == target)
{
Console.WriteLine(a[i] + "," + a[j] + "," + a[k]);
i++; k--;
}
else if (sum < target)
{
if (j + 1 != k)
j++;
else
i++;
}
else
{
if (j + 1 != k)
k--;
else
j--;
}
}
}
No comments:
Post a Comment