Skip to content

Instantly share code, notes, and snippets.

@Rezwanul-Haque
Created February 7, 2023 17:14
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 Rezwanul-Haque/68b77739eb122b2f2762e16316dc4877 to your computer and use it in GitHub Desktop.
Save Rezwanul-Haque/68b77739eb122b2f2762e16316dc4877 to your computer and use it in GitHub Desktop.
Md. Rezwanul Haque Razib task solution
package main
import (
"fmt"
"reflect"
"math"
)
//PROBLEM 01
//
//Write a program that takes in a string as input and determines whether it is a palindrome (i.e. a word or phrase that reads the same forwards and backwards).
//
//Challenge: Can you solve it without reversing the string?
//
//Example:
//Input: “abbc”
//Output: false
//
//Input: “abba”
//Output: true
//
//Input: “a”
//Output: true
//
//Input: “”
//Output: true
func problem1() {
str := "abbc"
palindrome := true
length := len(str)
if length == 0 || length == 1 {
fmt.Println(palindrome)
return
}
middle := (length + 1) / 2
for i := 0; i <= middle; i++ {
if str[i] == str[length-1-i] {
continue
} else {
palindrome = false
break
}
}
fmt.Println(palindrome)
return
}
//PROBLEM 02
//Given an array nums containing n distinct numbers in the range [0, n], return the missing number.
//Example:
//Input: [3, 0, 1]
//Output: [2]
//Input: [0, 1]
//Output: [2]
//Constraints: All numbers in the array will be unique.
func minMax(arr []int) (int, int) {
min := 999999
max := -999999
for _, v := range arr {
if v <= min {
min = v
}
if v >= max {
max = v
}
}
return min, max
}
func problem2() {
arr := []int{3, 0, 1}
min, max := minMax(arr)
//fmt.Println(min, max)
missing := 0
for i := min; i <= max; i++ {
if !Contains(arr, i) {
missing = i
}
}
if missing == 0 {
missing = max + 1
}
fmt.Println(missing)
}
func Contains(list interface{}, elem interface{}) bool {
listV := reflect.ValueOf(list)
if listV.Kind() == reflect.Slice {
for i := 0; i < listV.Len(); i++ {
item := listV.Index(i).Interface()
target := reflect.ValueOf(elem).Convert(reflect.TypeOf(item)).Interface()
if ok := reflect.DeepEqual(item, target); ok {
return true
}
}
}
return false
}
//PROBLEM 03
//You are given a sorted array of integers in non-decreasing order. You have to remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.
//In-place operation indicates you cannot use any extra space, memory or array to perform the task.
//[NOTE: YOU CAN NOT PERFORM ANY REMOVE OPERATION ON THE ARRAY. YOU CANNOT USE ANY EXTRA MEMORY]
//After you do some operation on the array, the duplicates can only remain at the rightmost end of
//the array.
//Example:-
//Input: [1, 1, 2, 3, 3, 3, 4, 4, 5, 6]
//Output: [1, 2, 3, 4, 5, 6, 4, 4, 5, 6]
//Input: [1, 1, 2]
//Output: [1, 2, 2]
//Input: [1, 2, 2]
//Output: [1, 2, 2]
//Input: [1, 2, 2, 3]
//
//Output: [1, 2, 3, 3]
//To know about in-place sorting: In-place algorithm - Wikipedia
func reverseInPlace(arr []float64) []float64 {
length := len(arr)
var i float64 = 0
for i = 0; i <= math.Floor((float64(length)-2)/2); i++ {
arr[int(i)], arr[int(length)-1-int(i)] = arr[int(length)-1-int(i)], arr[int(i)]
}
return arr
}
func problem3() {
arr := []float64{1, 1, 2}
fmt.Println(reverseInPlace(arr))
return
}
func main() {
// problem 1
problem1()
// problem 2
problem2()
// problem 3
problem3()
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment