Assalomu alaykum, yordam.uz saytimizga xush kelibsiz.
Bu saytda o`zingizni qiziqtirgan savollarga javob olishingiz va o`z sohangiz bo`yicha savollarga javob berishingiz mumkin. Bizning Oilamizga a'zo bo`lganingiz uchun chuqur Minnatdorchilik bildiramiz !!!

B massivda bo'lgan A massivning juft elementlari

+3 ovoz
221 marta ko‘rilgan
so‘radi 12 iyun, 17 Ilhomjon (25 bal)
tahrirlandi 12 iyun, 17 Saidolim
Butun sonli A va B massivlar berilgan. Shunday C massivni topingki, u o'zida B massivda bo'lgan A massivning juft elementlarini saqlasin

1 ta javob

0 ovoz
javob berdi 17 iyun, 17 SedBigDaddy (1,237 bal)

Masalan:

 void printIntersection(int massiv1[], int massiv2[], int m, int n)
{
    // Before finding intersection, make sure massiv1[0..m-1] is smaller
    if (m > n)
    {
        int *tempp = massiv1;
        massiv1= massiv2;
        massiv2= tempp;
 
        int temp = m;
        m = n;
        n = temp;
    }
 
    // Now arr1[] is smaller
 
    // Sort smaller array massiv1[0..m-1]
    sort(massiv1, massiv1+ m);
 
    // Search every element of bigger array in smaller array
    // and print the element if found
    for (int i=0; i<n; i++)
        if (binarySearch(massiv1, 0, m-1, massiv2[i]) != -1)
            cout << massiv2[i] << " ";
}

// A C++ program to print union and intersection of two unsorted arrays

#include <iostream>

#include <algorithm>

using namespace std;

int binarySearch(int arr[], int l, int r, int x);

// Prints union of arr1[0..m-1] and arr2[0..n-1]

void printUnion(int arr1[], int arr2[], int m, int n)

{

    // Before finding union, make sure arr1[0..m-1] is smaller

    if (m > n)

    {

        int *tempp = arr1;

        arr1 = arr2;

        arr2 = tempp;

        int temp = m;

        m = n;

        n = temp;

    }

    // Now arr1[] is smaller

    // Sort the first array and print its elements (these two

    // steps can be swapped as order in output is not important)

    sort(arr1, arr1 + m);

    for (int i=0; i<m; i++)

        cout << arr1[i] << " ";

    // Search every element of bigger array in smaller array

    // and print the element if not found

    for (int i=0; i<n; i++)

        if (binarySearch(arr1, 0, m-1, arr2[i]) == -1)

            cout << arr2[i] << " ";

}

// Prints intersection of arr1[0..m-1] and arr2[0..n-1]

void printIntersection(int arr1[], int arr2[], int m, int n)

{

    // Before finding intersection, make sure arr1[0..m-1] is smaller

    if (m > n)

    {

        int *tempp = arr1;

        arr1 = arr2;

        arr2 = tempp;

        int temp = m;

        m = n;

        n = temp;

    }

    // Now arr1[] is smaller

    // Sort smaller array arr1[0..m-1]

    sort(arr1, arr1 + m);

    // Search every element of bigger array in smaller array

    // and print the element if found

    for (int i=0; i<n; i++)

        if (binarySearch(arr1, 0, m-1, arr2[i]) != -1)

            cout << arr2[i] << " ";

}

// A recursive binary search function. It returns location of x in

// given array arr[l..r] is present, otherwise -1

int binarySearch(int arr[], int l, int r, int x)

{

    if (r >= l)

    {

        int mid = l + (r - l)/2;

        // If the element is present at the middle itself

        if (arr[mid] == x)  return mid;

        // If element is smaller than mid, then it can only be present

        // in left subarray

        if (arr[mid] > x) return binarySearch(arr, l, mid-1, x);

        // Else the element can only be present in right subarray

        return binarySearch(arr, mid+1, r, x);

    }

    // We reach here when element is not present in array

    return -1;

}

/* Driver program to test above function */

int main()

{

    

  int massiv1[] = {7, 1, 5, 2, 3, 6};

    int massiv2[] = {3, 8, 6, 20, 7};

    int m = sizeof(massiv1)/sizeof(massiv1[0]);

    int n = sizeof(massiv2)/sizeof(massiv2[0]);

    cout << "\nJusf massivlar  \n";

    printIntersection(massiv1, massiv2, m, n);

    return 0;

}

Assalomu alaykum, yordam.uz saytimizga xush kelibsiz.

Bu saytda o`zingizni qiziqtirgan savollarga javob olishingiz va o`z sohangiz bo`yicha savollarga javob berishingiz mumkin.

Bizning Oilamizga a'zo bo`lganingiz uchun chuqur Minnatdorchilik bildiramiz !!!

Telegram kanal YordamUzRss

...