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

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 answers

573 users

How to find all pairs in array that equal to a given sum in Rust

1 Answer

0 votes
#![allow(non_snake_case)]

fn findAllPairs(arr : [usize; 7], sum : usize) {
    let mut found : bool = false;
    {
        let mut i : usize = 0;
        while i < arr.len() - 1 {
            {
                let mut j : usize = i + 1;
                while j < arr.len() {
                    if arr[i as usize] + arr[j as usize] == sum {
                        println!("arr[{}]({}) + arr[{}]({})", i, arr[i], j, arr[j]);
                        found = true;
                    }
                    j += 1;
                }
            }
            i += 1;
        }
    }
    if !found {
        println!("Pair not found");
    }
}
    
fn main()
{
    let arr: [usize; 7] = [2, 4, 1, 5, 6, 8, 1];
        
    let sum : usize = 10;
    
    findAllPairs(arr, sum);
}





/*
run:
 
arr[0](2) + arr[5](8)
arr[1](4) + arr[4](6)
 
*/

 



answered Apr 16, 2023 by avibootz

Related questions

1 answer 215 views
1 answer 179 views
1 answer 206 views
1 answer 240 views
1 answer 204 views
...