DEV Community

Kazi Abdur Rakib
Kazi Abdur Rakib

Posted on

1

test

`c++
// something easy approach
int pairSum(int *arr, int n, int num)
{

sort(arr, arr + n); 
int count=0;
// for(int l=0;l<n-1;l++)
// {
    int i=0;
    int j=n-1;
    while(i<j)
    {
        int sum=arr[i]+arr[j];
        if(sum==num)
        {
            if(arr[i]==arr[i+1] || arr[j]==arr[j-1])
            {
                int p=j-1;
                while(arr[p]==arr[j] && p>i)
                {
                    count++;
                    p--;
                }
                count++;
                i++;
            }
            else
            {
                count++;
                i++;
                j--;
            }
        }
        else if(sum<num)
        {
            i++;
        }
        else
        {
            j--;
        }
    }

return count;
Enter fullscreen mode Exit fullscreen mode

}

// // SOmething Hard approach
// int pairSum(int *arr, int n, int num)
// {

// // befor sort=> example: 1 5 2 1 7 2 3 2

// /* We can sort using Merge Sort or using inbuilt Algorithm */

// //MergeSort
// if(n==0){return n;}
// mergeSortHelper(arr, 0, n-1);

// // //Algorithm
// // sort(arr, arr + n);

// int startIndex = 0;
// int endIndex = n - 1;
// int numPairs = 0;
// //after sort=> example: 1 1 2 2 2 3 5 7
// /*
// 1 1 2 2 2 3 3 5 7
// 1 1 2 2 2 3 3 5
// 1 1 2 2 2 3 3 => 1+3 & 1+3 (2 times 1* 2 times 3= 4)
// /
// while (startIndex < endIndex)
// {
// if (arr[startIndex] + arr[endIndex] < num) // 1+2 < 4 hole start++
// {
// startIndex++;
// }
// else if (arr[startIndex] + arr[endIndex] > num)// 1+7>4 hole end --
// {
// endIndex--;
// }
// else
// {
// /

// 1 1 2 2 2 3 3 5 7
// 1 1 2 2 2 3 3 5
// 1 1 2 2 2 3 3 => 1+3 & 1+3 (2 times 1* 2 times 3= 4)

// next step code for 2 2 2
// */
// int elementAtStart = arr[startIndex]; // 0=> 1
// int elementAtEnd = arr[endIndex]; // 5=> 3

// if (elementAtStart == elementAtEnd) // example: 2 2 2
// { int totalElementsFromStartToEnd = (endIndex - startIndex) + 1;
// numPairs += (totalElementsFromStartToEnd * (totalElementsFromStartToEnd - 1) / 2); //so 15 pairs
// return numPairs;
// }

// /*
// 1 1 2 2 2 3 3 5 7
// 1 1 2 2 2 3 3 5
// 1 1 2 2 2 3 3 => 1+3 & 1+3 (2 times 1* 2 times 3= 4)

// next code for 1 1 3 3
// */

// int tempStartIndex = startIndex + 1; //1
// int tempEndIndex = endIndex - 1; // 4

// // index 1 value = index 0 value hole (same then tempStartIndex increase untill not same and make the elementAtStart=tempStartIndex)
// while (tempStartIndex <= tempEndIndex && arr[tempStartIndex] == elementAtStart)
// {
// tempStartIndex += 1;
// }
// //index 4 value = index 5 value hole (same then tempEndIndex decrease untill not same and make the elementAtEnd=tempEndIndex)
// while (tempStartIndex <= tempEndIndex && arr[tempEndIndex] == elementAtEnd)
// {
// tempEndIndex -= 1;
// }
// int totalElementsFromStart = (tempStartIndex - startIndex);
// int totalElementsFromEnd = (endIndex - tempEndIndex);
// numPairs += (totalElementsFromStart * totalElementsFromEnd);
// startIndex = tempStartIndex; endIndex = tempEndIndex;
// }
// }
// return numPairs;
// }
`

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay