Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _16SumInArray
- {
- class SumInArray
- {
- static void Main(string[] args)
- {
- Console.Write("Array size? = ");
- int size = int.Parse(Console.ReadLine());
- double[] array = new double[size];
- Console.Write("Searched sum? = ");
- double sum = double.Parse(Console.ReadLine());
- for (int i = 0; i < array.Length; i++)
- {
- Console.Write("arr[{0}]=", i);
- array[i] = double.Parse(Console.ReadLine());
- }
- int bestLen = 1;
- int index = 0;
- Array.Sort(array);
- for (int i = 0; i < array.Length - 1; i++)
- {
- int len = 1;
- double tempSum = array[i];
- int j = i + 1;
- while (j < array.Length && tempSum <= sum)
- {
- tempSum += array[j];
- len++;
- if (tempSum == sum)
- {
- index = i;
- bestLen = len;
- }
- j++;
- }
- }
- int count = 0;
- while (count < bestLen)
- {
- Console.Write("{0} ", array[index]);
- index++;
- count++;
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment