Skip to content

Instantly share code, notes, and snippets.

@uncheckederror
Last active December 2, 2019 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uncheckederror/ef78d46a2a968058b444c6cb96e706aa to your computer and use it in GitHub Desktop.
Save uncheckederror/ef78d46a2a968058b444c6cb96e706aa to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
// Write a program that prints the numbers from 1 to 100.
// But for multiples of three print “City” instead of the number and for the multiples of five print “Park”.
// For numbers which are multiples of both three and five print “CityPark”.
for (var i = 1; i < 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("CityPark");
}
else if (i % 3 == 0)
{
Console.WriteLine("City");
}
else if (i % 5 == 0)
{
Console.WriteLine("Park");
}
else
{
Console.WriteLine(i);
}
}
// Given an array of integers with no duplicates, return indices of the two numbers such that they add up to a specific target.
// You may assume that each input would have exactly one solution, and you may not use the same element twice.
static string GetIndiciesThatAddToTarget(int[] items, int target)
{
var dictionary = new Dictionary<int,int>();
int iCount = 0;
foreach(var i in items)
{
dictionary.Add(i, iCount);
iCount++;
}
int jCount = 0;
foreach (var j in items)
{
int missing = target - j;
bool found = dictionary.TryGetValue(missing, out int indice);
if(found)
{
return $"[{jCount}, {indice}]";
}
jCount++;
}
return "Target Unreachable";
}
var array = new int[] { 2, 7, 11, 15 };
var target = 9;
Console.WriteLine(GetIndiciesThatAddToTarget(array, target));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment