Last active
August 15, 2020 14:25
-
-
Save sakshamsaxena/804cf36eacd907e2db81bcf112780684 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"io/ioutil" | |
"os" | |
"sort" | |
"strconv" | |
) | |
func main() { | |
r := NewReader() | |
w := bufio.NewWriter(os.Stdout) | |
t := r.ReadInt() | |
for t > 0 { | |
n := r.ReadInt() | |
x := r.ReadInt() | |
arr := make([]int, n) | |
var i int = 0 | |
for i < n { | |
arr[i] = r.ReadInt() | |
i++ | |
} | |
ans := process(arr, x) | |
w.WriteString(strconv.Itoa(ans) + "\n") | |
t-- | |
} | |
w.Flush() | |
} | |
func process(a []int, x int) int { | |
var cum, ans int | |
sort.Ints(a) | |
for _, ele := range a { | |
cum += ele | |
if cum > x { | |
break | |
} | |
ans++ | |
} | |
return ans | |
} | |
type Reader struct { | |
input []byte | |
needle int | |
} | |
func NewReader() *Reader { | |
reader := bufio.NewReader(os.Stdin) | |
bytes, err := ioutil.ReadAll(reader) | |
if err != nil { | |
panic("Error reading from STDIN") | |
return nil | |
} | |
return &Reader{ | |
input: bytes, | |
needle: 0, | |
} | |
} | |
func (r *Reader) ReadInt() int { | |
currentByte := r.input[r.needle] | |
for currentByte < 48 || currentByte > 57 { | |
r.needle = r.needle + 1 | |
if r.needle >= len(r.input) { | |
break | |
} | |
currentByte = r.input[r.needle] | |
} | |
var result int = 0 | |
for currentByte >= 48 && currentByte <= 57 { | |
result = (result << 1) + (result << 3) + int(currentByte-48) | |
r.needle = r.needle + 1 | |
if r.needle >= len(r.input) { | |
break | |
} | |
currentByte = r.input[r.needle] | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment