Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to check if array contains all elements of a given range in TypeScript

1 Answer

0 votes
function check_elements(arr: number[], rang_from: number, rang_to: number) {
	const range = rang_to - rang_from;
  	const size = arr.length;
          
  	for (let i = 0; i < size; i++) {
         if (arr[i] >= rang_from && arr[i] <= rang_to) {
              const j = arr[i] - rang_from;
              if (arr[j] > 0) {
                  arr[j] *= -1;
              }
              let s = "";
              for (let k = 0; k < size; k++) {
                   s += arr[k] + " ";
              }
              console.log(s);
            }
        }
       
        for (let i = 0; i < range; i++) {
            if (arr[i] > 0)
                return false;
        }
        
        return true;
}

const arr = [1, 4, 5, 2, 3, 7, 8, 3, 9, 4, 4, 0, 6];
const rang_from = 3, rang_to = 7; 
 
if (check_elements(arr, rang_from, rang_to))
    console.log("Yes");
else
    console.log("No");
 
 
 
 
   
/*
run:
         
"1 -4 5 2 3 7 8 3 9 4 4 0 6 " 
"1 -4 -5 2 3 7 8 3 9 4 4 0 6 " 
"-1 -4 -5 2 3 7 8 3 9 4 4 0 6 " 
"-1 -4 -5 2 -3 7 8 3 9 4 4 0 6 " 
"-1 -4 -5 2 -3 7 8 3 9 4 4 0 6 " 
"-1 -4 -5 2 -3 7 8 3 9 4 4 0 6 " 
"-1 -4 -5 2 -3 7 8 3 9 4 4 0 6 " 
"-1 -4 -5 -2 -3 7 8 3 9 4 4 0 6 " 
"Yes" 
 
*/

 



answered Dec 14, 2021 by avibootz
edited Dec 14, 2021 by avibootz

Related questions

1 answer 165 views
1 answer 181 views
1 answer 142 views
1 answer 158 views
1 answer 142 views
...