Question 7

7 comments on “Question 7”


  • Mainak

    import java.util.*;

    public class ans9 {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    ArrayList list=new ArrayList();
    int n=sc.nextInt();
    int d=sc.nextInt();
    int i=0;
    while(i<n){
    list.add(sc.nextInt());
    i++;
    }
    int trip=0;
    for(int j=0;j<n;j++){
    if(list.contains(list.get(j)+d) && list.contains(list.get(j)+2*d)){
    trip++;
    }
    }
    System.out.println(trip);
    }
    }


  • sohail17inamdar

    Python Wrote my own code
    pr = [1,2,4,5,7,8,10]
    d = 3
    value = 0
    n = len(pr)

    for i in range(0,n):

    for j in range(0,n):
    if pr[i] == pr[j] + d:
    for k in range(n-1,-1,-1):
    if pr[i] == pr[k] – d:
    value += 1

    print(value)


  • culbgaming12

    #include
    using namespace std;
    /*Brute Force Method(O(n^3))..!
    int main()
    {
    int n, d;

    cin >> n >> d;

    int arr[n];

    for (int i = 0; i > arr[i];
    }

    int i, j, k;
    int count = 0;
    for (i = 0; i < n – 2; i++)
    {
    for (j = i + 1; j < n – 1; j++)
    {
    for (k = j + 1; k < n; k++)
    {
    if ((arr[j] – arr[i] == d) && (arr[k] – arr[j] == d))
    {
    count++;
    }
    }
    }
    }

    cout << count << endl;
    return 0;
    }*/

    // O(nlogn) Time Complexity using Binary Search..!
    bool BinarySearch(int arr[], int low, int high, int k)
    {
    int mid = (low + high) / 2;

    if (low k)
    {
    BinarySearch(arr, 0, mid – 1, k);
    }

    else
    {
    BinarySearch(arr, mid + 1, high, k);
    }
    }
    }

    int main()
    {
    int n, d;

    cin >> n >> d;
    int arr[n];
    for (int i = 0; i > arr[i];
    }
    int count = 0;
    for (int i = 0; i < n; i++)
    {
    if (BinarySearch(arr, i, n – 1, arr[i] + d) && BinarySearch(arr, i, n – 1, arr[i] + 2 * d))
    {
    count++;
    cout << count << endl;
    }
    }

    return 0;
    }

    Dont know why my optimized solution isn't working..?! Please help.. if anyone figures out what sort of mistake I've done..!


  • Pratik

    from collections import Counter
    def bt(d,arr):
    m = Counter(arr)
    count = 0
    for num in arr:
    if m[num+d] and m[num+d+d]:
    count += 1
    return count
    n,d = map(int, input().split())
    arr = list(map(int, input().rstrip().split()))
    result = bt(d,arr)
    print(result)


  • Gangadhar

    import java.util.ArrayList;
    import java.util.Scanner;

    public class Solution {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“enter length and diffrence:”);
    int len = sc.nextInt();
    int diff = sc.nextInt();

    System.out.println(“enter numbers:”);

    ArrayList list = new ArrayList();
    for (int i = 0; i < len; i++) {

    list.add(sc.nextInt());
    }
    int count=0;
    for (int i = 0; i < len; i++) {
    if (list.contains((list.get(i)+diff)) && list.contains(list.get(i)+2*diff)) {
    count++;
    }
    }
    System.out.println(count);
    }

    }


  • Madhusudan

    {
    int arr[256],i=0,j=0,k=0,temp=0,count=0,n=0,d=0;
    printf(“please enter size of ARRAY”);
    scanf(“%d”,&n);
    printf(“please enter the d value”);
    scanf(” %d”,&d);
    printf(“value :”);
    for (i=0;i<n;i++){
    scanf(" %d",&arr[i]);
    }

    //sort
    for(i =0 ; i<n; i++){
    for (j=i; jarr[j]){
    temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    }
    }
    }
    //for triplet
    for(i=0; i<n-2; i++){
    for(j=i+1; j<n-1; j++){
    for(k=j+1; k<n; k++) {
    if(((arr[j]-arr[i])==d)&&((arr[k]-arr[j])==d))
    count++;
    }
    }
    }
    printf(" %d",count);
    return 0;
    }


  • Debparna

    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    String li[] = str.split(” “);
    int n = Integer.parseInt(li[0]);
    int d = Integer.parseInt(li[1]);
    int arr[] = new int[n];
    str = sc.nextLine();
    li = str.split(” “);
    int max = Integer.MIN_VALUE;
    int min = Integer.MAX_VALUE;
    if(li.length !=n){
    System.out.println(“Invalid Input”);
    }else{
    for(int i=0;i<n;i++){
    arr[i] = Integer.parseInt(li[i]);
    if(max arr[i]){
    min = arr[i];
    }
    }

    boolean visP[] = new boolean[max+1+d];
    boolean visN[] = new boolean[-1*min +1 + d];
    int cnt = 0;
    Arrays.fill(visP, false);
    Arrays.fill(visN, false);
    for(int i=0;i= 0){
    visP[arr[i]] = true;
    }else{
    visN[-1*arr[i]] = true;
    }
    }
    for(int i=0;i= 0){
    //System.out.println(i+” “+ arr[i]+” “+visP[arr[i]-d]+” “+ visP[arr[i]+d]);
    if(visP[arr[i]-d] && visP[arr[i]+d]){
    cnt++;
    }
    }else if(arr[i]+d < 0){
    if(visN[-1*(arr[i]-d)] && visN[-1*(arr[i]+d)]){
    cnt++;
    }
    }else{
    if(visN[-1*(arr[i]-d)] && visP[arr[i]+d]){
    cnt++;
    }
    }
    }
    System.out.println(cnt);
    }
    }
    }