-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTwoPointerExample.cs
More file actions
51 lines (40 loc) · 1.19 KB
/
TwoPointerExample.cs
File metadata and controls
51 lines (40 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
namespace HackerrankSolutionList
{
class Program
{
static void Main(string[] args)
{
//find two numbers in a unsorted array that sum to a target X.
//temukan/tentukan 2 bilangan di dalam array yang tidak berurutan yang ketika dijumlahkan hasilnya adalah X.
int x = int.Parse(Console.ReadLine());
int[] arr = { 1,-1, 2, 3, 5 };
//tentukan left pointer dan right pointer
int i = 0;
int j = arr.Length - 1;
int kiri = arr[i];
int kanan = arr[j];
//sort dlu arraynya(kalau belum sorted)
Array.Sort(arr);
while(kiri < arr.Length)
{
if (kiri+kanan < x)
{
kiri = arr[i++];
}else if(kiri+kanan > x)
{
kanan = arr[j--];
}
else
{
break;
}
}
Console.WriteLine(j+ " "+ i);
Console.ReadLine();
}
}
}